1 import { formatIntlDate } from '../../lib/date/formatIntlDate';
3 describe('formatIntlDate()', () => {
4 it('adapts order of year and month and day for different languages', () => {
5 const date = new Date(2023, 5, 17, 11, 44, 2);
7 expect(formatIntlDate(date, { month: 'short', year: 'numeric' }, 'en-US')).toEqual('Jun 2023');
8 expect(formatIntlDate(date, { month: 'short', year: 'numeric' }, 'zh-ZH')).toEqual('2023年6月');
11 it('offers four built-in date formatting options (not including time)', () => {
12 const date = new Date(2023, 5, 17, 11, 44, 2);
15 expect(formatIntlDate(date, { dateStyle: 'full' }, 'en-US')).toEqual('Saturday, June 17, 2023');
16 expect(formatIntlDate(date, { dateStyle: 'long' }, 'en-US')).toEqual('June 17, 2023');
17 expect(formatIntlDate(date, { dateStyle: 'short' }, 'en-US')).toEqual('6/17/23');
18 expect(formatIntlDate(date, { dateStyle: 'medium' }, 'en-US')).toEqual('Jun 17, 2023');
21 expect(formatIntlDate(date, { dateStyle: 'full' }, 'it')).toEqual('sabato 17 giugno 2023');
22 expect(formatIntlDate(date, { dateStyle: 'long' }, 'it')).toEqual('17 giugno 2023');
23 expect(formatIntlDate(date, { dateStyle: 'short' }, 'it')).toEqual('17/06/23');
24 expect(formatIntlDate(date, { dateStyle: 'medium' }, 'it')).toEqual('17 giu 2023');
27 expect(formatIntlDate(date, { dateStyle: 'full' }, 'ko')).toEqual('2023년 6월 17일 토요일');
28 expect(formatIntlDate(date, { dateStyle: 'long' }, 'ko')).toEqual('2023년 6월 17일');
29 expect(formatIntlDate(date, { dateStyle: 'short' }, 'ko')).toEqual('23. 6. 17.');
30 expect(formatIntlDate(date, { dateStyle: 'medium' }, 'ko')).toEqual('2023. 6. 17.');
33 it('offers four built-in time formatting options', () => {
34 const date = new Date(2023, 5, 13, 17, 44, 2);
37 // timeStyle 'full' includes a full time zone name, e.g. 15:44:02 Central European Summer Time
38 expect(formatIntlDate(date, { timeStyle: 'full' }, 'en-UK')).toContain('17:44:02');
39 // timeStyle 'long' includes a short time zone name, e.g. 15:44:02 CEST, or 15:44:02 GMT+2
40 expect(formatIntlDate(date, { timeStyle: 'long' }, 'en-UK')).toContain('17:44:02');
41 expect(formatIntlDate(date, { timeStyle: 'short' }, 'en-UK')).toEqual('17:44');
42 expect(formatIntlDate(date, { timeStyle: 'medium' }, 'en-UK')).toEqual('17:44:02');
45 expect(formatIntlDate(date, { timeStyle: 'full' }, 'es-ES')).toContain('17:44:02');
46 expect(formatIntlDate(date, { timeStyle: 'long' }, 'es-ES')).toContain('17:44:02');
47 expect(formatIntlDate(date, { timeStyle: 'short' }, 'es-ES')).toEqual('17:44');
48 expect(formatIntlDate(date, { timeStyle: 'medium' }, 'es-ES')).toEqual('17:44:02');
51 expect(formatIntlDate(date, { timeStyle: 'full' }, 'ja-JP')).toContain('17時44分02秒');
52 expect(formatIntlDate(date, { timeStyle: 'long' }, 'ja-JP')).toContain('17:44:02');
53 expect(formatIntlDate(date, { timeStyle: 'short' }, 'ja-JP')).toEqual('17:44');
54 expect(formatIntlDate(date, { timeStyle: 'medium' }, 'ja-JP')).toEqual('17:44:02');
57 it('should default to US English when locale is unknown to Javascript', () => {
58 const date = new Date(2023, 5, 17, 11, 44, 2);
60 expect(formatIntlDate(date, { dateStyle: 'full' }, 'not_a_locale')).toEqual('Saturday, June 17, 2023');
61 expect(formatIntlDate(date, { dateStyle: 'long' }, 'not_a_locale')).toEqual('June 17, 2023');
62 expect(formatIntlDate(date, { dateStyle: 'short' }, 'not_a_locale')).toEqual('6/17/23');
63 expect(formatIntlDate(date, { dateStyle: 'medium' }, 'not_a_locale')).toEqual('Jun 17, 2023');