|
| 1 | +/** |
| 2 | + * Please refer to the terms of the license agreement in the root of the project |
| 3 | + * |
| 4 | + * (c) 2025 Feedzai |
| 5 | + */ |
| 6 | +import { formatDate } from "../../../../src/functions"; |
| 7 | + |
| 8 | +const DEFAULT_LOCALES = { locales: "en-US" }; |
| 9 | + |
| 10 | +describe("formatDate", () => { |
| 11 | + it("should format date string correctly", () => { |
| 12 | + expect(formatDate({ date: "2024-01-15", ...DEFAULT_LOCALES })).to.equal( |
| 13 | + "January 15" |
| 14 | + ); |
| 15 | + expect(formatDate({ date: "2024-12-31", ...DEFAULT_LOCALES })).to.equal( |
| 16 | + "December 31" |
| 17 | + ); |
| 18 | + }); |
| 19 | + |
| 20 | + it("should format Date object correctly", () => { |
| 21 | + const dateObject = new Date("2024-01-15"); |
| 22 | + |
| 23 | + expect(formatDate({ date: dateObject, ...DEFAULT_LOCALES })).to.equal( |
| 24 | + "January 15" |
| 25 | + ); |
| 26 | + }); |
| 27 | + |
| 28 | + it("should format timestamp correctly", () => { |
| 29 | + const timestamp = new Date("2024-01-15").getTime(); |
| 30 | + |
| 31 | + expect(formatDate({ date: timestamp, ...DEFAULT_LOCALES })).to.equal( |
| 32 | + "January 15" |
| 33 | + ); |
| 34 | + }); |
| 35 | + |
| 36 | + it("should handle different date string formats", () => { |
| 37 | + expect(formatDate({ date: "2024/01/15", ...DEFAULT_LOCALES })).to.equal( |
| 38 | + "January 15" |
| 39 | + ); |
| 40 | + expect(formatDate({ date: "01-15-2024", ...DEFAULT_LOCALES })).to.equal( |
| 41 | + "January 15" |
| 42 | + ); |
| 43 | + expect(formatDate({ date: "15 Jan 2024", ...DEFAULT_LOCALES })).to.equal( |
| 44 | + "January 15" |
| 45 | + ); |
| 46 | + }); |
| 47 | + |
| 48 | + it("should include year when specified", () => { |
| 49 | + const customOptions = { year: "numeric" } as const; |
| 50 | + |
| 51 | + expect( |
| 52 | + formatDate({ |
| 53 | + date: "2024-01-15", |
| 54 | + options: customOptions, |
| 55 | + ...DEFAULT_LOCALES, |
| 56 | + }) |
| 57 | + ).to.equal("January 15, 2024"); |
| 58 | + }); |
| 59 | + |
| 60 | + it("should show short month format", () => { |
| 61 | + const customOptions = { month: "short" } as const; |
| 62 | + |
| 63 | + expect( |
| 64 | + formatDate({ |
| 65 | + date: "2024-01-15", |
| 66 | + options: customOptions, |
| 67 | + ...DEFAULT_LOCALES, |
| 68 | + }) |
| 69 | + ).to.equal("Jan 15"); |
| 70 | + }); |
| 71 | + |
| 72 | + it("should include weekday", () => { |
| 73 | + const customOptions = { weekday: "long" } as const; |
| 74 | + |
| 75 | + expect( |
| 76 | + formatDate({ |
| 77 | + date: "2024-01-15", |
| 78 | + options: customOptions, |
| 79 | + ...DEFAULT_LOCALES, |
| 80 | + }) |
| 81 | + ).to.equal("Monday, January 15"); |
| 82 | + }); |
| 83 | + |
| 84 | + it("should respect all provided options while maintaining defaults", () => { |
| 85 | + const customOptions = { |
| 86 | + weekday: "long", |
| 87 | + year: "numeric", |
| 88 | + month: "long", |
| 89 | + day: "numeric", |
| 90 | + } as const; |
| 91 | + |
| 92 | + expect( |
| 93 | + formatDate({ |
| 94 | + date: "2024-01-15", |
| 95 | + options: customOptions, |
| 96 | + ...DEFAULT_LOCALES, |
| 97 | + }) |
| 98 | + ).to.equal("Monday, January 15, 2024"); |
| 99 | + }); |
| 100 | + |
| 101 | + it("should handle invalid dates", () => { |
| 102 | + expect(() => formatDate({ date: "invalid-date" })).throw(); |
| 103 | + expect(() => formatDate({ date: "2024-13-45" })).throw(); |
| 104 | + }); |
| 105 | + |
| 106 | + it("should handle empty input", () => { |
| 107 | + expect(() => formatDate({ date: "" })).throw(); |
| 108 | + }); |
| 109 | + |
| 110 | + it("should handle null and undefined", () => { |
| 111 | + expect(() => formatDate({ date: null })).throw(); |
| 112 | + expect(() => formatDate()).throw(); |
| 113 | + }); |
| 114 | +}); |
0 commit comments