@@ -459,4 +459,108 @@ describe('Popover', () => {
459459 await userEvent . click ( screen . getByTestId ( 'tabtip-checkbox' ) ) ;
460460 expect ( onRequestClose ) . not . toHaveBeenCalled ( ) ;
461461 } ) ;
462+
463+ it ( 'should NOT call onRequestClose when clicking DatePicker calendar rendered in body' , async ( ) => {
464+ const onRequestClose = jest . fn ( ) ;
465+ render (
466+ < Popover open autoAlign onRequestClose = { onRequestClose } >
467+ < button type = "button" > Open</ button >
468+ < PopoverContent >
469+ < input id = "date-picker-input" aria-label = "Date input" readOnly />
470+ </ PopoverContent >
471+ </ Popover >
472+ ) ;
473+
474+ const input = screen . getByLabelText ( 'Date input' ) ;
475+ const calendar = document . createElement ( 'div' ) ;
476+ calendar . className = 'flatpickr-calendar open' ;
477+ calendar . innerHTML = '<span class="flatpickr-day">26</span>' ;
478+ document . body . appendChild ( calendar ) ;
479+
480+ try {
481+ Object . defineProperty ( input , '_flatpickr' , {
482+ value : { calendarContainer : calendar } ,
483+ configurable : true ,
484+ } ) ;
485+
486+ await userEvent . click ( calendar . querySelector ( '.flatpickr-day' ) ) ;
487+
488+ expect ( onRequestClose ) . not . toHaveBeenCalled ( ) ;
489+ } finally {
490+ document . body . removeChild ( calendar ) ;
491+ }
492+ } ) ;
493+
494+ it ( 'should NOT call onRequestClose when focus moves to DatePicker calendar' , async ( ) => {
495+ const onRequestClose = jest . fn ( ) ;
496+ const { container } = render (
497+ < Popover open autoAlign onRequestClose = { onRequestClose } >
498+ < button type = "button" > Open</ button >
499+ < PopoverContent >
500+ < input id = "date-picker-input" aria-label = "Date input" readOnly />
501+ </ PopoverContent >
502+ </ Popover >
503+ ) ;
504+
505+ const input = screen . getByLabelText ( 'Date input' ) ;
506+ const calendar = document . createElement ( 'div' ) ;
507+ calendar . className = 'flatpickr-calendar open' ;
508+ calendar . innerHTML = '<span class="flatpickr-day" tabindex="0">26</span>' ;
509+ document . body . appendChild ( calendar ) ;
510+
511+ try {
512+ Object . defineProperty ( input , '_flatpickr' , {
513+ value : { calendarContainer : calendar } ,
514+ configurable : true ,
515+ } ) ;
516+
517+ input . focus ( ) ;
518+ const popoverEl = container . firstChild ;
519+ const focusoutEvent = new FocusEvent ( 'focusout' , {
520+ bubbles : true ,
521+ relatedTarget : calendar . querySelector ( '.flatpickr-day' ) ,
522+ } ) ;
523+ popoverEl . dispatchEvent ( focusoutEvent ) ;
524+
525+ expect ( onRequestClose ) . not . toHaveBeenCalled ( ) ;
526+ } finally {
527+ document . body . removeChild ( calendar ) ;
528+ }
529+ } ) ;
530+
531+ it ( 'should call onRequestClose when clicking DatePicker calendar outside popover' , async ( ) => {
532+ const onRequestClose = jest . fn ( ) ;
533+ render (
534+ < Popover open autoAlign onRequestClose = { onRequestClose } >
535+ < button type = "button" > Open</ button >
536+ < PopoverContent >
537+ < input id = "popover-input" aria-label = "Popover input" readOnly />
538+ </ PopoverContent >
539+ </ Popover >
540+ ) ;
541+
542+ const outsideInput = document . createElement ( 'input' ) ;
543+ outsideInput . setAttribute ( 'aria-label' , 'Outside input' ) ;
544+ outsideInput . readOnly = true ;
545+ document . body . appendChild ( outsideInput ) ;
546+
547+ const calendar = document . createElement ( 'div' ) ;
548+ calendar . className = 'flatpickr-calendar open' ;
549+ calendar . innerHTML = '<span class="flatpickr-day">26</span>' ;
550+ document . body . appendChild ( calendar ) ;
551+
552+ try {
553+ Object . defineProperty ( outsideInput , '_flatpickr' , {
554+ value : { calendarContainer : calendar } ,
555+ configurable : true ,
556+ } ) ;
557+
558+ await userEvent . click ( calendar . querySelector ( '.flatpickr-day' ) ) ;
559+
560+ expect ( onRequestClose ) . toHaveBeenCalled ( ) ;
561+ } finally {
562+ document . body . removeChild ( calendar ) ;
563+ document . body . removeChild ( outsideInput ) ;
564+ }
565+ } ) ;
462566} ) ;
0 commit comments