Skip to content

Commit 73aab8c

Browse files
VIA-629 AJ/EO Move getNow() to utils and add tests
1 parent edf7bf5 commit 73aab8c

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

src/utils/date.test.ts

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import { UtcDateFromStringSchema, UtcDateTimeFromStringSchema } from "@src/utils/date";
2-
import { calculateAge } from "@src/utils/date";
1+
import { UtcDateFromStringSchema, UtcDateTimeFromStringSchema, calculateAge, getNow } from "@src/utils/date";
2+
import { headers } from "next/headers";
3+
4+
jest.mock("next/headers");
35

46
describe("utils-date", () => {
57
describe("UtcDateFromStringSchema", () => {
@@ -64,6 +66,7 @@ describe("utils-date", () => {
6466
}).toThrow();
6567
});
6668
});
69+
6770
describe("calculateAge", () => {
6871
beforeAll(() => {
6972
jest.useFakeTimers();
@@ -117,4 +120,41 @@ describe("utils-date", () => {
117120
}).toThrow();
118121
});
119122
});
123+
124+
describe("getNow", () => {
125+
const fakeDateInSystem = "2000-01-01T01:01:01Z";
126+
const fakeDateInHeader = "1212-12-12T12:12:12Z";
127+
128+
beforeEach(() => {
129+
jest.useFakeTimers();
130+
jest.setSystemTime(new Date(fakeDateInSystem));
131+
});
132+
133+
it("should return current date by default", async () => {
134+
expect(await getNow()).toEqual(new Date(fakeDateInSystem));
135+
});
136+
137+
it("should return date set in the header, when it is valid", async () => {
138+
const mockHeaders = {
139+
get: jest.fn(() => {
140+
return fakeDateInHeader;
141+
}),
142+
};
143+
(headers as jest.Mock).mockResolvedValue(mockHeaders);
144+
145+
expect(await getNow()).toEqual(new Date(fakeDateInHeader));
146+
});
147+
148+
it("should return current date when date set in the header is malformed", async () => {
149+
const fakeDateInHeaderInvalid = "invalid-date";
150+
const mockHeaders = {
151+
get: jest.fn(() => {
152+
return fakeDateInHeaderInvalid;
153+
}),
154+
};
155+
(headers as jest.Mock).mockResolvedValue(mockHeaders);
156+
157+
expect(await getNow()).toEqual(new Date(fakeDateInSystem));
158+
});
159+
});
120160
});

src/utils/date.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Age } from "@src/utils/auth/types";
22
import { differenceInYears } from "date-fns";
3+
import { headers } from "next/headers";
34
import { z } from "zod";
45

56
const UtcDateFromStringSchema = z
@@ -38,4 +39,13 @@ const calculateAge = (date: string): Age => {
3839
return differenceInYears(today, birthDate) as Age;
3940
};
4041

41-
export { UtcDateFromStringSchema, UtcDateTimeFromStringSchema, calculateAge };
42+
const getNow = async (): Promise<Date> => {
43+
try {
44+
const headersList = await headers();
45+
return UtcDateTimeFromStringSchema.safeParse(headersList.get("x-e2e-datetime")).data ?? new Date();
46+
} catch {
47+
return new Date();
48+
}
49+
};
50+
51+
export { UtcDateFromStringSchema, UtcDateTimeFromStringSchema, calculateAge, getNow };

0 commit comments

Comments
 (0)