Skip to content

Commit

Permalink
Fix TZ breaking tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nonword committed Dec 20, 2023
1 parent eb9708e commit b7a0b13
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 37 deletions.
4 changes: 2 additions & 2 deletions pages/test-pickup-times/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -411,7 +411,7 @@ export async function getServerSideProps({ resolvedUrl }) {
fulfillment,
holdingLocation,
result: JSON.parse(JSON.stringify(result)),
error
error,
},
}
}
Expand Down
13 changes: 7 additions & 6 deletions src/utils/nyplCore.ts
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 2 additions & 1 deletion src/utils/pickupTimeEstimator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
85 changes: 57 additions & 28 deletions src/utils/utilsTests/pickupTimeEstimator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,36 +120,65 @@ 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
window.nyOffsets = [
{ 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
Expand Down Expand Up @@ -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",
Expand All @@ -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
Expand All @@ -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"
)
Expand All @@ -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"
)
})

Expand Down

0 comments on commit b7a0b13

Please sign in to comment.