@@ -8,7 +8,7 @@ import isToday from 'date-fns/is_today';
8
8
import frLocale from 'date-fns/locale/fr' ;
9
9
import startOfToday from 'date-fns/start_of_today' ;
10
10
import { mount } from 'enzyme' ;
11
- import React from 'react' ;
11
+ import React , { useState } from 'react' ;
12
12
import sinon from 'sinon' ;
13
13
import Button from '../Button/Button' ;
14
14
import Icon from '../Icon/Icon' ;
@@ -159,11 +159,17 @@ describe('<DateInput />', () => {
159
159
} ) ;
160
160
161
161
describe ( 'date picker' , ( ) => {
162
- const callback = sinon . spy ( ) ;
163
- const component = mount ( < DateInput onChange = { callback } showOnFocus /> ) ;
162
+ let callback ;
163
+ let onCloseCallback ;
164
+ let component ;
165
+
166
+ beforeEach ( ( ) => {
167
+ callback = sinon . spy ( ) ;
168
+ onCloseCallback = sinon . spy ( ) ;
169
+ component = mount ( < DateInput onChange = { callback } onClose = { onCloseCallback } showOnFocus /> ) ;
170
+ } ) ;
164
171
165
172
it ( 'should set date after clicking a date' , ( ) => {
166
- callback . resetHistory ( ) ;
167
173
const firstDate = component . find ( 'Day' ) . first ( ) ;
168
174
const expectedDate = firstDate . props ( ) . day . date ;
169
175
firstDate . simulate ( 'click' ) ;
@@ -172,15 +178,13 @@ describe('<DateInput />', () => {
172
178
} ) ;
173
179
174
180
it ( 'should call onChange after clicking a date' , ( ) => {
175
- callback . resetHistory ( ) ;
176
181
const lastDate = component . find ( 'Day' ) . first ( ) ;
177
182
const expectedDate = lastDate . props ( ) . day . date ;
178
183
lastDate . simulate ( 'click' ) ;
179
184
assert ( callback . calledWith ( expectedDate , true ) ) ;
180
185
} ) ;
181
186
182
187
it ( 'should set date after clicking prev year' , ( ) => {
183
- callback . resetHistory ( ) ;
184
188
const expectedDate = addYears ( component . instance ( ) . getCurrentDate ( ) , - 1 ) ;
185
189
const prevYear = component . find ( 'Button.js-prev-year' ) ;
186
190
@@ -191,7 +195,6 @@ describe('<DateInput />', () => {
191
195
} ) ;
192
196
193
197
it ( 'should set date after clicking next year' , ( ) => {
194
- callback . resetHistory ( ) ;
195
198
const expectedDate = addYears ( component . instance ( ) . getCurrentDate ( ) , 1 ) ;
196
199
const nextYear = component . find ( 'Button.js-next-year' ) ;
197
200
@@ -202,7 +205,6 @@ describe('<DateInput />', () => {
202
205
} ) ;
203
206
204
207
it ( 'should set date after clicking prev month' , ( ) => {
205
- callback . resetHistory ( ) ;
206
208
const expectedDate = addMonths ( component . instance ( ) . getCurrentDate ( ) , - 1 ) ;
207
209
const prevMonth = component . find ( 'Button.js-prev-month' ) ;
208
210
@@ -213,7 +215,6 @@ describe('<DateInput />', () => {
213
215
} ) ;
214
216
215
217
it ( 'should set date after clicking next month' , ( ) => {
216
- callback . resetHistory ( ) ;
217
218
const expectedDate = addMonths ( component . instance ( ) . getCurrentDate ( ) , 1 ) ;
218
219
const nextMonth = component . find ( 'Button.js-next-month' ) ;
219
220
@@ -230,7 +231,6 @@ describe('<DateInput />', () => {
230
231
} ) ;
231
232
232
233
it ( 'should call onChange after clicking today' , ( ) => {
233
- callback . resetHistory ( ) ;
234
234
const today = component . find ( 'footer Button' ) . at ( 0 ) ;
235
235
today . simulate ( 'click' ) ;
236
236
assert ( callback . called ) ;
@@ -246,7 +246,6 @@ describe('<DateInput />', () => {
246
246
} ) ;
247
247
248
248
it ( 'should call onChange after clicking clear' , ( ) => {
249
- callback . resetHistory ( ) ;
250
249
const clear = component . find ( 'footer Button' ) . at ( 1 ) ;
251
250
clear . simulate ( 'click' ) ;
252
251
assert ( callback . calledWith ( '' , false ) ) ;
@@ -272,6 +271,110 @@ describe('<DateInput />', () => {
272
271
input . simulate ( 'keydown' , { key : 'ArrowRight' , keyCode : 39 , which : 39 } ) ;
273
272
assert ( isSameDay ( component . instance ( ) . getCurrentDate ( ) , expectedDate ) ) ;
274
273
} ) ;
274
+
275
+ it ( 'should call onClose when closing the date picker with the latest selected date in the current month' , ( ) => {
276
+ const toggle = component . find ( 'InputGroup' ) . find ( 'Button' ) ;
277
+ toggle . simulate ( 'click' ) ;
278
+
279
+ const initialDate = component . instance ( ) . getCurrentDate ( ) ;
280
+ const isFirstOfMonth = initialDate . getDate ( ) === 1 ;
281
+ const dayToPick = isFirstOfMonth ? component . find ( 'Day' ) . at ( 1 ) : component . find ( 'Day' ) . at ( 0 ) ;
282
+
283
+ const expectedDate = dayToPick . props ( ) . day . date ;
284
+
285
+ dayToPick . simulate ( 'click' ) ;
286
+ assert ( onCloseCallback . calledOnce ) ;
287
+ assert ( onCloseCallback . calledWith ( expectedDate , true ) ) ;
288
+ } ) ;
289
+
290
+ it ( 'should call onClose with the last selected date when we change the month' , ( ) => {
291
+ const toggle = component . find ( 'InputGroup' ) . find ( 'Button' ) ;
292
+ toggle . simulate ( 'click' ) ;
293
+
294
+ const nextMonth = component . find ( 'Button.js-next-month' ) ;
295
+ nextMonth . simulate ( 'click' ) ;
296
+
297
+ const tentativeIndexDayToPick = 15 ;
298
+ const tentativeDayToPick = component . find ( 'Day' ) . at ( tentativeIndexDayToPick ) ;
299
+ const dayToPick =
300
+ tentativeDayToPick . props ( ) . day . date . getDate ( ) === component . instance ( ) . getCurrentDate ( ) . getDate ( )
301
+ ? component . find ( 'Day' ) . at ( tentativeIndexDayToPick + 1 )
302
+ : tentativeDayToPick ;
303
+
304
+ const expectedDate = dayToPick . props ( ) . day . date ;
305
+
306
+ dayToPick . simulate ( 'click' ) ;
307
+ assert ( onCloseCallback . calledOnce ) ;
308
+ assert ( onCloseCallback . calledWith ( expectedDate , true ) ) ;
309
+ } ) ;
310
+ } ) ;
311
+
312
+ describe ( 'date picker with value property' , ( ) => {
313
+ let onChangeCallback ;
314
+ let onCloseCallback ;
315
+ let wrapper ;
316
+
317
+ beforeEach ( ( ) => {
318
+ onChangeCallback = sinon . spy ( ) ;
319
+ onCloseCallback = sinon . spy ( ) ;
320
+
321
+ const WrappingComponent = ( ) => {
322
+ const [ dateValue , setDateValue ] = useState ( new Date ( ) ) ;
323
+
324
+ const onChange = ( date , valid ) => {
325
+ onChangeCallback ( date , valid ) ;
326
+ setDateValue ( date ) ;
327
+ } ;
328
+
329
+ return (
330
+ < DateInput onChange = { onChange } onClose = { onCloseCallback } value = { dateValue } />
331
+ ) ;
332
+ } ;
333
+
334
+ wrapper = mount ( < WrappingComponent /> ) ;
335
+ } ) ;
336
+
337
+ it ( 'should call onClose when closing the date picker with the latest selected date in the current month' , ( ) => {
338
+ const toggle = wrapper . find ( 'DateInput' ) . find ( 'InputGroup' ) . find ( 'Button' ) ;
339
+ toggle . simulate ( 'click' ) ;
340
+
341
+ const initialDate = wrapper . find ( 'DateInput' ) . instance ( ) . getCurrentDate ( ) ;
342
+ const isFirstOfMonth = initialDate . getDate ( ) === 1 ;
343
+ const dayToPick = isFirstOfMonth
344
+ ? wrapper . find ( 'DateInput' ) . find ( 'Day' ) . at ( 1 )
345
+ : wrapper . find ( 'DateInput' ) . find ( 'Day' ) . at ( 0 ) ;
346
+
347
+ const expectedDate = dayToPick . props ( ) . day . date ;
348
+
349
+ dayToPick . simulate ( 'click' ) ;
350
+ assert ( onCloseCallback . calledOnce ) ;
351
+ assert ( onCloseCallback . calledWith ( expectedDate , true ) ) ;
352
+ } ) ;
353
+
354
+ it ( 'should call onClose with the last selected date when we change the month' , ( ) => {
355
+ const toggle = wrapper . find ( 'DateInput' ) . find ( 'InputGroup' ) . find ( 'Button' ) ;
356
+ toggle . simulate ( 'click' ) ;
357
+
358
+ const nextMonth = wrapper . find ( 'DateInput' ) . find ( 'Button.js-next-month' ) ;
359
+ nextMonth . simulate ( 'click' ) ;
360
+
361
+ const tentativeIndexDayToPick = 15 ;
362
+ const tentativeDayToPick = wrapper . find ( 'DateInput' ) . find ( 'Day' ) . at ( tentativeIndexDayToPick ) ;
363
+ const dayToPick =
364
+ tentativeDayToPick . props ( ) . day . date . getDate ( ) ===
365
+ wrapper . find ( 'DateInput' ) . instance ( ) . getCurrentDate ( ) . getDate ( )
366
+ ? wrapper
367
+ . find ( 'DateInput' )
368
+ . find ( 'Day' )
369
+ . at ( tentativeIndexDayToPick + 1 )
370
+ : tentativeDayToPick ;
371
+
372
+ const expectedDate = dayToPick . props ( ) . day . date ;
373
+
374
+ dayToPick . simulate ( 'click' ) ;
375
+ assert ( onCloseCallback . calledOnce ) ;
376
+ assert ( onCloseCallback . calledWith ( expectedDate , true ) ) ;
377
+ } ) ;
275
378
} ) ;
276
379
277
380
describe ( 'date picker with controlled visible dates' , ( ) => {
0 commit comments