Skip to content

Commit

Permalink
Last couple tests and changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
7emansell committed Nov 26, 2024
1 parent a26fcb7 commit 8679de9
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 14 deletions.
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

### Added

- Added tests for API helpers (DR-3271)

## [0.2.3] 2024-11-21

### Updated
Expand Down
1 change: 1 addition & 0 deletions app/divisions/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const metadata: Metadata = {

export default async function Divisions() {
const data = await getDivisionData();
console.log(data);
// Repo API returns 404s within the data.
if (data?.headers?.code === "404") {
redirect("/404");
Expand Down
111 changes: 99 additions & 12 deletions app/src/utils/apiHelpers.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { waitFor } from "@testing-library/react";
import {
getDivisionData,
getFeaturedImage,
getFeaturedItemData,
getHomePageData,
getItemsCountFromUUIDs,
getNumDigitizedItems,
getRandomFeaturedItem,
Expand All @@ -17,21 +20,84 @@ beforeEach(() => {
jest.clearAllMocks();
});

describe("getHomePageData", () => {});
describe("getHomePageData", () => {
it("creates response containing random number and all 7 lanes", async () => {
(apiResponse as jest.Mock).mockResolvedValueOnce(
Promise.resolve({
nyplAPI: {
response: {
counts: {
count: [
{
uuid: { $: "test" },
count_value: { $: "10" },
},
],
},
},
},
})
);
const result = await getHomePageData();
expect(apiResponse as jest.Mock).toHaveBeenCalled();
expect([0, 1]).toContain(result.randomNumber);
expect(result.lanesWithNumItems.length).toEqual(7);
// Test uuid response doesn't match the collection uuid, so all of these should be 0.
expect(
result.lanesWithNumItems[0].collections[3].numberOfDigitizedItems
).toEqual("0");
});
});

describe("getFeaturedItemData", () => {});
describe("getFeaturedItemData", () => {
it("creates response containing featuredItem and numDigitizedItems", async () => {
const result = await getFeaturedItemData();

describe("getFeaturedImage", () => {});
// Fallback values:
expect(result.numberOfDigitizedItems).toEqual("875,861");
expect(result.featuredItem.imageID).toEqual(
defaultFeaturedItem.production.featuredItem.imageID
);
});
});

describe("getItemData", () => {
it("returns expected item", async () => {
describe("getDivisionData", () => {
it("forms the correct request from params", async () => {
const result = await getDivisionData({
slug: "testSlug",
pageNum: 1,
perPage: 3,
});

expect(apiResponse as jest.Mock).toHaveBeenCalledWith(
`${process.env.API_URL}/api/v2/divisions/testSlug?page=1&per_page=3`
);
});
it("returns successful response", async () => {
(apiResponse as jest.Mock).mockResolvedValueOnce(
Promise.resolve(mockItemResponse)
Promise.resolve({
headers: { status: "success", code: "200", message: "ok" },
summary: "divisions test",
divisions: [
{
name: "Billy Rose Theatre Division",
slug: "billy-rose-theatre-division",
collections: [],
},
{
name: "Carl H. Pforzheimer Collection of Shelley and His Circle",
slug: "carl-h-pforzheimer-collection-of-shelley-and-his-circle",
collections: [],
},
],
})
);
const item = await getRandomFeaturedItem();
expect(item).toEqual(mockItemResponse);
expect(item).toHaveProperty("capture");
expect(item).toHaveProperty("mods");
const result = await getDivisionData({
pageNum: 1,
perPage: 2,
});
expect(result.divisions.length).toEqual(2);
expect(result).toHaveProperty("summary");
});
});

Expand Down Expand Up @@ -122,4 +188,25 @@ describe("getRandomFeaturedItem", () => {
});
});

describe("getDivisionData", () => {});
describe("getFeaturedImage", () => {
it("returns expected item", async () => {
(apiResponse as jest.Mock).mockResolvedValueOnce(
Promise.resolve(defaultFeaturedItem["production"].featuredItem)
);
const item = await getFeaturedImage();
expect(item).toHaveProperty("imageID");
expect(item.imageID).toEqual("482815");
});
});

describe("getItemData", () => {
it("returns expected item", async () => {
(apiResponse as jest.Mock).mockResolvedValueOnce(
Promise.resolve(mockItemResponse)
);
const item = await getRandomFeaturedItem();
expect(item).toEqual(mockItemResponse);
expect(item).toHaveProperty("capture");
expect(item).toHaveProperty("mods");
});
});
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8679de9

Please sign in to comment.