-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
localizedFormat.test.js
118 lines (98 loc) · 3.61 KB
/
localizedFormat.test.js
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../../src'
import es from '../../src/locale/es'
import znCn from '../../src/locale/zh-cn'
import localizedFormat from '../../src/plugin/localizedFormat'
dayjs.extend(localizedFormat)
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
it('Declares English localized formats', () => {
expect(dayjs.en).toBeDefined()
expect(dayjs.en.formats).toBeDefined();
['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL'].forEach(option =>
expect(dayjs.en.formats[option]).toBeDefined())
})
it('Should not interpolate characters inside square brackets', () => {
const date = new Date(0)
const actualDate = dayjs(date)
const expectedDate = moment(date)
expect(actualDate.format('[l]')).toBe('l')
expect(actualDate.format('YYYY [l] YYYY')).toBe('1970 l 1970')
expect(actualDate.format('l [l] l')).toBe('1/1/1970 l 1/1/1970')
expect(actualDate.format('[L LL LLL LLLL]')).toBe(expectedDate.format('[L LL LLL LLLL]'))
const localeFormats = {
L: '[MMMM MM DD dddd]'
}
const mockedDayJsLocale = {
...es,
name: 'fake-locale',
formats: {
...localeFormats
}
}
const fakeDate = dayjs(date, { locale: mockedDayJsLocale })
expect(fakeDate.locale('fake-locale').format('l')).toEqual('MMMM MM DD dddd')
})
it('Recognizes localized format options', () => {
const { formats } = dayjs.en
const date = dayjs();
['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL'].forEach(option =>
expect(date.format(option)).toBe(date.format(formats[option])))
})
it('Uses correct English formats', () => {
const date = new Date()
const actualDate = dayjs(date)
const expectedDate = moment(date);
['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL'].forEach(option =>
expect(actualDate.format(option)).toBe(expectedDate.format(option)))
})
it('Uses English formats in other locales as default', () => {
const date = new Date()
const actualDate = dayjs(date)
const expectedDate = moment(date)
const mockLocale = {
name: 'mock',
weekdays: Array(7).fill(' '),
months: Array(12).fill(' ')
}
expect(actualDate.locale(mockLocale).format('L')).toBe(expectedDate.format('L'))
})
it('Leaves the default format intact', () => {
const date = new Date()
const actualDate = dayjs(date)
const expectedDate = moment(date)
expect(actualDate.format()).toBe(expectedDate.format())
})
it('Uses the locale of the dayjs instance', () => {
const date = new Date()
const englishDate = dayjs(date)
const spanishDate = dayjs(date, { locale: es })
expect(englishDate.format('L LTS')).not.toBe(spanishDate.format('L LTS'))
})
it('Uses the localized lowercase formats if defined', () => {
const date = new Date()
const znDate = dayjs(date, { locale: znCn });
['l', 'll', 'lll', 'llll'].forEach(option => expect(znDate.format(option)).toBe(znDate.format(znCn.formats[option])))
})
it('Uses fallback to xx if xx-yy not available', () => {
expect(dayjs('2019-02-01').locale('en-yy').format('MMMM'))
.toBe('February')
})
it('Uses xx-yy if xx-YY is provided', () => {
expect(dayjs('2019-02-01').locale('es-US').format('MMMM'))
.toBe('febrero')
})
it('Uses the localized uppercase formats as a base for lowercase formats, if not defined', () => {
const date = new Date()
const spanishDate = dayjs(date, { locale: es });
['l', 'll', 'lll', 'llll'].forEach((option) => {
const upperCaseFormat = es.formats[option.toUpperCase()]
const adaptedFormat = upperCaseFormat.replace(/(\[[^\]]+])|(MMMM|MM|DD|dddd)/g, (_, a, b) => a || b.slice(1))
expect(spanishDate.format(option)).toBe(spanishDate.format(adaptedFormat))
})
})