@@ -45,11 +45,11 @@ describe('DateInput', () => {
4545 } ) ;
4646
4747 it ( 'should forward a ref' , ( ) => {
48- const ref = createRef < HTMLDivElement > ( ) ;
48+ const ref = createRef < HTMLInputElement > ( ) ;
4949 const { container } = render ( < DateInput { ...props } ref = { ref } /> ) ;
5050 // eslint-disable-next-line testing-library/no-container
51- const wrapper = container . querySelectorAll ( 'div' ) [ 0 ] ;
52- expect ( ref . current ) . toBe ( wrapper ) ;
51+ const input = container . querySelector ( 'input[type="date"]' ) ;
52+ expect ( ref . current ) . toBe ( input ) ;
5353 } ) ;
5454
5555 it ( 'should merge a custom class name with the default ones' , ( ) => {
@@ -222,34 +222,44 @@ describe('DateInput', () => {
222222
223223 describe ( 'state' , ( ) => {
224224 it ( 'should display a default value' , ( ) => {
225- render ( < DateInput { ...props } defaultValue = "2000-01-12" /> ) ;
225+ const ref = createRef < HTMLInputElement > ( ) ;
226+ render ( < DateInput { ...props } ref = { ref } defaultValue = "2000-01-12" /> ) ;
226227
228+ expect ( ref . current ) . toHaveValue ( '2000-01-12' ) ;
227229 expect ( screen . getByLabelText ( / d a y / i) ) . toHaveValue ( '12' ) ;
228230 expect ( screen . getByLabelText ( / m o n t h / i) ) . toHaveValue ( '1' ) ;
229231 expect ( screen . getByLabelText ( / y e a r / i) ) . toHaveValue ( '2000' ) ;
230232 } ) ;
231233
232234 it ( 'should display an initial value' , ( ) => {
233- render ( < DateInput { ...props } value = "2000-01-12" /> ) ;
235+ const ref = createRef < HTMLInputElement > ( ) ;
236+ render ( < DateInput { ...props } ref = { ref } value = "2000-01-12" /> ) ;
234237
238+ expect ( ref . current ) . toHaveValue ( '2000-01-12' ) ;
235239 expect ( screen . getByLabelText ( / d a y / i) ) . toHaveValue ( '12' ) ;
236240 expect ( screen . getByLabelText ( / m o n t h / i) ) . toHaveValue ( '1' ) ;
237241 expect ( screen . getByLabelText ( / y e a r / i) ) . toHaveValue ( '2000' ) ;
238242 } ) ;
239243
240244 it ( 'should ignore an invalid value' , ( ) => {
241- render ( < DateInput { ...props } value = "2000-13-54" /> ) ;
245+ const ref = createRef < HTMLInputElement > ( ) ;
246+ render ( < DateInput { ...props } ref = { ref } value = "2000-13-54" /> ) ;
242247
248+ expect ( ref . current ) . toHaveValue ( '' ) ;
243249 expect ( screen . getByLabelText ( / d a y / i) ) . toHaveValue ( '' ) ;
244250 expect ( screen . getByLabelText ( / m o n t h / i) ) . toHaveValue ( '' ) ;
245251 expect ( screen . getByLabelText ( / y e a r / i) ) . toHaveValue ( '' ) ;
246252 } ) ;
247253
248254 it ( 'should update the displayed value' , ( ) => {
249- const { rerender } = render ( < DateInput { ...props } value = "2000-01-12" /> ) ;
255+ const ref = createRef < HTMLInputElement > ( ) ;
256+ const { rerender } = render (
257+ < DateInput { ...props } ref = { ref } value = "2000-01-12" /> ,
258+ ) ;
250259
251- rerender ( < DateInput { ...props } value = "2000-01-15" /> ) ;
260+ rerender ( < DateInput { ...props } ref = { ref } value = "2000-01-15" /> ) ;
252261
262+ expect ( ref . current ) . toHaveValue ( '2000-01-15' ) ;
253263 expect ( screen . getByLabelText ( / d a y / i) ) . toHaveValue ( '15' ) ;
254264 expect ( screen . getByLabelText ( / m o n t h / i) ) . toHaveValue ( '1' ) ;
255265 expect ( screen . getByLabelText ( / y e a r / i) ) . toHaveValue ( '2000' ) ;
@@ -266,15 +276,17 @@ describe('DateInput', () => {
266276 } ) ;
267277
268278 it ( 'should allow users to type a date' , async ( ) => {
279+ const ref = createRef < HTMLInputElement > ( ) ;
269280 const onChange = vi . fn ( ) ;
270281
271- render ( < DateInput { ...props } onChange = { onChange } /> ) ;
282+ render ( < DateInput { ...props } ref = { ref } onChange = { onChange } /> ) ;
272283
273284 await userEvent . type ( screen . getByLabelText ( 'Year' ) , '2017' ) ;
274285 await userEvent . type ( screen . getByLabelText ( 'Month' ) , '8' ) ;
275286 await userEvent . type ( screen . getByLabelText ( 'Day' ) , '28' ) ;
276287
277- expect ( onChange ) . toHaveBeenCalledWith ( '2017-08-28' ) ;
288+ expect ( onChange ) . toHaveBeenCalled ( ) ;
289+ expect ( ref . current ) . toHaveValue ( '2017-08-28' ) ;
278290 } ) ;
279291
280292 it ( 'should update the minimum and maximum input values as the user types' , async ( ) => {
@@ -304,26 +316,34 @@ describe('DateInput', () => {
304316 } ) ;
305317
306318 it ( 'should allow users to delete the date' , async ( ) => {
319+ const ref = createRef < HTMLInputElement > ( ) ;
307320 const onChange = vi . fn ( ) ;
308321
309322 render (
310- < DateInput { ...props } defaultValue = "2000-01-12" onChange = { onChange } /> ,
323+ < DateInput
324+ { ...props }
325+ ref = { ref }
326+ defaultValue = "2000-01-12"
327+ onChange = { onChange }
328+ /> ,
311329 ) ;
312330
313331 await userEvent . click ( screen . getByLabelText ( / y e a r / i) ) ;
314332 await userEvent . keyboard ( Array ( 9 ) . fill ( '{backspace}' ) . join ( '' ) ) ;
315333
334+ expect ( ref . current ) . toHaveValue ( '' ) ;
316335 expect ( screen . getByLabelText ( / d a y / i) ) . toHaveValue ( '' ) ;
317336 expect ( screen . getByLabelText ( / m o n t h / i) ) . toHaveValue ( '' ) ;
318337 expect ( screen . getByLabelText ( / y e a r / i) ) . toHaveValue ( '' ) ;
319338
320- expect ( onChange ) . toHaveBeenCalledWith ( '' ) ;
339+ expect ( onChange ) . toHaveBeenCalled ( ) ;
321340 } ) ;
322341
323342 it ( 'should allow users to select a date on a calendar' , async ( ) => {
343+ const ref = createRef < HTMLInputElement > ( ) ;
324344 const onChange = vi . fn ( ) ;
325345
326- render ( < DateInput { ...props } onChange = { onChange } /> ) ;
346+ render ( < DateInput { ...props } ref = { ref } onChange = { onChange } /> ) ;
327347
328348 const openCalendarButton = screen . getByRole ( 'button' , {
329349 name : / c h a n g e d a t e / i,
@@ -336,14 +356,21 @@ describe('DateInput', () => {
336356 const dateButton = screen . getByRole ( 'button' , { name : / 1 2 / } ) ;
337357 await userEvent . click ( dateButton ) ;
338358
339- expect ( onChange ) . toHaveBeenCalledWith ( '2000-01-12' ) ;
359+ expect ( ref . current ) . toHaveValue ( '2000-01-12' ) ;
360+ expect ( onChange ) . toHaveBeenCalled ( ) ;
340361 } ) ;
341362
342363 it ( 'should allow users to clear the date' , async ( ) => {
364+ const ref = createRef < HTMLInputElement > ( ) ;
343365 const onChange = vi . fn ( ) ;
344366
345367 render (
346- < DateInput { ...props } defaultValue = "2000-01-12" onChange = { onChange } /> ,
368+ < DateInput
369+ { ...props }
370+ ref = { ref }
371+ defaultValue = "2000-01-12"
372+ onChange = { onChange }
373+ /> ,
347374 ) ;
348375
349376 const openCalendarButton = screen . getByRole ( 'button' , {
@@ -357,7 +384,8 @@ describe('DateInput', () => {
357384 const clearButton = screen . getByRole ( 'button' , { name : / c l e a r d a t e / i } ) ;
358385 await userEvent . click ( clearButton ) ;
359386
360- expect ( onChange ) . toHaveBeenCalledWith ( '' ) ;
387+ expect ( ref . current ) . toHaveValue ( '' ) ;
388+ expect ( onChange ) . toHaveBeenCalled ( ) ;
361389 } ) ;
362390
363391 describe ( 'on narrow viewports' , ( ) => {
@@ -367,9 +395,10 @@ describe('DateInput', () => {
367395
368396 it ( 'should allow users to select a date on a calendar' , async ( ) => {
369397 ( useMedia as Mock ) . mockReturnValue ( true ) ;
398+ const ref = createRef < HTMLInputElement > ( ) ;
370399 const onChange = vi . fn ( ) ;
371400
372- render ( < DateInput { ...props } onChange = { onChange } /> ) ;
401+ render ( < DateInput { ...props } ref = { ref } onChange = { onChange } /> ) ;
373402
374403 const openCalendarButton = screen . getByRole ( 'button' , {
375404 name : / c h a n g e d a t e / i,
@@ -387,15 +416,18 @@ describe('DateInput', () => {
387416 const applyButton = screen . getByRole ( 'button' , { name : / a p p l y / i } ) ;
388417 await userEvent . click ( applyButton ) ;
389418
390- expect ( onChange ) . toHaveBeenCalledWith ( '2000-01-12' ) ;
419+ expect ( ref . current ) . toHaveValue ( '2000-01-12' ) ;
420+ expect ( onChange ) . toHaveBeenCalled ( ) ;
391421 } ) ;
392422
393423 it ( 'should allow users to clear the date' , async ( ) => {
424+ const ref = createRef < HTMLInputElement > ( ) ;
394425 const onChange = vi . fn ( ) ;
395426
396427 render (
397428 < DateInput
398429 { ...props }
430+ ref = { ref }
399431 defaultValue = "2000-01-12"
400432 onChange = { onChange }
401433 /> ,
@@ -412,15 +444,18 @@ describe('DateInput', () => {
412444 const clearButton = screen . getByRole ( 'button' , { name : / c l e a r d a t e / i } ) ;
413445 await userEvent . click ( clearButton ) ;
414446
415- expect ( onChange ) . toHaveBeenCalledWith ( '' ) ;
447+ expect ( ref . current ) . toHaveValue ( '' ) ;
448+ expect ( onChange ) . toHaveBeenCalled ( ) ;
416449 } ) ;
417450
418451 it ( 'should allow users to close the calendar dialog without selecting a date' , async ( ) => {
452+ const ref = createRef < HTMLInputElement > ( ) ;
419453 const onChange = vi . fn ( ) ;
420454
421455 render (
422456 < DateInput
423457 { ...props }
458+ ref = { ref }
424459 defaultValue = "2000-01-12"
425460 onChange = { onChange }
426461 /> ,
@@ -438,6 +473,7 @@ describe('DateInput', () => {
438473 await userEvent . click ( closeButton ) ;
439474
440475 expect ( calendarDialog ) . not . toBeVisible ( ) ;
476+ expect ( ref . current ) . toHaveValue ( '2000-01-12' ) ;
441477 expect ( onChange ) . not . toHaveBeenCalled ( ) ;
442478 } ) ;
443479 } ) ;
0 commit comments