|
| 1 | +import { LiquidDate } from './liquid-date' |
| 2 | +import { disableIntl } from '../../test/stub/no-intl' |
| 3 | + |
| 4 | +describe('LiquidDate', () => { |
| 5 | + describe('timezone', () => { |
| 6 | + it('should respect timezone set to 00:00', () => { |
| 7 | + const date = new LiquidDate('2021-10-06T14:26:00.000+08:00', 'en-US', 0) |
| 8 | + expect(date.getTimezoneOffset()).toBe(0) |
| 9 | + expect(date.getHours()).toBe(6) |
| 10 | + expect(date.getMinutes()).toBe(26) |
| 11 | + }) |
| 12 | + it('should respect timezone set to -06:00', () => { |
| 13 | + const date = new LiquidDate('2021-10-06T14:26:00.000+08:00', 'en-US', -360) |
| 14 | + expect(date.getTimezoneOffset()).toBe(-360) |
| 15 | + expect(date.getMinutes()).toBe(26) |
| 16 | + }) |
| 17 | + }) |
| 18 | + it('should support Date as argument', () => { |
| 19 | + const date = new LiquidDate(new Date('2021-10-06T14:26:00.000+08:00'), 'en-US', 0) |
| 20 | + expect(date.getHours()).toBe(6) |
| 21 | + }) |
| 22 | + it('should support .getMilliseconds()', () => { |
| 23 | + const date = new LiquidDate('2021-10-06T14:26:00.001+00:00', 'en-US', 0) |
| 24 | + expect(date.getMilliseconds()).toBe(1) |
| 25 | + }) |
| 26 | + it('should support .getDay()', () => { |
| 27 | + const date = new LiquidDate('2021-12-07T00:00:00.001+08:00', 'en-US', -480) |
| 28 | + expect(date.getDay()).toBe(2) |
| 29 | + }) |
| 30 | + it('should support .toLocaleString()', () => { |
| 31 | + const date = new LiquidDate('2021-10-06T00:00:00.001+00:00', 'en-US', -480) |
| 32 | + expect(date.toLocaleString('en-US')).toMatch(/8:00:00\sAM$/) |
| 33 | + expect(date.toLocaleString('en-US', { timeZone: 'America/New_York' })).toMatch(/8:00:00\sPM$/) |
| 34 | + expect(() => date.toLocaleString()).not.toThrow() |
| 35 | + }) |
| 36 | + it('should support .toLocaleTimeString()', () => { |
| 37 | + const date = new LiquidDate('2021-10-06T00:00:00.001+00:00', 'en-US', -480) |
| 38 | + expect(date.toLocaleTimeString('en-US')).toMatch(/^8:00:00\sAM$/) |
| 39 | + expect(() => date.toLocaleDateString()).not.toThrow() |
| 40 | + }) |
| 41 | + it('should support .toLocaleDateString()', () => { |
| 42 | + const date = new LiquidDate('2021-10-06T22:00:00.001+00:00', 'en-US', -480) |
| 43 | + expect(date.toLocaleDateString('en-US')).toBe('10/7/2021') |
| 44 | + expect(() => date.toLocaleDateString()).not.toThrow() |
| 45 | + }) |
| 46 | + describe('compatibility', () => { |
| 47 | + disableIntl() |
| 48 | + it('should use English months if Intl.DateTimeFormat not supported', () => { |
| 49 | + expect(new LiquidDate('2021-10-06T22:00:00.001+00:00', 'en-US', -480).getLongMonthName()).toEqual('October') |
| 50 | + expect(new LiquidDate('2021-10-06T22:00:00.001+00:00', 'zh-CN', -480).getLongMonthName()).toEqual('October') |
| 51 | + expect(new LiquidDate('2021-10-06T22:00:00.001+00:00', 'zh-CN', -480).getShortMonthName()).toEqual('Oct') |
| 52 | + }) |
| 53 | + it('should use English weekdays if Intl.DateTimeFormat not supported', () => { |
| 54 | + expect(new LiquidDate('2024-07-21T22:00:00.001+00:00', 'en-US', 0).getLongWeekdayName()).toEqual('Sunday') |
| 55 | + expect(new LiquidDate('2024-07-21T22:00:00.001+00:00', 'zh-CN', -480).getLongWeekdayName()).toEqual('Monday') |
| 56 | + expect(new LiquidDate('2024-07-21T22:00:00.001+00:00', 'zh-CN', -480).getShortWeekdayName()).toEqual('Mon') |
| 57 | + }) |
| 58 | + it('should return none for timezone if Intl.DateTimeFormat not supported', () => { |
| 59 | + expect(new LiquidDate('2024-07-21T22:00:00.001', 'en-US').getTimeZoneName()).toEqual(undefined) |
| 60 | + }) |
| 61 | + }) |
| 62 | +}) |
0 commit comments