-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
updateLocale.test.js
82 lines (69 loc) · 2.23 KB
/
updateLocale.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
import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../../src'
import updateLocale from '../../src/plugin/updateLocale'
import localizedFormat from '../../src/plugin/localizedFormat'
import '../../src/locale/zh-cn'
dayjs.extend(updateLocale)
dayjs.extend(localizedFormat)
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
const newLocale = {
months: new Array(12).fill('testMonth'),
formats: { // formats for dayjs and longDateFormat for momentjs
LT: '[testFormat]'
},
longDateFormat: {
LT: '[testFormat]'
}
}
const formatString = 'MMMM LT'
describe('Update locale', () => {
it('Invalid argument', () => {
const result = dayjs.updateLocale('InvalidLocaleName', {})
expect(result)
.toEqual(undefined)
expect(dayjs().format(formatString))
.toEqual(moment().format(formatString))
})
it('Return value', () => {
const result1 = dayjs.updateLocale('en')
expect(typeof result1).toEqual('object')
const result2 = dayjs.updateLocale('en', {})
expect(typeof result2).toEqual('object')
const result3 = dayjs.updateLocale('en', newLocale)
expect(typeof result3).toEqual('object')
})
it('Update build-in en locale', () => {
moment.updateLocale('en', newLocale)
dayjs.updateLocale('en', newLocale)
expect(dayjs().format(formatString))
.toEqual('testMonth testFormat')
expect(dayjs().format(formatString))
.toEqual(moment().format(formatString))
})
it('Update imported zh-cn locale', () => {
moment.updateLocale('zh-cn', newLocale)
dayjs.updateLocale('zh-cn', newLocale)
dayjs.locale('zh-cn')
moment.locale('zh-cn')
expect(dayjs().format(formatString))
.toEqual('testMonth testFormat')
expect(dayjs().format(formatString))
.toEqual(moment().format(formatString))
})
it('Update invalid date string', () => {
const locale = 'en'
const localeSetting = { invalidDate: 'bad date' }
dayjs.updateLocale(locale, localeSetting)
moment.updateLocale(locale, localeSetting)
dayjs.locale(locale)
moment.locale(locale)
expect(dayjs('').format()).toBe(moment('').format())
expect(dayjs('otherString').format()).toBe(moment('otherString').format())
})
})