|
1 | 1 | import { afterEach, expect, test } from 'vitest'; |
| 2 | +import { Temporal } from '@js-temporal/polyfill'; |
2 | 3 | import { cleanup } from 'solid-testing-library'; |
3 | 4 | import { |
4 | 5 | ColorScheme, |
5 | 6 | createLocalePrimitive, |
6 | 7 | formatNumber, |
| 8 | + formatDate, |
| 9 | + formatTime, |
7 | 10 | useLocale |
8 | 11 | } from '..'; |
9 | | -import { NumberFormat } from 'user-locale'; |
| 12 | +import { DateEndianness, NumberFormat } from 'user-locale'; |
10 | 13 |
|
11 | 14 | afterEach(cleanup); |
12 | 15 |
|
@@ -161,3 +164,41 @@ test('format number', () => { |
161 | 164 |
|
162 | 165 | expect(formatNumber(1000.01, { useGrouping: false })).toBe('1000,01'); |
163 | 166 | }); |
| 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