|
1 | 1 | import { DateUtilities } from './date'; |
2 | 2 |
|
3 | | -describe('date utities', () => { |
| 3 | +describe('date utilities', () => { |
| 4 | + beforeAll(() => { |
| 5 | + jest.useFakeTimers().setSystemTime(new Date('2023-10-01')); |
| 6 | + }); |
| 7 | + |
| 8 | + afterAll(() => { |
| 9 | + jest.useRealTimers(); |
| 10 | + }); |
| 11 | + |
4 | 12 | describe('when calling toIsoDate', () => { |
5 | | - it('should return the date in ISO format with `MMM yyyy` format', () => { |
| 13 | + it('should correctly format a date in `MMM yyyy` format to ISO format', () => { |
6 | 14 | const date = 'Mar 2026'; |
7 | 15 | const expected = '2026-03-01'; |
8 | 16 |
|
9 | | - const result = DateUtilities.toIsoDate(date, 'MMM yyyy'); |
| 17 | + const result = DateUtilities.toIsoDate(date); |
10 | 18 | expect(result).toBe(expected); |
11 | 19 | }); |
12 | 20 |
|
13 | | - it('should return the date in ISO format with `yyyy` format', () => { |
| 21 | + it('should correctly format a date in `yyyy` format to ISO format', () => { |
14 | 22 | const date = '2026'; |
15 | 23 | const expected = '2026-01-01'; |
16 | 24 |
|
17 | | - const result = DateUtilities.toIsoDate(date, 'yyyy'); |
| 25 | + const result = DateUtilities.toIsoDate(date); |
18 | 26 | expect(result).toBe(expected); |
19 | 27 | }); |
20 | 28 |
|
21 | | - it('should return undefined when the date is not provided', () => { |
| 29 | + it("should return today's date in ISO format when the date is not provided", () => { |
22 | 30 | const date = undefined; |
| 31 | + const expected = '2023-10-01'; |
23 | 32 |
|
24 | | - const result = DateUtilities.toIsoDate(date, 'MMM yyyy'); |
25 | | - expect(result).toBeUndefined(); |
| 33 | + const result = DateUtilities.toIsoDate(date); |
| 34 | + expect(result).toBe(expected); |
26 | 35 | }); |
27 | 36 |
|
28 | | - it('should return undefined when the date is not valid', () => { |
| 37 | + it("should return today's date in ISO format when the date is invalid", () => { |
29 | 38 | const date = 'invalid date'; |
| 39 | + const expected = '2023-10-01'; |
30 | 40 |
|
31 | | - const result = DateUtilities.toIsoDate(date, 'MMM yyyy'); |
32 | | - expect(result).toBeUndefined(); |
| 41 | + const result = DateUtilities.toIsoDate(date); |
| 42 | + expect(result).toBe(expected); |
33 | 43 | }); |
34 | 44 | }); |
35 | 45 | }); |
0 commit comments