@@ -2,6 +2,7 @@ import MockDate from 'mockdate'
22import moment from 'moment'
33import dayjs from '../../src'
44import es from '../../src/locale/es'
5+ import znCn from '../../src/locale/zh-cn'
56import localizedFormat from '../../src/plugin/localizedFormat'
67
78dayjs . extend ( localizedFormat )
@@ -17,7 +18,7 @@ afterEach(() => {
1718it ( 'Declares English localized formats' , ( ) => {
1819 expect ( dayjs . en ) . toBeDefined ( )
1920 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 =>
2122 expect ( dayjs . en . formats [ option ] ) . toBeDefined ( ) )
2223} )
2324
@@ -30,20 +31,35 @@ it('Should not interpolate characters inside square brackets', () => {
3031 expect ( actualDate . format ( 'YYYY [l] YYYY' ) ) . toBe ( '1970 l 1970' )
3132 expect ( actualDate . format ( 'l [l] l' ) ) . toBe ( '1/1/1970 l 1/1/1970' )
3233 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' )
3349} )
3450
3551it ( 'Recognizes localized format options' , ( ) => {
3652 const { formats } = dayjs . en
3753 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 =>
3955 expect ( date . format ( option ) ) . toBe ( date . format ( formats [ option ] ) ) )
4056} )
4157
4258it ( 'Uses correct English formats' , ( ) => {
4359 const date = new Date ( )
4460 const actualDate = dayjs ( date )
4561 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 =>
4763 expect ( actualDate . format ( option ) ) . toBe ( expectedDate . format ( option ) ) )
4864} )
4965
@@ -72,3 +88,21 @@ it('Uses the locale of the dayjs instance', () => {
7288 const spanishDate = dayjs ( date , { locale : es } )
7389 expect ( englishDate . format ( 'L LTS' ) ) . not . toBe ( spanishDate . format ( 'L LTS' ) )
7490} )
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