@@ -2,6 +2,7 @@ import MockDate from 'mockdate'
2
2
import moment from 'moment'
3
3
import dayjs from '../../src'
4
4
import es from '../../src/locale/es'
5
+ import znCn from '../../src/locale/zh-cn'
5
6
import localizedFormat from '../../src/plugin/localizedFormat'
6
7
7
8
dayjs . extend ( localizedFormat )
@@ -17,7 +18,7 @@ afterEach(() => {
17
18
it ( 'Declares English localized formats' , ( ) => {
18
19
expect ( dayjs . en ) . toBeDefined ( )
19
20
expect ( dayjs . en . formats ) . toBeDefined ( ) ;
20
- [ 'LT' , 'LTS' , 'L' , 'LL' , 'LLL' , 'LLLL' , 'l' , 'll' , 'lll' , 'llll' ] . forEach ( option =>
21
+ [ 'LT' , 'LTS' , 'L' , 'LL' , 'LLL' , 'LLLL' ] . forEach ( option =>
21
22
expect ( dayjs . en . formats [ option ] ) . toBeDefined ( ) )
22
23
} )
23
24
@@ -30,20 +31,35 @@ it('Should not interpolate characters inside square brackets', () => {
30
31
expect ( actualDate . format ( 'YYYY [l] YYYY' ) ) . toBe ( '1970 l 1970' )
31
32
expect ( actualDate . format ( 'l [l] l' ) ) . toBe ( '1/1/1970 l 1/1/1970' )
32
33
expect ( actualDate . format ( '[L LL LLL LLLL]' ) ) . toBe ( expectedDate . format ( '[L LL LLL LLLL]' ) )
34
+
35
+
36
+ const localeFormats = {
37
+ L : '[MMMM MM DD dddd]'
38
+ }
39
+ const mockedDayJsLocale = {
40
+ ...es ,
41
+ name : 'fake-locale' ,
42
+ formats : {
43
+ ...localeFormats
44
+ }
45
+ }
46
+ const fakeDate = dayjs ( date , { locale : mockedDayJsLocale } )
47
+
48
+ expect ( fakeDate . locale ( 'fake-locale' ) . format ( 'l' ) ) . toEqual ( 'MMMM MM DD dddd' )
33
49
} )
34
50
35
51
it ( 'Recognizes localized format options' , ( ) => {
36
52
const { formats } = dayjs . en
37
53
const date = dayjs ( ) ;
38
- [ 'LT' , 'LTS' , 'L' , 'LL' , 'LLL' , 'LLLL' , 'l' , 'll' , 'lll' , 'llll' ] . forEach ( option =>
54
+ [ 'LT' , 'LTS' , 'L' , 'LL' , 'LLL' , 'LLLL' ] . forEach ( option =>
39
55
expect ( date . format ( option ) ) . toBe ( date . format ( formats [ option ] ) ) )
40
56
} )
41
57
42
58
it ( 'Uses correct English formats' , ( ) => {
43
59
const date = new Date ( )
44
60
const actualDate = dayjs ( date )
45
61
const expectedDate = moment ( date ) ;
46
- [ 'LT' , 'LTS' , 'L' , 'LL' , 'LLL' , 'LLLL' , 'l' , 'll' , 'lll' , 'llll' ] . forEach ( option =>
62
+ [ 'LT' , 'LTS' , 'L' , 'LL' , 'LLL' , 'LLLL' ] . forEach ( option =>
47
63
expect ( actualDate . format ( option ) ) . toBe ( expectedDate . format ( option ) ) )
48
64
} )
49
65
@@ -72,3 +88,21 @@ it('Uses the locale of the dayjs instance', () => {
72
88
const spanishDate = dayjs ( date , { locale : es } )
73
89
expect ( englishDate . format ( 'L LTS' ) ) . not . toBe ( spanishDate . format ( 'L LTS' ) )
74
90
} )
91
+
92
+
93
+ it ( 'Uses the localized lowercase formats if defined' , ( ) => {
94
+ const date = new Date ( )
95
+ const znDate = dayjs ( date , { locale : znCn } ) ;
96
+ [ 'l' , 'll' , 'lll' , 'llll' ] . forEach ( option => expect ( znDate . format ( option ) ) . toBe ( znDate . format ( znCn . formats [ option ] ) ) )
97
+ } )
98
+
99
+ it ( 'Uses the localized uppercase formats as a base for lowercase formats, if not defined' , ( ) => {
100
+ const date = new Date ( )
101
+ const spanishDate = dayjs ( date , { locale : es } ) ;
102
+
103
+ [ 'l' , 'll' , 'lll' , 'llll' ] . forEach ( ( option ) => {
104
+ const upperCaseFormat = es . formats [ option . toUpperCase ( ) ]
105
+ const adaptedFormat = upperCaseFormat . replace ( / ( \[ [ ^ \] ] + ] ) | ( M M M M | M M | D D | d d d d ) / g, ( _ , a , b ) => a || b . slice ( 1 ) )
106
+ expect ( spanishDate . format ( option ) ) . toBe ( spanishDate . format ( adaptedFormat ) )
107
+ } )
108
+ } )
0 commit comments