|
| 1 | +import cls from '../../services/cls.js'; |
1 | 2 | import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest'; |
2 | | -import dayjs from "dayjs"; |
3 | 3 | import { resolveDateParams } from "./edited-notes.js"; |
4 | 4 |
|
5 | | -function resolveAsDate(dateStr: string) { |
6 | | - return resolveDateParams(dateStr).date; |
| 5 | +// test date setup |
| 6 | +// client: UTC+1 |
| 7 | +// server: UTC |
| 8 | +// day/month/year is changed when server converts a client date to to UTC |
| 9 | +const clientDate = "2025-01-01 00:11:11.000+0100"; |
| 10 | +const serverDate = "2024-12-31 23:11:11.000Z"; |
| 11 | + |
| 12 | +// expected values - from client's point of view |
| 13 | +const expectedToday = "2025-01-01"; |
| 14 | +const expectedTodayMinus1 = "2024-12-31"; |
| 15 | +const expectedMonth = "2025-01"; |
| 16 | +const expectedMonthMinus2 = "2024-11"; |
| 17 | +const expectedYear = "2025"; |
| 18 | +const expectedYearMinus1 = "2024"; |
| 19 | + |
| 20 | +function runTest(dateStrToResolve: string, expectedDate: string) { |
| 21 | + cls.init(() => { |
| 22 | + cls.set("localNowDateTime", clientDate); |
| 23 | + const resolvedDate = resolveDateParams(dateStrToResolve).date; |
| 24 | + expect(resolvedDate).toBe(expectedDate); |
| 25 | + }); |
7 | 26 | } |
8 | 27 |
|
9 | | -describe("edited-notes::resolveAsDate", () => { |
| 28 | +describe("edited-notes::resolveDateParams", () => { |
10 | 29 | beforeEach(() => { |
11 | | - // Set a fixed date and time before each test |
12 | 30 | vi.useFakeTimers(); |
13 | | - vi.setSystemTime(new Date('2012-11-10T23:22:21Z')); // NOTE!!: Date wrap in my timezone |
| 31 | + vi.setSystemTime(new Date(serverDate)); |
14 | 32 | }); |
15 | 33 |
|
16 | 34 | afterEach(() => { |
17 | 35 | // Restore real timers after each test |
18 | 36 | vi.useRealTimers(); |
19 | 37 | }); |
20 | 38 |
|
21 | | - |
22 | 39 | it("resolves 'TODAY' to today's date", () => { |
23 | | - const expectedDate = dayjs().format("YYYY-MM-DD"); |
24 | | - const resolvedDate = resolveAsDate("TODAY"); |
25 | | - expect(resolvedDate).toBe(expectedDate); |
| 40 | + runTest("TODAY", expectedToday); |
26 | 41 | }); |
27 | 42 |
|
28 | 43 | it("resolves 'MONTH' to current month", () => { |
29 | | - const expectedMonth = dayjs().format("YYYY-MM"); |
30 | | - const resolvedMonth = resolveAsDate("MONTH"); |
31 | | - expect(resolvedMonth).toBe(expectedMonth); |
| 44 | + runTest("MONTH", expectedMonth); |
32 | 45 | }); |
33 | 46 |
|
34 | 47 | it("resolves 'YEAR' to current year", () => { |
35 | | - const expectedYear = dayjs().format("YYYY"); |
36 | | - const resolvedYear = resolveAsDate("YEAR"); |
37 | | - expect(resolvedYear).toBe(expectedYear); |
| 48 | + runTest("YEAR", expectedYear); |
38 | 49 | }); |
39 | 50 |
|
40 | 51 | it("resolves 'TODAY-1' to yesterday's date", () => { |
41 | | - const expectedDate = dayjs().subtract(1, "day").format("YYYY-MM-DD"); |
42 | | - const resolvedDate = resolveAsDate("TODAY-1"); |
43 | | - expect(resolvedDate).toBe(expectedDate); |
| 52 | + runTest("TODAY-1", expectedTodayMinus1); |
44 | 53 | }); |
45 | 54 |
|
46 | 55 | it("resolves 'MONTH-2' to 2 months ago", () => { |
47 | | - const expectedMonth = dayjs().subtract(2, "month").format("YYYY-MM"); |
48 | | - const resolvedMonth = resolveAsDate("MONTH-2"); |
49 | | - expect(resolvedMonth).toBe(expectedMonth); |
| 56 | + runTest("MONTH-2", expectedMonthMinus2); |
50 | 57 | }); |
51 | 58 |
|
52 | | - it("resolves 'YEAR+1' to next year", () => { |
53 | | - const expectedYear = dayjs().add(1, "year").format("YYYY"); |
54 | | - const resolvedYear = resolveAsDate("YEAR+1"); |
55 | | - expect(resolvedYear).toBe(expectedYear); |
| 59 | + it("resolves 'YEAR-1' to last year", () => { |
| 60 | + runTest("YEAR-1", expectedYearMinus1); |
56 | 61 | }); |
57 | 62 |
|
58 | 63 | it("returns original string for unrecognized keyword", () => { |
59 | | - const unrecognizedString = "NOT_A_DYNAMIC_DATE"; |
60 | | - const resolvedString = resolveAsDate(unrecognizedString); |
61 | | - expect(resolvedString).toBe(unrecognizedString); |
| 64 | + runTest("FOO", "FOO"); |
62 | 65 | }); |
63 | 66 |
|
64 | 67 | it("returns original string for partially recognized keyword", () => { |
65 | | - const partialString = "TODAY-"; |
66 | | - const resolvedString = resolveAsDate(partialString); |
67 | | - expect(resolvedString).toBe(partialString); |
| 68 | + runTest("TODAY-", "TODAY-"); |
68 | 69 | }); |
69 | 70 |
|
70 | 71 | it("resolves 'today' (lowercase) to today's date", () => { |
71 | | - const expectedDate = dayjs().format("YYYY-MM-DD"); |
72 | | - const resolvedDate = resolveAsDate("today"); |
73 | | - expect(resolvedDate).toBe(expectedDate); |
| 72 | + runTest("today", expectedToday); |
74 | 73 | }); |
75 | 74 |
|
76 | 75 | }); |
0 commit comments