@@ -2,6 +2,7 @@ import MockDate from 'mockdate'
2
2
import moment from 'moment'
3
3
import dayjs from '../../src'
4
4
import customParseFormat from '../../src/plugin/customParseFormat'
5
+ import uk from '../../src/locale/uk'
5
6
6
7
dayjs . extend ( customParseFormat )
7
8
@@ -84,3 +85,56 @@ it('fails with an invalid format', () => {
84
85
expect ( dayjs ( input , format ) . format ( ) . toLowerCase ( ) )
85
86
. toBe ( moment ( input , format ) . format ( ) . toLowerCase ( ) )
86
87
} )
88
+
89
+ it ( 'parse month from string' , ( ) => {
90
+ const input = '2018 February 03'
91
+ const format = 'YYYY MMMM DD'
92
+ expect ( dayjs ( input , format ) . valueOf ( ) ) . toBe ( moment ( input , format ) . valueOf ( ) )
93
+ } )
94
+
95
+ it ( 'parse month from short string' , ( ) => {
96
+ const input = '2018 Feb 03'
97
+ const format = 'YYYY MMM DD'
98
+ expect ( dayjs ( input , format ) . valueOf ( ) ) . toBe ( moment ( input , format ) . valueOf ( ) )
99
+ } )
100
+
101
+ it ( 'parse month from string with locale in config' , ( ) => {
102
+ const input = '2018 лютий 03'
103
+ const format = 'YYYY MMMM DD'
104
+
105
+ expect ( dayjs ( input , { format, locale : uk } ) . valueOf ( ) ) . toBe ( moment ( input , format , 'uk' ) . valueOf ( ) )
106
+ } )
107
+
108
+ it ( 'parse month from short string with locale in config' , ( ) => {
109
+ const input = '2018 трав 03'
110
+ const format = 'YYYY MMM DD'
111
+ expect ( dayjs ( input , { format, locale : uk } ) . valueOf ( ) ) . toBe ( moment ( input , format , 'uk' ) . valueOf ( ) )
112
+ } )
113
+
114
+ it ( 'return Invalid Date when parse corrupt string' , ( ) => {
115
+ const input = '2018 Turnip 03'
116
+ const format = 'YYYY MMMM DD'
117
+ expect ( dayjs ( input , format ) . format ( ) ) . toBe ( 'Invalid Date' )
118
+ } )
119
+
120
+ it ( 'return Invalid Date when parse corrupt short string' , ( ) => {
121
+ const input = '2018 Dog 03'
122
+ const format = 'YYYY MMM DD'
123
+ expect ( dayjs ( input , format ) . format ( ) ) . toBe ( 'Invalid Date' )
124
+ } )
125
+
126
+ it ( 'correctly parse month from string after changing locale globally' , ( ) => {
127
+ const input = '2018 лютий 03'
128
+ const format = 'YYYY MMMM DD'
129
+
130
+ const dayjsLocale = dayjs ( ) . $locale ( )
131
+ const momentLocale = moment . locale ( )
132
+ try {
133
+ dayjs . locale ( uk )
134
+ moment . locale ( 'uk' )
135
+ expect ( dayjs ( input , format ) . valueOf ( ) ) . toBe ( moment ( input , format ) . valueOf ( ) )
136
+ } finally {
137
+ dayjs . locale ( dayjsLocale )
138
+ moment . locale ( momentLocale )
139
+ }
140
+ } )
0 commit comments