Skip to content

Commit deca168

Browse files
committed
Test formatDate and formatTime
1 parent 224b7ea commit deca168

1 file changed

Lines changed: 42 additions & 1 deletion

File tree

src/locale/useLocale.test.tsx

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import { afterEach, expect, test } from 'vitest';
2+
import { Temporal } from '@js-temporal/polyfill';
23
import { cleanup } from 'solid-testing-library';
34
import {
45
ColorScheme,
56
createLocalePrimitive,
67
formatNumber,
8+
formatDate,
9+
formatTime,
710
useLocale
811
} from '..';
9-
import { NumberFormat } from 'user-locale';
12+
import { DateEndianness, NumberFormat } from 'user-locale';
1013

1114
afterEach(cleanup);
1215

@@ -161,3 +164,41 @@ test('format number', () => {
161164

162165
expect(formatNumber(1000.01, { useGrouping: false })).toBe('1000,01');
163166
});
167+
168+
test('format date', () => {
169+
createLocalePrimitive({
170+
initialValues: {
171+
dateFormat: { endianness: DateEndianness.LittleEndian }
172+
},
173+
supportedLanguageTags: ['en']
174+
});
175+
176+
const [locale, { setDateFormat }] = useLocale();
177+
178+
expect(locale.dateFormat).toEqual({ endianness: DateEndianness.LittleEndian });
179+
expect(formatDate(Temporal.PlainDate.from('2000-12-31'))).toBe('31/12/2000');
180+
181+
setDateFormat({ endianness: DateEndianness.MiddleEndian });
182+
183+
expect(locale.dateFormat).toEqual({ endianness: DateEndianness.MiddleEndian });
184+
expect(formatDate(Temporal.PlainDate.from('2000-12-31'))).toBe('12/31/2000');
185+
});
186+
187+
test('format time', () => {
188+
createLocalePrimitive({
189+
initialValues: {
190+
timeFormat: { is24HourClock: true }
191+
},
192+
supportedLanguageTags: ['en']
193+
});
194+
195+
const [locale, { setTimeFormat }] = useLocale();
196+
197+
expect(locale.timeFormat).toEqual({ is24HourClock: true });
198+
expect(formatTime(Temporal.PlainTime.from('00:00:05'), { precision: 'minute', omitZeroUnits: true })).toBe('00:00');
199+
200+
setTimeFormat({ is24HourClock: false });
201+
202+
expect(locale.timeFormat).toEqual({ is24HourClock: false });
203+
expect(formatTime(Temporal.PlainTime.from('00:00:05'), { precision: 'minute', omitZeroUnits: true })).toBe('12 AM');
204+
});

0 commit comments

Comments
 (0)