|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +import { |
| 6 | + constructLocalDateString, |
| 7 | + constructLocalTimeAndDateStrings, |
| 8 | +} from './email-helpers'; |
| 9 | + |
| 10 | +describe('EmailHelpers', () => { |
| 11 | + afterEach(() => { |
| 12 | + jest.restoreAllMocks(); |
| 13 | + }); |
| 14 | + |
| 15 | + describe('constructLocalTimeAndDateStrings', () => { |
| 16 | + beforeEach(() => { |
| 17 | + jest |
| 18 | + .spyOn(Date, 'now') |
| 19 | + .mockReturnValue(new Date('2024-01-15T20:30:45Z').getTime()); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should construct time and date strings with timezone', () => { |
| 23 | + const result = constructLocalTimeAndDateStrings( |
| 24 | + 'America/Los_Angeles', |
| 25 | + 'en-US' |
| 26 | + ); |
| 27 | + |
| 28 | + expect(result.time).toEqual('12:30:45 PM (PST)'); |
| 29 | + expect(result.date).toEqual('Monday, Jan 15, 2024'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should use default timezone when not provided', () => { |
| 33 | + const result = constructLocalTimeAndDateStrings(undefined, 'en-US'); |
| 34 | + |
| 35 | + expect(result.time).toEqual('8:30:45 PM (UTC)'); |
| 36 | + expect(result.date).toEqual('Monday, Jan 15, 2024'); |
| 37 | + expect(result.acceptLanguage).toEqual('en-US'); |
| 38 | + expect(result.timeZone).toEqual('Etc/UTC'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should use default locale when not provided', () => { |
| 42 | + const result = constructLocalTimeAndDateStrings('America/New_York'); |
| 43 | + |
| 44 | + expect(result.time).toEqual('3:30:45 PM (EST)'); |
| 45 | + expect(result.date).toEqual('Monday, Jan 15, 2024'); |
| 46 | + expect(result.acceptLanguage).toEqual('en'); |
| 47 | + expect(result.timeZone).toEqual('America/New_York'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should handle different locales', () => { |
| 51 | + const result = constructLocalTimeAndDateStrings('Europe/London', 'fr-FR'); |
| 52 | + |
| 53 | + expect(result.time).toEqual('20:30:45 (GMT)'); |
| 54 | + expect(result.date).toEqual('lundi, 15 janv. 2024'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should format with no parameters', () => { |
| 58 | + const result = constructLocalTimeAndDateStrings(); |
| 59 | + |
| 60 | + expect(result.time).toEqual('8:30:45 PM (UTC)'); |
| 61 | + expect(result.date).toEqual('Monday, Jan 15, 2024'); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + describe('constructLocalDateString', () => { |
| 66 | + it('should construct localized date string with timezone', () => { |
| 67 | + const date = new Date('2024-01-15T12:00:00Z'); |
| 68 | + |
| 69 | + const result = constructLocalDateString( |
| 70 | + 'America/Los_Angeles', |
| 71 | + 'en-US', |
| 72 | + date |
| 73 | + ); |
| 74 | + |
| 75 | + expect(result).toEqual('01/15/2024'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should accept custom format string', () => { |
| 79 | + const date = new Date('2024-01-15T12:00:00Z'); |
| 80 | + |
| 81 | + const result = constructLocalDateString( |
| 82 | + 'America/Chicago', |
| 83 | + 'en-US', |
| 84 | + date, |
| 85 | + 'YYYY-MM-DD' |
| 86 | + ); |
| 87 | + |
| 88 | + expect(result).toEqual('2024-01-15'); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should use current date when not provided', () => { |
| 92 | + jest |
| 93 | + .spyOn(Date, 'now') |
| 94 | + .mockReturnValue(new Date('2024-01-15T12:00:00Z').getTime()); |
| 95 | + |
| 96 | + const result = constructLocalDateString('America/Denver', 'en-US'); |
| 97 | + |
| 98 | + expect(result).toEqual('01/15/2024'); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should handle different locales', () => { |
| 102 | + const date = new Date('2024-01-15T12:00:00Z'); |
| 103 | + |
| 104 | + const result = constructLocalDateString('Europe/Paris', 'de-DE', date); |
| 105 | + |
| 106 | + expect(result).toEqual('15.01.2024'); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should accept timestamp as date parameter', () => { |
| 110 | + jest |
| 111 | + .spyOn(Date, 'now') |
| 112 | + .mockReturnValue(new Date('2025-12-31T23:59:59Z').getTime()); |
| 113 | + |
| 114 | + const timestamp = Date.now(); |
| 115 | + |
| 116 | + const result = constructLocalDateString( |
| 117 | + 'Asia/Tokyo', |
| 118 | + 'ja-JP', |
| 119 | + timestamp // epoch timestamp instead of Date object |
| 120 | + ); |
| 121 | + |
| 122 | + expect(result).toEqual('2026/01/01'); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should use default timezone when not provided', () => { |
| 126 | + const date = new Date('2024-01-15T02:00:00Z'); |
| 127 | + const resultDefault = constructLocalDateString(undefined, 'en-US', date); |
| 128 | + |
| 129 | + const resultEst = constructLocalDateString( |
| 130 | + 'America/New_York', |
| 131 | + 'en-US', |
| 132 | + date |
| 133 | + ); |
| 134 | + |
| 135 | + expect(resultDefault).not.toBe(resultEst); |
| 136 | + expect(resultDefault).toEqual('01/15/2024'); |
| 137 | + expect(resultEst).toEqual('01/14/2024'); |
| 138 | + }); |
| 139 | + }); |
| 140 | +}); |
0 commit comments