-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
97 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { apiResponse } from "./api"; | ||
import { getItemsCountFromCollections } from "./itemCount"; | ||
|
||
jest.mock("./api"); | ||
|
||
describe("getItemsCountFromCollections()", () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("should return the correct numItems for each UUID", async () => { | ||
(apiResponse as jest.Mock) | ||
.mockResolvedValueOnce( | ||
Promise.resolve({ | ||
status: 200, | ||
numItems: 10, | ||
}) | ||
) | ||
.mockResolvedValueOnce( | ||
Promise.resolve({ | ||
status: 200, | ||
numItems: 20, | ||
}) | ||
); | ||
|
||
const uuids = ["uuid1", "uuid2"]; | ||
|
||
const results = await getItemsCountFromCollections(uuids); | ||
|
||
expect(results).toEqual({ | ||
uuid1: 10, | ||
uuid2: 20, | ||
}); | ||
}); | ||
|
||
it("should handle server error gracefully", async () => { | ||
const uuids = ["uuid1"]; | ||
(apiResponse as jest.Mock).mockRejectedValueOnce( | ||
Promise.resolve({ | ||
status: 400, | ||
}) | ||
); | ||
const results = await getItemsCountFromCollections(uuids); | ||
expect(results).toEqual({ | ||
uuid1: 0, | ||
}); | ||
}); | ||
|
||
it("should handle missing numItems field gracefully", async () => { | ||
const uuids = ["uuid1"]; | ||
(apiResponse as jest.Mock).mockResolvedValueOnce( | ||
Promise.resolve({ | ||
status: 200, | ||
}) | ||
); | ||
const results = await getItemsCountFromCollections(uuids); | ||
expect(results).toEqual({ | ||
uuid1: 0, | ||
}); | ||
}); | ||
|
||
it("should handle an empty array of UUIDs", async () => { | ||
const uuids = []; | ||
const results = await getItemsCountFromCollections(uuids); | ||
expect(results).toEqual({}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { apiResponse } from "./api"; | ||
|
||
/** | ||
* Returns a map of collection uuids with corresponding item counts. | ||
* @param {string[]} uuids - collection uuids | ||
*/ | ||
export const getItemsCountFromCollections = async (uuids: string[]) => { | ||
const results = {}; | ||
const apiUrl = `${process.env.API_URL}/api/v2/collections/`; | ||
await Promise.all( | ||
uuids.map(async (uuid) => { | ||
try { | ||
const response = await apiResponse(`${apiUrl}${uuid}`); | ||
const numItems = response.numItems || 0; | ||
results[uuid] = numItems; | ||
} catch (error) { | ||
console.error(`Error fetching collection ${uuid}:`, error.message); | ||
results[uuid] = 0; | ||
} | ||
}) | ||
); | ||
return results; | ||
}; |