|
| 1 | +import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest'; |
| 2 | +import dayjs from "dayjs"; |
| 3 | +import { resolveDateParams } from "./edited-notes.js"; |
| 4 | + |
| 5 | +function resolveAsDate(dateStr: string) { |
| 6 | + return resolveDateParams(dateStr).date; |
| 7 | +} |
| 8 | + |
| 9 | +describe("edited-notes::resolveAsDate", () => { |
| 10 | + beforeEach(() => { |
| 11 | + // Set a fixed date and time before each test |
| 12 | + vi.useFakeTimers(); |
| 13 | + vi.setSystemTime(new Date('2012-11-10T23:22:21Z')); // NOTE!!: Date wrap in my timezone |
| 14 | + }); |
| 15 | + |
| 16 | + afterEach(() => { |
| 17 | + // Restore real timers after each test |
| 18 | + vi.useRealTimers(); |
| 19 | + }); |
| 20 | + |
| 21 | + |
| 22 | + 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); |
| 26 | + }); |
| 27 | + |
| 28 | + it("resolves 'MONTH' to current month", () => { |
| 29 | + const expectedMonth = dayjs().format("YYYY-MM"); |
| 30 | + const resolvedMonth = resolveAsDate("MONTH"); |
| 31 | + expect(resolvedMonth).toBe(expectedMonth); |
| 32 | + }); |
| 33 | + |
| 34 | + it("resolves 'YEAR' to current year", () => { |
| 35 | + const expectedYear = dayjs().format("YYYY"); |
| 36 | + const resolvedYear = resolveAsDate("YEAR"); |
| 37 | + expect(resolvedYear).toBe(expectedYear); |
| 38 | + }); |
| 39 | + |
| 40 | + 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); |
| 44 | + }); |
| 45 | + |
| 46 | + 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); |
| 50 | + }); |
| 51 | + |
| 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); |
| 56 | + }); |
| 57 | + |
| 58 | + it("returns original string for unrecognized keyword", () => { |
| 59 | + const unrecognizedString = "NOT_A_DYNAMIC_DATE"; |
| 60 | + const resolvedString = resolveAsDate(unrecognizedString); |
| 61 | + expect(resolvedString).toBe(unrecognizedString); |
| 62 | + }); |
| 63 | + |
| 64 | + it("returns original string for partially recognized keyword", () => { |
| 65 | + const partialString = "TODAY-"; |
| 66 | + const resolvedString = resolveAsDate(partialString); |
| 67 | + expect(resolvedString).toBe(partialString); |
| 68 | + }); |
| 69 | + |
| 70 | + 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); |
| 74 | + }); |
| 75 | + |
| 76 | +}); |
0 commit comments