11import React , { createRef } from 'react' ;
2- import { act , fireEvent , render , waitForElementToBeRemoved } from '@testing-library/react' ;
2+ import { act , fireEvent , render , waitFor , waitForElementToBeRemoved } from '@testing-library/react' ;
3+ import userEvent from '@testing-library/user-event' ;
34
45import DateTimePicker from './DateTimePicker' ;
56
7+ async function waitForElementToBeRemovedOrHidden ( callback ) {
8+ const element = callback ( ) ;
9+
10+ if ( element ) {
11+ try {
12+ await waitFor ( ( ) =>
13+ expect ( element ) . toHaveAttribute ( 'class' , expect . stringContaining ( '--closed' ) ) ,
14+ ) ;
15+ } catch ( error ) {
16+ await waitForElementToBeRemoved ( element ) ;
17+ }
18+ }
19+ }
20+
621describe ( 'DateTimePicker' , ( ) => {
722 it ( 'passes default name to DateTimeInput' , ( ) => {
823 const { container } = render ( < DateTimePicker /> ) ;
@@ -409,68 +424,82 @@ describe('DateTimePicker', () => {
409424 } ) ;
410425 } ) ;
411426
412- it ( 'closes Calendar component when clicked outside' , ( ) => {
427+ it ( 'closes Calendar component when clicked outside' , async ( ) => {
413428 const root = document . createElement ( 'div' ) ;
414429 document . body . appendChild ( root ) ;
415430
416431 const { container } = render ( < DateTimePicker isCalendarOpen /> , { attachTo : root } ) ;
417432
418- fireEvent . mouseDown ( document . body ) ;
433+ userEvent . click ( document . body ) ;
419434
420- waitForElementToBeRemoved ( ( ) => container . querySelector ( '.react-calendar' ) ) ;
435+ await waitForElementToBeRemovedOrHidden ( ( ) =>
436+ container . querySelector ( '.react-datetime-picker__calendar' ) ,
437+ ) ;
421438 } ) ;
422439
423- it ( 'closes Calendar component when focused outside' , ( ) => {
440+ it ( 'closes Calendar component when focused outside' , async ( ) => {
424441 const root = document . createElement ( 'div' ) ;
425442 document . body . appendChild ( root ) ;
426443
427444 const { container } = render ( < DateTimePicker isCalendarOpen /> , { attachTo : root } ) ;
428445
429446 fireEvent . focusIn ( document . body ) ;
430447
431- waitForElementToBeRemoved ( ( ) => container . querySelector ( '.react-calendar' ) ) ;
448+ await waitForElementToBeRemovedOrHidden ( ( ) =>
449+ container . querySelector ( '.react-datetime-picker__calendar' ) ,
450+ ) ;
432451 } ) ;
433452
434- it ( 'closes Calendar component when tapped outside' , ( ) => {
453+ it ( 'closes Calendar component when tapped outside' , async ( ) => {
435454 const root = document . createElement ( 'div' ) ;
436455 document . body . appendChild ( root ) ;
437456
438457 const { container } = render ( < DateTimePicker isCalendarOpen /> , { attachTo : root } ) ;
439458
440- waitForElementToBeRemoved ( ( ) => container . querySelector ( '.react-calendar' ) ) ;
459+ fireEvent . touchStart ( document . body ) ;
460+
461+ await waitForElementToBeRemovedOrHidden ( ( ) =>
462+ container . querySelector ( '.react-datetime-picker__calendar' ) ,
463+ ) ;
441464 } ) ;
442465
443- it ( 'closes Clock component when clicked outside' , ( ) => {
466+ it ( 'closes Clock component when clicked outside' , async ( ) => {
444467 const root = document . createElement ( 'div' ) ;
445468 document . body . appendChild ( root ) ;
446469
447470 const { container } = render ( < DateTimePicker isClockOpen /> , { attachTo : root } ) ;
448471
449- fireEvent . mouseDown ( document . body ) ;
472+ userEvent . click ( document . body ) ;
450473
451- waitForElementToBeRemoved ( ( ) => container . querySelector ( '.react-clock' ) ) ;
474+ await waitForElementToBeRemovedOrHidden ( ( ) =>
475+ container . querySelector ( '.react-datetime-picker__clock' ) ,
476+ ) ;
452477 } ) ;
453478
454- it ( 'closes Clock component when focused outside' , ( ) => {
479+ it ( 'closes Clock component when focused outside' , async ( ) => {
455480 const root = document . createElement ( 'div' ) ;
456481 document . body . appendChild ( root ) ;
457482
458483 const { container } = render ( < DateTimePicker isClockOpen /> , { attachTo : root } ) ;
459484
460485 fireEvent . focusIn ( document . body ) ;
461486
462- waitForElementToBeRemoved ( ( ) => container . querySelector ( '.react-clock' ) ) ;
487+ await waitForElementToBeRemovedOrHidden ( ( ) =>
488+ container . querySelector ( '.react-datetime-picker__clock' ) ,
489+ ) ;
463490 } ) ;
464491
465- it ( 'closes Clock component when tapped outside' , ( ) => {
492+ it ( 'closes Clock component when tapped outside' , async ( ) => {
466493 const root = document . createElement ( 'div' ) ;
467494 document . body . appendChild ( root ) ;
468495
469496 const { container } = render ( < DateTimePicker isClockOpen /> , { attachTo : root } ) ;
470497
471498 fireEvent . touchStart ( document . body ) ;
472499
473- waitForElementToBeRemoved ( ( ) => container . querySelector ( '.react-clock' ) ) ;
500+ await waitForElementToBeRemovedOrHidden ( ( ) =>
501+ container . querySelector ( '.react-datetime-picker__clock' ) ,
502+ ) ;
474503 } ) ;
475504
476505 it ( 'does not close Calendar component when focused within date inputs' , ( ) => {
@@ -503,7 +532,7 @@ describe('DateTimePicker', () => {
503532 expect ( clock ) . toBeInTheDocument ( ) ;
504533 } ) ;
505534
506- it ( 'closes Clock when Calendar is opened by a click on the calendar icon' , ( ) => {
535+ it ( 'closes Clock when Calendar is opened by a click on the calendar icon' , async ( ) => {
507536 const { container } = render ( < DateTimePicker isClockOpen /> ) ;
508537
509538 const clock = container . querySelector ( '.react-clock' ) ;
@@ -513,10 +542,12 @@ describe('DateTimePicker', () => {
513542
514543 fireEvent . click ( button ) ;
515544
516- waitForElementToBeRemoved ( ( ) => container . querySelector ( '.react-clock' ) ) ;
545+ await waitForElementToBeRemovedOrHidden ( ( ) =>
546+ container . querySelector ( '.react-datetime-picker__clock' ) ,
547+ ) ;
517548 } ) ;
518549
519- it ( 'closes Calendar when calling internal onChange by default' , ( ) => {
550+ it ( 'closes Calendar when calling internal onChange by default' , async ( ) => {
520551 const instance = createRef ( ) ;
521552
522553 const { container } = render ( < DateTimePicker isCalendarOpen ref = { instance } /> ) ;
@@ -527,7 +558,9 @@ describe('DateTimePicker', () => {
527558 onChangeInternal ( new Date ( ) ) ;
528559 } ) ;
529560
530- waitForElementToBeRemoved ( ( ) => container . querySelector ( '.react-calendar' ) ) ;
561+ await waitForElementToBeRemovedOrHidden ( ( ) =>
562+ container . querySelector ( '.react-datetime-picker__calendar' ) ,
563+ ) ;
531564 } ) ;
532565
533566 it ( 'does not close Calendar when calling internal onChange with prop closeWidgets = false' , ( ) => {
@@ -564,7 +597,7 @@ describe('DateTimePicker', () => {
564597 expect ( calendar ) . toBeInTheDocument ( ) ;
565598 } ) ;
566599
567- it ( 'closes Clock when calling internal onChange by default' , ( ) => {
600+ it ( 'closes Clock when calling internal onChange by default' , async ( ) => {
568601 const instance = createRef ( ) ;
569602
570603 const { container } = render ( < DateTimePicker isClockOpen ref = { instance } /> ) ;
@@ -575,7 +608,9 @@ describe('DateTimePicker', () => {
575608 onChangeInternal ( new Date ( ) ) ;
576609 } ) ;
577610
578- waitForElementToBeRemoved ( ( ) => container . querySelector ( '.react-clock' ) ) ;
611+ await waitForElementToBeRemovedOrHidden ( ( ) =>
612+ container . querySelector ( '.react-datetime-picker__clock' ) ,
613+ ) ;
579614 } ) ;
580615
581616 it ( 'does not close Clock when calling internal onChange with prop closeWidgets = false' , ( ) => {
0 commit comments