From b7a0b13889d8fdc960bd2021cccf2b40c19f55e9 Mon Sep 17 00:00:00 2001 From: Paul Beaudoin Date: Wed, 20 Dec 2023 10:51:30 -0500 Subject: [PATCH] Fix TZ breaking tests --- pages/test-pickup-times/index.tsx | 4 +- src/utils/nyplCore.ts | 13 +-- src/utils/pickupTimeEstimator.ts | 3 +- .../utilsTests/pickupTimeEstimator.test.js | 85 +++++++++++++------ 4 files changed, 68 insertions(+), 37 deletions(-) diff --git a/pages/test-pickup-times/index.tsx b/pages/test-pickup-times/index.tsx index 476a5c9b5..49680d90f 100644 --- a/pages/test-pickup-times/index.tsx +++ b/pages/test-pickup-times/index.tsx @@ -401,7 +401,7 @@ export async function getServerSideProps({ resolvedUrl }) { requestTime ).catch((e) => { error = e.message - error += '\n__________________________________' + e.stack + error += "\n__________________________________" + e.stack return {} }) // const result = null @@ -411,7 +411,7 @@ export async function getServerSideProps({ resolvedUrl }) { fulfillment, holdingLocation, result: JSON.parse(JSON.stringify(result)), - error + error, }, } } diff --git a/src/utils/nyplCore.ts b/src/utils/nyplCore.ts index f0d5533e4..72310d12d 100644 --- a/src/utils/nyplCore.ts +++ b/src/utils/nyplCore.ts @@ -1,14 +1,15 @@ -export async function nyplCore (which: string) { - const response = await fetch(`https://nypl-core-objects-mapping-qa.s3.amazonaws.com/by_${which}.json`) +export async function nyplCore(which: string) { + const response = await fetch( + `https://nypl-core-objects-mapping-qa.s3.amazonaws.com/by_${which}.json` + ) if (response.status >= 400) { throw new Error(`Could not fetch ${which} from nypl-core S3`) } - const mapping = await response.json() - .catch((e) => { - throw new Error(`Could not parse ${which} from nypl-core S3: ${e.message}`) - }) + const mapping = await response.json().catch((e) => { + throw new Error(`Could not parse ${which} from nypl-core S3: ${e.message}`) + }) return mapping } diff --git a/src/utils/pickupTimeEstimator.ts b/src/utils/pickupTimeEstimator.ts index 6d3b76ea2..b95be12b9 100644 --- a/src/utils/pickupTimeEstimator.ts +++ b/src/utils/pickupTimeEstimator.ts @@ -48,7 +48,8 @@ export const getPickupTimeEstimate = async ( const fulfillments = await nyplCore("fulfillment") const fulfillment = fulfillments[item[`${type}Fulfillment`]] - if (!fulfillment) throw new Error(`Invalid fulfillment: ${item[`${type}Fulfillment`]}`) + if (!fulfillment) + throw new Error(`Invalid fulfillment: ${item[`${type}Fulfillment`]}`) fromTimestamp = fromTimestamp || module.exports.now() diff --git a/src/utils/utilsTests/pickupTimeEstimator.test.js b/src/utils/utilsTests/pickupTimeEstimator.test.js index d8e4deb4b..6a2f34255 100644 --- a/src/utils/utilsTests/pickupTimeEstimator.test.js +++ b/src/utils/utilsTests/pickupTimeEstimator.test.js @@ -120,28 +120,57 @@ jest.mock("../../../pages/api/locations", () => { describe("pickupTimeEstimator", () => { beforeEach(() => { - window.fetch = jest.fn() - .mockImplementation((url) => { - return Promise.resolve({ - ok: true, - json: () => { - if (/fulfillment.json$/.test(url)) { - return Promise.resolve({ - "fulfillment:hd-offsite": { estimatedTime: "P2D", label: "Offsite Harvard Depository" }, - "fulfillment:recap-offsite": { estimatedTime: "P1D", label: "Offsite ReCAP" }, - "fulfillment:sasb-edd": { estimatedTime: "P2W", label: "EDD Onsite" }, - "fulfillment:lpa-edd": { estimatedTime: "P2W", label: "EDD Onsite" }, - "fulfillment:hd-edd": { estimatedTime: "P1W", label: "EDD Harvard Depository" }, - "fulfillment:sasb-onsite": { estimatedTime: "PT45M", label: "Onsite SASB" }, - "fulfillment:sc-edd": { estimatedTime: "P2W", label: "EDD Onsite" }, - "fulfillment:sc-onsite": { estimatedTime: "PT15M", label: "Onsite Schomburg" }, - "fulfillment:recap-edd": { estimatedTime: "P1W", label: "EDD ReCAP" }, - "fulfillment:lpa-onsite": { estimatedTime: "PT45M", label: "Onsite LPA" } - }) - } + window.fetch = jest.fn().mockImplementation((url) => { + return Promise.resolve({ + ok: true, + json: () => { + if (/fulfillment.json$/.test(url)) { + return Promise.resolve({ + "fulfillment:hd-offsite": { + estimatedTime: "P2D", + label: "Offsite Harvard Depository", + }, + "fulfillment:recap-offsite": { + estimatedTime: "P1D", + label: "Offsite ReCAP", + }, + "fulfillment:sasb-edd": { + estimatedTime: "P2W", + label: "EDD Onsite", + }, + "fulfillment:lpa-edd": { + estimatedTime: "P2W", + label: "EDD Onsite", + }, + "fulfillment:hd-edd": { + estimatedTime: "P1W", + label: "EDD Harvard Depository", + }, + "fulfillment:sasb-onsite": { + estimatedTime: "PT45M", + label: "Onsite SASB", + }, + "fulfillment:sc-edd": { + estimatedTime: "P2W", + label: "EDD Onsite", + }, + "fulfillment:sc-onsite": { + estimatedTime: "PT15M", + label: "Onsite Schomburg", + }, + "fulfillment:recap-edd": { + estimatedTime: "P1W", + label: "EDD ReCAP", + }, + "fulfillment:lpa-onsite": { + estimatedTime: "PT45M", + label: "Onsite LPA", + }, + }) } - }) + }, }) + }) // Add window global nyOffsets var, emulating a global var set by server // for the client, giving the current and future NY TZ offsets @@ -149,7 +178,7 @@ describe("pickupTimeEstimator", () => { { from: "2023-03-10T06:00:00.000Z", offset: 4 }, { from: "2023-11-05T06:00:00.000Z", offset: 5 }, ] - mockNowTimestamp = "2023-06-01T12:00:00" + mockNowTimestamp = "2023-06-01T12:00:00-04:00" jest.spyOn(estimator, "now").mockImplementation(() => mockNowTimestamp) // When running tests with TZ set to anything other than ET, let"s expect @@ -211,7 +240,7 @@ describe("pickupTimeEstimator", () => { }) it("should return tomorrow when called at end of service hours", async () => { - mockNowTimestamp = "2023-06-01T18:00:00" + mockNowTimestamp = "2023-06-01T18:00:00-04:00" expect(await estimator.getNextServiceHours("sc")).toEqual({ day: "Friday", startTime: "2023-06-02T14:00:00.000Z", @@ -223,11 +252,11 @@ describe("pickupTimeEstimator", () => { // Each of these times is afterhours ;[ // 7pm closing: - "2023-06-01T19:00:09", + "2023-06-01T19:00:09-04:00", // 8pm: - "2023-06-01T20:00:00", + "2023-06-01T20:00:00-04:00", // 2am: - "2023-06-02T02:00:00", + "2023-06-02T02:00:00-04:00", ].forEach(async (timestamp) => { it(`should return tomorrow when called after service hours (${timestamp})`, async () => { mockNowTimestamp = timestamp @@ -243,7 +272,7 @@ describe("pickupTimeEstimator", () => { describe("getServiceTime", () => { it('should return start of tomorrow"s service time when after hours', async () => { - mockNowTimestamp = "2023-06-01T23:00:00" + mockNowTimestamp = "2023-06-01T23:00:00-04:00" expect(await estimator.getServiceTime("sc")).toBe( "2023-06-02T14:00:00.000Z" ) @@ -255,8 +284,8 @@ describe("pickupTimeEstimator", () => { }) it("should return the given time if it falls within the next destination service time", async () => { - expect(await estimator.getServiceTime("sc", "2023-06-01T12:00:00")).toBe( - "2023-06-01T12:00:00" + expect(await estimator.getServiceTime("sc", "2023-06-01T12:00:00-04:00")).toBe( + "2023-06-01T12:00:00-04:00" ) })