-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathlocale.test.ts
More file actions
49 lines (42 loc) · 1.25 KB
/
locale.test.ts
File metadata and controls
49 lines (42 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { describe, expect, it } from 'vitest'
import {
formatDateTime,
formatTime,
getPreferredLocale,
sanitizeLocale,
} from '../utils'
describe('locale utils', () => {
it('falls back to en-US when navigator.language is an invalid string', () => {
expect(
getPreferredLocale({
language: 'undefined',
userLanguage: 'undefined',
}),
).toBe('en-US')
})
it('keeps valid locale tags unchanged', () => {
expect(
getPreferredLocale({
language: 'fr-FR',
}),
).toBe('fr-FR')
})
it('sanitizes invalid locales before formatting dates', () => {
const value = new Date('2024-01-02T03:04:05.000Z')
expect(formatDateTime(value, 'undefined')).toBe(
value.toLocaleString('en-US'),
)
expect(() => formatDateTime(value, 'undefined')).not.toThrow()
})
it('sanitizes invalid locales before formatting times', () => {
const value = new Date('2024-01-02T03:04:05.000Z')
expect(formatTime(value, 'undefined')).toBe(
value.toLocaleTimeString('en-US'),
)
expect(() => formatTime(value, 'undefined')).not.toThrow()
})
it('falls back for empty locale values', () => {
expect(sanitizeLocale('')).toBe('en-US')
expect(sanitizeLocale(' ')).toBe('en-US')
})
})