1
1
import React from 'react'
2
- import moment from 'moment'
3
2
import Calendar from '../src/calendar'
4
3
import Month from '../src/month'
5
4
import Day from '../src/day'
6
5
import YearDropdown from '../src/year_dropdown'
7
6
import MonthDropdown from '../src/month_dropdown'
8
7
import { shallow , mount } from 'enzyme'
9
8
import sinon from 'sinon'
9
+ import * as utils from '../src/date_utils'
10
+
11
+ // TODO Possibly rename
12
+ const DATE_FORMAT = 'MM/DD/YYYY'
10
13
11
14
describe ( 'Calendar' , function ( ) {
12
15
const dateFormat = 'MMMM YYYY'
@@ -24,105 +27,105 @@ describe('Calendar', function () {
24
27
}
25
28
26
29
it ( 'should start with the current date in view if no date range' , function ( ) {
27
- const now = moment ( )
30
+ const now = utils . newDate ( )
28
31
const calendar = getCalendar ( )
29
- assert ( calendar . state ( ) . date . isSame ( now , 'day' ) )
32
+ assert ( utils . isSameDay ( calendar . state ( ) . date , now ) )
30
33
} )
31
34
32
35
it ( 'should start with the today date with specified time zone' , function ( ) {
33
36
const utcOffset = 12
34
37
const calendar = getCalendar ( { utcOffset} )
35
- assert ( calendar . state ( ) . date . isSame ( moment . utc ( ) . utcOffset ( utcOffset ) , 'day' ) )
38
+ assert ( utils . isSameDay ( calendar . state ( ) . date , utils . newDateWithOffset ( utcOffset ) ) )
36
39
} )
37
40
38
41
it ( 'should start with the selected date in view if provided' , function ( ) {
39
- const selected = moment ( ) . add ( 1 , 'year' )
42
+ const selected = utils . addYears ( utils . newDate ( ) , 1 )
40
43
const calendar = getCalendar ( { selected} )
41
- assert ( calendar . state ( ) . date . isSame ( selected , 'day' ) )
44
+ assert ( utils . isSameDay ( calendar . state ( ) . date , selected ) )
42
45
} )
43
46
44
47
it ( 'should start with the pre-selected date in view if provided' , function ( ) {
45
- const preSelected = moment ( ) . add ( 2 , 'year' )
46
- const selected = moment ( ) . add ( 1 , 'year' )
48
+ const preSelected = utils . addYears ( utils . newDate ( ) , 2 )
49
+ const selected = utils . addYears ( utils . newDate ( ) , 1 )
47
50
const calendar = getCalendar ( { preSelected, selected } )
48
- assert ( calendar . state ( ) . date . isSame ( selected , 'day' ) )
51
+ assert ( utils . isSameDay ( calendar . state ( ) . date , selected ) )
49
52
} )
50
53
51
54
it ( 'should start with the current date in view if in date range' , function ( ) {
52
- const now = moment ( )
53
- const minDate = now . clone ( ) . subtract ( 1 , 'year' )
54
- const maxDate = now . clone ( ) . add ( 1 , 'year' )
55
+ const now = utils . newDate ( )
56
+ const minDate = utils . subtractYears ( utils . cloneDate ( now ) , 1 )
57
+ const maxDate = utils . addYears ( utils . cloneDate ( now ) , 1 )
55
58
const calendar = getCalendar ( { minDate, maxDate } )
56
- assert ( calendar . state ( ) . date . isSame ( now , 'day' ) )
59
+ assert ( utils . isSameDay ( calendar . state ( ) . date , now ) )
57
60
} )
58
61
59
62
it ( 'should start with the min date in view if after the current date' , function ( ) {
60
- const minDate = moment ( ) . add ( 1 , 'year' )
63
+ const minDate = utils . addYears ( utils . newDate ( ) , 1 )
61
64
const calendar = getCalendar ( { minDate } )
62
- assert ( calendar . state ( ) . date . isSame ( minDate , 'day' ) )
65
+ assert ( utils . isSameDay ( calendar . state ( ) . date , minDate ) )
63
66
} )
64
67
65
68
it ( 'should start with the min include date in view if after the current date' , function ( ) {
66
- const minDate = moment ( ) . add ( 1 , 'year' )
69
+ const minDate = utils . addYears ( utils . newDate ( ) , 1 )
67
70
const calendar = getCalendar ( { includeDates : [ minDate ] } )
68
- assert ( calendar . state ( ) . date . isSame ( minDate , 'day' ) )
71
+ assert ( utils . isSameDay ( calendar . state ( ) . date , minDate ) )
69
72
} )
70
73
71
74
it ( 'should start with the max date in view if before the current date' , function ( ) {
72
- const maxDate = moment ( ) . subtract ( 1 , 'year' )
75
+ const maxDate = utils . subtractYears ( utils . newDate ( ) , 1 )
73
76
const calendar = getCalendar ( { maxDate } )
74
- assert ( calendar . state ( ) . date . isSame ( maxDate , 'day' ) )
77
+ assert ( utils . isSameDay ( calendar . state ( ) . date , maxDate ) )
75
78
} )
76
79
77
80
it ( 'should start with the max include date in view if before the current date' , function ( ) {
78
- const maxDate = moment ( ) . subtract ( 1 , 'year' )
81
+ const maxDate = utils . subtractYears ( utils . newDate ( ) , 1 )
79
82
const calendar = getCalendar ( { includeDates : [ maxDate ] } )
80
- assert ( calendar . state ( ) . date . isSame ( maxDate , 'day' ) )
83
+ assert ( utils . isSameDay ( calendar . state ( ) . date , maxDate ) )
81
84
} )
82
85
83
86
it ( 'should start with the open to date in view if given and no selected/min/max dates given' , function ( ) {
84
- const openToDate = moment ( '09/28/1993' , 'MM/DD/YYYY' )
87
+ const openToDate = utils . parseDate ( '09/28/1993' , { dateFormat : DATE_FORMAT } )
85
88
const calendar = getCalendar ( { openToDate } )
86
- assert ( calendar . state ( ) . date . isSame ( openToDate , 'day' ) )
89
+ assert ( utils . isSameDay ( calendar . state ( ) . date , openToDate ) )
87
90
} )
88
91
89
92
it ( 'should start with the open to date in view if given and after a min date' , function ( ) {
90
- const openToDate = moment ( '09/28/1993' , 'MM/DD/YYYY' )
91
- const minDate = moment ( '01/01/1993' , 'MM/DD/YYYY' )
93
+ const openToDate = utils . parseDate ( '09/28/1993' , { dateFormat : DATE_FORMAT } )
94
+ const minDate = utils . parseDate ( '01/01/1993' , { dateFormat : DATE_FORMAT } )
92
95
const calendar = getCalendar ( { openToDate, minDate } )
93
- assert ( calendar . state ( ) . date . isSame ( openToDate , 'day' ) )
96
+ assert ( utils . isSameDay ( calendar . state ( ) . date , openToDate ) )
94
97
} )
95
98
96
99
it ( 'should start with the open to date in view if given and before a max date' , function ( ) {
97
- const openToDate = moment ( '09/28/1993' , 'MM/DD/YYYY' )
98
- const maxDate = moment ( '12/31/1993' , 'MM/DD/YYYY' )
100
+ const openToDate = utils . parseDate ( '09/28/1993' , { dateFormat : DATE_FORMAT } )
101
+ const maxDate = utils . parseDate ( '12/31/1993' , { dateFormat : DATE_FORMAT } )
99
102
const calendar = getCalendar ( { openToDate, maxDate } )
100
- assert ( calendar . state ( ) . date . isSame ( openToDate , 'day' ) )
103
+ assert ( utils . isSameDay ( calendar . state ( ) . date , openToDate ) )
101
104
} )
102
105
103
106
it ( 'should start with the open to date in view if given and in range of the min/max dates' , function ( ) {
104
- const openToDate = moment ( '09/28/1993' , 'MM/DD/YYYY' )
105
- const minDate = moment ( '01/01/1993' , 'MM/DD/YYYY' )
106
- const maxDate = moment ( '12/31/1993' , 'MM/DD/YYYY' )
107
+ const openToDate = utils . parseDate ( '09/28/1993' , { dateFormat : DATE_FORMAT } )
108
+ const minDate = utils . parseDate ( '01/01/1993' , { dateFormat : DATE_FORMAT } )
109
+ const maxDate = utils . parseDate ( '12/31/1993' , { dateFormat : DATE_FORMAT } )
107
110
const calendar = getCalendar ( { openToDate, minDate, maxDate } )
108
- assert ( calendar . state ( ) . date . isSame ( openToDate , 'day' ) )
111
+ assert ( utils . isSameDay ( calendar . state ( ) . date , openToDate ) )
109
112
} )
110
113
111
114
it ( 'should open on openToDate date rather than selected date when both are specified' , function ( ) {
112
- var openToDate = moment ( '09/28/1993' , 'MM/DD/YYYY' )
113
- var selected = moment ( '09/28/1995' , 'MM/DD/YYYY' )
115
+ var openToDate = utils . parseDate ( '09/28/1993' , { dateFormat : DATE_FORMAT } )
116
+ var selected = utils . parseDate ( '09/28/1995' , { dateFormat : DATE_FORMAT } )
114
117
var calendar = getCalendar ( { openToDate, selected } )
115
- assert ( calendar . state ( ) . date . isSame ( openToDate , 'day' ) )
118
+ assert ( utils . isSameDay ( calendar . state ( ) . date , openToDate ) )
116
119
} )
117
120
118
121
it ( 'should trigger date change when openToDate prop is set after calcInitialState()' , ( ) => {
119
- const openToDate = moment ( '09/28/1993' , 'MM/DD/YYYY' )
120
- const oneMonthFromOpenToDate = moment ( '10/28/1993' , 'MM/DD/YYYY' )
122
+ const openToDate = utils . parseDate ( '09/28/1993' , { dateFormat : DATE_FORMAT } )
123
+ const oneMonthFromOpenToDate = utils . parseDate ( '10/28/1993' , { dateFormat : DATE_FORMAT } )
121
124
const calendar = getCalendar ( { openToDate } )
122
125
123
- assert ( calendar . state ( ) . date . isSame ( openToDate , 'day' ) )
126
+ assert ( utils . isSameDay ( calendar . state ( ) . date , openToDate ) )
124
127
calendar . setProps ( { openToDate : oneMonthFromOpenToDate } )
125
- assert ( calendar . state ( ) . date . isSame ( oneMonthFromOpenToDate ) )
128
+ assert ( utils . isSameDay ( calendar . state ( ) . date , oneMonthFromOpenToDate ) )
126
129
} )
127
130
128
131
it ( 'should not show the year dropdown menu by default' , function ( ) {
@@ -138,7 +141,7 @@ describe('Calendar', function () {
138
141
} )
139
142
140
143
it ( 'should show month navigation if toggled on' , function ( ) {
141
- const calendar = getCalendar ( { includeDates : [ moment ( ) ] , forceShowMonthNavigation : true } )
144
+ const calendar = getCalendar ( { includeDates : [ utils . newDate ( ) ] , forceShowMonthNavigation : true } )
142
145
const nextNavigationButton = calendar . find ( '.react-datepicker__navigation--next' )
143
146
expect ( nextNavigationButton ) . to . have . length ( 1 )
144
147
} )
@@ -172,15 +175,15 @@ describe('Calendar', function () {
172
175
const calendar = getCalendar ( { todayButton : 'Vandaag' } )
173
176
const todayButton = calendar . find ( '.react-datepicker__today-button' )
174
177
todayButton . simulate ( 'click' )
175
- expect ( calendar . state ( ) . date . isSame ( moment ( ) , 'day' ) )
178
+ expect ( calendar . state ( ) . date . isSame ( utils . newDate ( ) , 'day' ) )
176
179
} )
177
180
178
181
it ( 'should set custom today date when pressing todayButton' , ( ) => {
179
- const todayInAuckland = moment . utc ( ) . utcOffset ( 12 )
182
+ const todayInAuckland = utils . newDateWithOffset ( 12 )
180
183
const calendar = getCalendar ( { todayButton : 'Vandaag' , utcOffset : 12 } )
181
184
const todayButton = calendar . find ( '.react-datepicker__today-button' )
182
185
todayButton . simulate ( 'click' )
183
- expect ( calendar . state ( ) . date . isSame ( todayInAuckland , 'day' ) )
186
+ expect ( utils . isSameDay ( calendar . state ( ) . date , todayInAuckland ) )
184
187
} )
185
188
186
189
it ( 'should use a hash for week label if weekLabel is NOT provided' , ( ) => {
@@ -207,7 +210,7 @@ describe('Calendar', function () {
207
210
const month = calendar . find ( Month ) . first ( )
208
211
day . simulate ( 'mouseenter' )
209
212
expect ( month . prop ( 'selectingDate' ) ) . to . exist
210
- expect ( month . prop ( 'selectingDate' ) . isSame ( day . prop ( 'day' ) , 'day' ) ) . to . be . true
213
+ expect ( utils . isSameDay ( month . prop ( 'selectingDate' ) , day . prop ( 'day' ) ) ) . to . be . true
211
214
} )
212
215
213
216
it ( 'should clear the hovered day when the mouse leaves' , ( ) => {
@@ -218,15 +221,15 @@ describe('Calendar', function () {
218
221
onClickOutside = { ( ) => { } }
219
222
onSelect = { ( ) => { } } />
220
223
)
221
- calendar . setState ( { selectingDate : moment ( ) } )
224
+ calendar . setState ( { selectingDate : utils . newDate ( ) } )
222
225
const month = calendar . find ( Month ) . first ( )
223
226
expect ( month . prop ( 'selectingDate' ) ) . to . exist
224
227
month . simulate ( 'mouseleave' )
225
228
expect ( month . prop ( 'selectingDate' ) ) . not . to . exist
226
229
} )
227
230
228
- it ( 'uses MomentJS\'s weekdaysShort instead of weekdaysMin provided useWeekdaysShort prop is present' , ( ) => {
229
- moment . defineLocale ( 'weekDaysLocale' , {
231
+ it ( 'uses weekdaysShort instead of weekdaysMin provided useWeekdaysShort prop is present' , ( ) => {
232
+ utils . registerLocale ( 'weekDaysLocale' , {
230
233
parentLocale : 'en' ,
231
234
weekdaysMin : 'AA_BB_CC_DD_EE_FF_GG' . split ( '_' ) ,
232
235
weekdaysShort : 'AAA_BBB_CCC_DDD_EEE_FFF_GGG' . split ( '_' )
@@ -333,39 +336,39 @@ describe('Calendar', function () {
333
336
334
337
describe ( 'localization' , function ( ) {
335
338
function testLocale ( calendar , selected , locale ) {
336
- const localized = selected . clone ( ) . locale ( locale )
339
+ const localized = utils . localizeDate ( selected , locale )
337
340
338
341
const calendarText = calendar . find ( '.react-datepicker__current-month' )
339
- expect ( calendarText . text ( ) ) . to . equal ( localized . format ( dateFormat ) )
342
+ expect ( calendarText . text ( ) ) . to . equal ( utils . formatDate ( localized , dateFormat ) )
340
343
341
- const firstDateOfWeek = localized . clone ( ) . startOf ( 'week' )
342
- const firstWeekDayMin = firstDateOfWeek . localeData ( ) . weekdaysMin ( firstDateOfWeek )
344
+ const firstDateOfWeek = utils . getStartOfWeek ( utils . cloneDate ( localized ) )
345
+ const firstWeekDayMin = utils . getWeekdayMinInLocale ( utils . getLocaleData ( firstDateOfWeek ) , firstDateOfWeek )
343
346
const firstHeader = calendar . find ( '.react-datepicker__day-name' ) . at ( 0 )
344
347
expect ( firstHeader . text ( ) ) . to . equal ( firstWeekDayMin )
345
348
}
346
349
347
350
it ( 'should use the globally-defined locale by default' , function ( ) {
348
- const selected = moment ( )
351
+ const selected = utils . newDate ( )
349
352
const calendar = getCalendar ( { selected } )
350
- testLocale ( calendar , selected , moment . locale ( ) )
353
+ testLocale ( calendar , selected , utils . getDefaultLocale ( ) )
351
354
} )
352
355
353
356
it ( 'should use the locale specified as a prop' , function ( ) {
354
357
const locale = 'fr'
355
- const selected = moment ( ) . locale ( locale )
358
+ const selected = utils . localizeDate ( utils . newDate ( ) , locale )
356
359
const calendar = getCalendar ( { selected, locale } )
357
360
testLocale ( calendar , selected , locale )
358
361
} )
359
362
360
363
it ( 'should override the locale of the date with the globally-defined locale' , function ( ) {
361
- const selected = moment ( ) . locale ( 'fr' )
364
+ const selected = utils . localizeDate ( utils . newDate ( ) , 'fr' )
362
365
const calendar = getCalendar ( { selected } )
363
- testLocale ( calendar , selected , moment . locale ( ) )
366
+ testLocale ( calendar , selected , utils . getDefaultLocale ( ) )
364
367
} )
365
368
366
369
it ( 'should override the locale of the date with the locale prop' , function ( ) {
367
370
const locale = 'fr'
368
- const selected = moment ( )
371
+ const selected = utils . newDate ( )
369
372
const calendar = getCalendar ( { selected, locale } )
370
373
testLocale ( calendar , selected , locale )
371
374
} )
0 commit comments