From 8679de9d39e53bdb12f78db3c454f5f7811417fb Mon Sep 17 00:00:00 2001 From: Emma Mansell <73774046+7emansell@users.noreply.github.com> Date: Tue, 26 Nov 2024 13:26:11 -0500 Subject: [PATCH] Last couple tests and changelog --- CHANGELOG.md | 4 ++ app/divisions/page.tsx | 1 + app/src/utils/apiHelpers.test.tsx | 111 ++++++++++++++++++++++++++---- package-lock.json | 4 +- 4 files changed, 106 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ceb2dea8..18ad3225 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/app/divisions/page.tsx b/app/divisions/page.tsx index bdda5a2c..53980c3a 100644 --- a/app/divisions/page.tsx +++ b/app/divisions/page.tsx @@ -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"); diff --git a/app/src/utils/apiHelpers.test.tsx b/app/src/utils/apiHelpers.test.tsx index 08508604..9676d344 100644 --- a/app/src/utils/apiHelpers.test.tsx +++ b/app/src/utils/apiHelpers.test.tsx @@ -1,5 +1,8 @@ -import { waitFor } from "@testing-library/react"; import { + getDivisionData, + getFeaturedImage, + getFeaturedItemData, + getHomePageData, getItemsCountFromUUIDs, getNumDigitizedItems, getRandomFeaturedItem, @@ -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"); }); }); @@ -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"); + }); +}); diff --git a/package-lock.json b/package-lock.json index 6dc58a12..835d17b1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "digital-collections", - "version": "0.2.1", + "version": "0.2.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "digital-collections", - "version": "0.2.1", + "version": "0.2.2", "dependencies": { "@samvera/clover-iiif": "^2.9.0", "@types/node": "20.5.9",