|
1 | | -import { describe, it, expect } from "vitest"; |
2 | | -import { parseTimeInputToISOString } from "./utils"; |
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; |
| 2 | +import { parseTimeInputToISOString, getTodayDateString } from "./utils"; |
3 | 3 |
|
4 | 4 | describe("parseTimeInputToISOString", () => { |
5 | 5 | const baseDate = "2024-07-31"; // This will be treated as YYYY-MM-DD 00:00:00 in the local timezone by new Date() |
@@ -121,3 +121,52 @@ describe("parseTimeInputToISOString", () => { |
121 | 121 | ); |
122 | 122 | }); |
123 | 123 | }); |
| 124 | + |
| 125 | +describe("getTodayDateString", () => { |
| 126 | + beforeEach(() => { |
| 127 | + // Mock Date.prototype.toLocaleString |
| 128 | + vi.spyOn(Date.prototype, "toLocaleString").mockImplementation( |
| 129 | + // @ts-expect-error - Mocking Date.prototype.toLocaleString with simplified signature |
| 130 | + function ( |
| 131 | + this: Date, |
| 132 | + locales?: string | string[], |
| 133 | + options?: Intl.DateTimeFormatOptions, |
| 134 | + ) { |
| 135 | + if (locales === "ja-JP" && options?.timeZone === "Asia/Tokyo") { |
| 136 | + // Mock a specific date for testing |
| 137 | + return "2024/12/31 12:00:00"; |
| 138 | + } |
| 139 | + return ""; |
| 140 | + }, |
| 141 | + ); |
| 142 | + }); |
| 143 | + |
| 144 | + afterEach(() => { |
| 145 | + vi.restoreAllMocks(); |
| 146 | + }); |
| 147 | + |
| 148 | + it("should return today's date in YYYY-MM-DD format", () => { |
| 149 | + const result = getTodayDateString(); |
| 150 | + expect(result).toBe("2024-12-31"); |
| 151 | + }); |
| 152 | + |
| 153 | + it("should pad single-digit month and day with zero", () => { |
| 154 | + // Mock a date with single-digit month and day |
| 155 | + vi.spyOn(Date.prototype, "toLocaleString").mockImplementation( |
| 156 | + // @ts-expect-error - Mocking Date.prototype.toLocaleString with simplified signature |
| 157 | + function ( |
| 158 | + this: Date, |
| 159 | + locales?: string | string[], |
| 160 | + options?: Intl.DateTimeFormatOptions, |
| 161 | + ) { |
| 162 | + if (locales === "ja-JP" && options?.timeZone === "Asia/Tokyo") { |
| 163 | + return "2024/1/5 12:00:00"; |
| 164 | + } |
| 165 | + return ""; |
| 166 | + }, |
| 167 | + ); |
| 168 | + |
| 169 | + const result = getTodayDateString(); |
| 170 | + expect(result).toBe("2024-01-05"); |
| 171 | + }); |
| 172 | +}); |
0 commit comments