Skip to content

Commit

Permalink
Okay
Browse files Browse the repository at this point in the history
  • Loading branch information
7emansell committed Nov 20, 2024
1 parent 660d5a2 commit e68900d
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Updated

- Refactor `getHomePageData` to pull item counts from collection endpoints (DR-3297)

## [0.2.0] 2024-11-20

### Removed
Expand Down
4 changes: 3 additions & 1 deletion app/src/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import appConfig from "../../../appConfig";
import defaultFeaturedItems from "../data/defaultFeaturedItemData";
import { CARDS_PER_PAGE } from "../config/constants";
import { DC_URL } from "../config/constants";
import { getItemsCountFromCollections } from "./itemCount";

export const getHomePageData = async () => {
const randomNumber = Math.floor(Math.random() * 2);
Expand All @@ -15,7 +16,8 @@ export const getHomePageData = async () => {
const allCollectionUUIDs: string[] = lanes.reduce((acc, lane) => {
return acc.concat(lane.collections.map((collection) => collection.uuid));
}, [] as string[]);
const uuidtoItemCountMap = await getItemsCountFromUUIDs(allCollectionUUIDs);
const uuidtoItemCountMap =
await getItemsCountFromCollections(allCollectionUUIDs);

// Update the collections for each lane with the number of items
const updatedLanes = lanes.map((lane) => {
Expand Down
67 changes: 67 additions & 0 deletions app/src/utils/itemCount.test.tsx
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({});
});
});
23 changes: 23 additions & 0 deletions app/src/utils/itemCount.ts
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;
};

0 comments on commit e68900d

Please sign in to comment.