@@ -8,7 +8,7 @@ import isToday from 'date-fns/is_today';
88import frLocale from 'date-fns/locale/fr' ;
99import startOfToday from 'date-fns/start_of_today' ;
1010import { mount } from 'enzyme' ;
11- import React from 'react' ;
11+ import React , { useState } from 'react' ;
1212import sinon from 'sinon' ;
1313import Button from '../Button/Button' ;
1414import Icon from '../Icon/Icon' ;
@@ -159,11 +159,17 @@ describe('<DateInput />', () => {
159159 } ) ;
160160
161161 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+ } ) ;
164171
165172 it ( 'should set date after clicking a date' , ( ) => {
166- callback . resetHistory ( ) ;
167173 const firstDate = component . find ( 'Day' ) . first ( ) ;
168174 const expectedDate = firstDate . props ( ) . day . date ;
169175 firstDate . simulate ( 'click' ) ;
@@ -172,15 +178,13 @@ describe('<DateInput />', () => {
172178 } ) ;
173179
174180 it ( 'should call onChange after clicking a date' , ( ) => {
175- callback . resetHistory ( ) ;
176181 const lastDate = component . find ( 'Day' ) . first ( ) ;
177182 const expectedDate = lastDate . props ( ) . day . date ;
178183 lastDate . simulate ( 'click' ) ;
179184 assert ( callback . calledWith ( expectedDate , true ) ) ;
180185 } ) ;
181186
182187 it ( 'should set date after clicking prev year' , ( ) => {
183- callback . resetHistory ( ) ;
184188 const expectedDate = addYears ( component . instance ( ) . getCurrentDate ( ) , - 1 ) ;
185189 const prevYear = component . find ( 'Button.js-prev-year' ) ;
186190
@@ -191,7 +195,6 @@ describe('<DateInput />', () => {
191195 } ) ;
192196
193197 it ( 'should set date after clicking next year' , ( ) => {
194- callback . resetHistory ( ) ;
195198 const expectedDate = addYears ( component . instance ( ) . getCurrentDate ( ) , 1 ) ;
196199 const nextYear = component . find ( 'Button.js-next-year' ) ;
197200
@@ -202,7 +205,6 @@ describe('<DateInput />', () => {
202205 } ) ;
203206
204207 it ( 'should set date after clicking prev month' , ( ) => {
205- callback . resetHistory ( ) ;
206208 const expectedDate = addMonths ( component . instance ( ) . getCurrentDate ( ) , - 1 ) ;
207209 const prevMonth = component . find ( 'Button.js-prev-month' ) ;
208210
@@ -213,7 +215,6 @@ describe('<DateInput />', () => {
213215 } ) ;
214216
215217 it ( 'should set date after clicking next month' , ( ) => {
216- callback . resetHistory ( ) ;
217218 const expectedDate = addMonths ( component . instance ( ) . getCurrentDate ( ) , 1 ) ;
218219 const nextMonth = component . find ( 'Button.js-next-month' ) ;
219220
@@ -230,7 +231,6 @@ describe('<DateInput />', () => {
230231 } ) ;
231232
232233 it ( 'should call onChange after clicking today' , ( ) => {
233- callback . resetHistory ( ) ;
234234 const today = component . find ( 'footer Button' ) . at ( 0 ) ;
235235 today . simulate ( 'click' ) ;
236236 assert ( callback . called ) ;
@@ -246,7 +246,6 @@ describe('<DateInput />', () => {
246246 } ) ;
247247
248248 it ( 'should call onChange after clicking clear' , ( ) => {
249- callback . resetHistory ( ) ;
250249 const clear = component . find ( 'footer Button' ) . at ( 1 ) ;
251250 clear . simulate ( 'click' ) ;
252251 assert ( callback . calledWith ( '' , false ) ) ;
@@ -272,6 +271,109 @@ describe('<DateInput />', () => {
272271 input . simulate ( 'keydown' , { key : 'ArrowRight' , keyCode : 39 , which : 39 } ) ;
273272 assert ( isSameDay ( component . instance ( ) . getCurrentDate ( ) , expectedDate ) ) ;
274273 } ) ;
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 ( ) ===
301+ component . instance ( ) . getCurrentDate ( ) . getDate ( )
302+ ? component . find ( 'Day' ) . at ( tentativeIndexDayToPick + 1 )
303+ : tentativeDayToPick ;
304+
305+ const expectedDate = dayToPick . props ( ) . day . date ;
306+
307+ dayToPick . simulate ( 'click' ) ;
308+ assert ( onCloseCallback . calledOnce ) ;
309+ assert ( onCloseCallback . calledWith ( expectedDate , true ) ) ;
310+ } ) ;
311+ } ) ;
312+
313+ describe ( 'date picker with value property' , ( ) => {
314+ let onChangeCallback ;
315+ let onCloseCallback ;
316+ let wrapper ;
317+
318+ beforeEach ( ( ) => {
319+ onChangeCallback = sinon . spy ( ) ;
320+ onCloseCallback = sinon . spy ( ) ;
321+
322+ const WrappingComponent = ( ) => {
323+ const [ dateValue , setDateValue ] = useState ( new Date ( ) ) ;
324+
325+ const onChange = ( date , valid ) => {
326+ onChangeCallback ( date , valid ) ;
327+ setDateValue ( date ) ;
328+ } ;
329+
330+ return < DateInput onChange = { onChange } onClose = { onCloseCallback } value = { dateValue } /> ;
331+ } ;
332+
333+ wrapper = mount ( < WrappingComponent /> ) ;
334+ } ) ;
335+
336+ it ( 'should call onClose when closing the date picker with the latest selected date in the current month' , ( ) => {
337+ const toggle = wrapper . find ( 'DateInput' ) . find ( 'InputGroup' ) . find ( 'Button' ) ;
338+ toggle . simulate ( 'click' ) ;
339+
340+ const initialDate = wrapper . find ( 'DateInput' ) . instance ( ) . getCurrentDate ( ) ;
341+ const isFirstOfMonth = initialDate . getDate ( ) === 1 ;
342+ const dayToPick = isFirstOfMonth
343+ ? wrapper . find ( 'DateInput' ) . find ( 'Day' ) . at ( 1 )
344+ : wrapper . find ( 'DateInput' ) . find ( 'Day' ) . at ( 0 ) ;
345+
346+ const expectedDate = dayToPick . props ( ) . day . date ;
347+
348+ dayToPick . simulate ( 'click' ) ;
349+ assert ( onCloseCallback . calledOnce ) ;
350+ assert ( onCloseCallback . calledWith ( expectedDate , true ) ) ;
351+ } ) ;
352+
353+ it ( 'should call onClose with the last selected date when we change the month' , ( ) => {
354+ const toggle = wrapper . find ( 'DateInput' ) . find ( 'InputGroup' ) . find ( 'Button' ) ;
355+ toggle . simulate ( 'click' ) ;
356+
357+ const nextMonth = wrapper . find ( 'DateInput' ) . find ( 'Button.js-next-month' ) ;
358+ nextMonth . simulate ( 'click' ) ;
359+
360+ const tentativeIndexDayToPick = 15 ;
361+ const tentativeDayToPick = wrapper . find ( 'DateInput' ) . find ( 'Day' ) . at ( tentativeIndexDayToPick ) ;
362+ const dayToPick =
363+ tentativeDayToPick . props ( ) . day . date . getDate ( ) ===
364+ wrapper . find ( 'DateInput' ) . instance ( ) . getCurrentDate ( ) . getDate ( )
365+ ? wrapper
366+ . find ( 'DateInput' )
367+ . find ( 'Day' )
368+ . at ( tentativeIndexDayToPick + 1 )
369+ : tentativeDayToPick ;
370+
371+ const expectedDate = dayToPick . props ( ) . day . date ;
372+
373+ dayToPick . simulate ( 'click' ) ;
374+ assert ( onCloseCallback . calledOnce ) ;
375+ assert ( onCloseCallback . calledWith ( expectedDate , true ) ) ;
376+ } ) ;
275377 } ) ;
276378
277379 describe ( 'date picker with controlled visible dates' , ( ) => {
0 commit comments