@@ -580,7 +580,24 @@ describe("DatePicker", () => {
580580 } ) ;
581581 } ) ;
582582
583- it ( "should hide the calendar when the pressing Shift + Tab in the date input" , ( ) => {
583+ it ( "should auto-close the datepicker and lose focus when Tab key is pressed when the date input is focused" , async ( ) => {
584+ const { container } = render ( < DatePicker /> ) ;
585+ const input = safeQuerySelector ( container , "input" ) ;
586+ fireEvent . focus ( input ) ;
587+
588+ let reactCalendar = container . querySelector ( "div.react-datepicker" ) ;
589+ expect ( reactCalendar ) . not . toBeNull ( ) ;
590+
591+ fireEvent . keyDown ( input , getKey ( KeyType . Tab ) ) ;
592+
593+ reactCalendar = container . querySelector ( "div.react-datepicker" ) ;
594+ expect ( reactCalendar ) . toBeNull ( ) ;
595+ await waitFor ( ( ) => {
596+ expect ( document . activeElement ) . not . toBe ( input ) ;
597+ } ) ;
598+ } ) ;
599+
600+ it ( "should hide the calendar when the pressing Shift + Tab in the date input" , async ( ) => {
584601 // eslint-disable-next-line prefer-const
585602 let onBlurSpy : ReturnType < typeof jest . spyOn > ;
586603 const onBlur : React . FocusEventHandler < HTMLElement > = (
@@ -594,7 +611,9 @@ describe("DatePicker", () => {
594611 fireEvent . focus ( input ) ;
595612 fireEvent . keyDown ( input , getKey ( KeyType . Tab , true ) ) ;
596613 expect ( container . querySelector ( ".react-datepicker" ) ) . toBeNull ( ) ;
597- expect ( onBlurSpy ) . toHaveBeenCalled ( ) ;
614+ await waitFor ( ( ) => {
615+ expect ( onBlurSpy ) . toHaveBeenCalled ( ) ;
616+ } ) ;
598617 } ) ;
599618
600619 it ( "should not apply the react-datepicker-ignore-onclickoutside class to the date input when closed" , ( ) => {
@@ -1964,7 +1983,7 @@ describe("DatePicker", () => {
19641983 } ) ;
19651984 expect ( div . querySelector ( "input" ) ) . toBe ( document . activeElement ) ;
19661985 } ) ;
1967- it ( "should autoFocus the input when calling the setFocus method" , ( ) => {
1986+ it ( "should autoFocus the input when calling the setFocus method" , async ( ) => {
19681987 const div = document . createElement ( "div" ) ;
19691988 document . body . appendChild ( div ) ;
19701989 let instance : DatePicker | null = null ;
@@ -1982,7 +2001,9 @@ describe("DatePicker", () => {
19822001 act ( ( ) => {
19832002 instance ! . setFocus ( ) ;
19842003 } ) ;
1985- expect ( div . querySelector ( "input" ) ) . toBe ( document . activeElement ) ;
2004+ await waitFor ( ( ) => {
2005+ expect ( div . querySelector ( "input" ) ) . toBe ( document . activeElement ) ;
2006+ } ) ;
19862007 } ) ;
19872008 it ( "should clear preventFocus timeout id when component is unmounted" , ( ) => {
19882009 const div = document . createElement ( "div" ) ;
0 commit comments