|
1 | 1 | import { formatDate } from "."; |
2 | | -import { FormatDateOptions } from "./formatDate"; |
3 | 2 |
|
4 | | -interface FormatDateTestCase extends FormatDateOptions { |
| 3 | +interface FormatDateTestCase { |
5 | 4 | title: string; |
6 | 5 | date: string | Date; |
7 | | - outputFormat: string; |
| 6 | + format?: Intl.DateTimeFormatOptions; |
| 7 | + locale?: string; |
8 | 8 | result: string; |
9 | 9 | } |
10 | 10 |
|
| 11 | +const today: Date = new Date(); |
| 12 | + |
11 | 13 | describe("Util: formatDate", () => { |
12 | 14 | const testCases: Array<FormatDateTestCase> = [ |
13 | 15 | { |
14 | | - title: "Should return formatted Date", |
| 16 | + title: "Should format to Swedish by default", |
15 | 17 | date: "7-12-1987", |
16 | | - inputFormat: "D-MM-YYYY", |
17 | | - outputFormat: "MM-DD-YYYY", |
18 | | - result: "12-07-1987" |
| 18 | + result: "12 juli 1987", |
19 | 19 | }, |
20 | 20 | { |
21 | | - title: "Should return null when date is invalid", |
| 21 | + title: "Should use other locale when passed (en-US)", |
| 22 | + date: "7-12-1987", |
| 23 | + locale: "en-US", |
| 24 | + result: "July 12, 1987", |
| 25 | + }, |
| 26 | + { |
| 27 | + title: |
| 28 | + "Should return a string of the input if it's an invalid date", |
22 | 29 | date: "it's a string", |
23 | | - inputFormat: "D-MM-YYYY", |
24 | | - outputFormat: "MM-DD-YYYY", |
25 | | - result: null |
| 30 | + result: "it's a string", |
26 | 31 | }, |
27 | 32 | { |
28 | | - title: "Should return null if date is invalid and invalid output is undefined", |
29 | | - date: null, |
30 | | - inputFormat: "D-MM-YYYY", |
31 | | - outputFormat: "MM-DD-YYYY", |
32 | | - result: null |
| 33 | + title: |
| 34 | + "Should correctly format ISO string date without passing input or output formats", |
| 35 | + date: "2000-01-23T04:56:07.000+00:00", |
| 36 | + result: "23 januari 2000", |
33 | 37 | }, |
34 | 38 | { |
35 | | - title: "Should return formatted Date based on locale", |
36 | | - date: new Date("12-7-1987"), |
37 | | - inputFormat: "D-MM-YYYY", |
38 | | - outputFormat: "YYYY MMM DD", |
39 | | - locale: "sv-SE", |
40 | | - result: "1987 dec 07" |
| 39 | + title: "Should accept date object", |
| 40 | + date: new Date(), |
| 41 | + format: { day: "numeric", month: "numeric", year: "numeric" }, |
| 42 | + result: today.toLocaleDateString("sv-SE"), |
41 | 43 | }, |
42 | 44 | { |
43 | | - title: "Should correctly format ISO string date without passing input or output formats", |
44 | | - date: "2000-01-23T04:56:07.000+00:00", |
45 | | - outputFormat: "DD-MM-YYYY", |
46 | | - result: "23-01-2000" |
47 | | - } |
| 45 | + title: "Should accept custom format", |
| 46 | + date: new Date(), |
| 47 | + format: { day: "numeric", month: "numeric" }, |
| 48 | + result: `${today.getDate()}/${today.getMonth() + 1}`, |
| 49 | + }, |
48 | 50 | ]; |
49 | | - testCases.map((testCase: FormatDateTestCase) => { |
50 | | - it(testCase.title, () => { |
51 | | - expect(formatDate(testCase.date, testCase.outputFormat, { ...testCase })).toEqual(testCase.result); |
52 | | - }); |
53 | | - }); |
54 | | - |
55 | | - it("Should correctly format ISO string date without passing input formats", () => { |
56 | | - const formatted: string = formatDate("2000-01-23T04:56:07.000+00:00", "DD-MM-YYYY"); |
57 | | - expect(formatted).toEqual("23-01-2000"); |
58 | | - }); |
| 51 | + testCases.map( |
| 52 | + ({ title, date, format, locale, result }: FormatDateTestCase) => { |
| 53 | + // prettier-ignore |
| 54 | + it(`${title} (Sample: ${String(date)} - Returns: ${result})`, () => { |
| 55 | + expect(formatDate(date, format, locale)).toEqual(result); |
| 56 | + }); |
| 57 | + } |
| 58 | + ); |
59 | 59 | }); |
0 commit comments