@@ -591,50 +591,57 @@ describe('dropdown', () => {
591591 jest . useRealTimers ( ) ;
592592 } ) ;
593593
594- it ( 'should support autoFocus' , async ( ) => {
595- jest . useFakeTimers ( ) ;
596- const focusSpy = jest . spyOn ( HTMLElement . prototype , 'focus' ) ;
594+ it . each ( [ 'direct' , 'wrapped' ] ) (
595+ 'should support autoFocus for a %s menu' ,
596+ async ( mode ) => {
597+ jest . useFakeTimers ( ) ;
598+ const focusSpy = jest . spyOn ( HTMLElement . prototype , 'focus' ) ;
599+
600+ try {
601+ const overlay = (
602+ < Menu >
603+ < MenuItem key = "1" >
604+ < span className = "my-menuitem" > one</ span >
605+ </ MenuItem >
606+ < MenuItem key = "2" > two</ MenuItem >
607+ </ Menu >
608+ ) ;
609+ const { container } = render (
610+ < Dropdown
611+ autoFocus
612+ trigger = { [ 'click' ] }
613+ overlay = { mode === 'wrapped' ? < div > { overlay } </ div > : overlay }
614+ >
615+ < button className = "my-button" > open</ button >
616+ </ Dropdown > ,
617+ ) ;
618+ const trigger = container . querySelector ( '.my-button' ) ;
597619
598- try {
599- const overlay = (
600- < Menu >
601- < MenuItem key = "1" >
602- < span className = "my-menuitem" > one</ span >
603- </ MenuItem >
604- < MenuItem key = "2" > two</ MenuItem >
605- </ Menu >
606- ) ;
607- const { container } = render (
608- < Dropdown autoFocus trigger = { [ 'click' ] } overlay = { overlay } >
609- < button className = "my-button" > open</ button >
610- </ Dropdown > ,
611- ) ;
612- const trigger = container . querySelector ( '.my-button' ) ;
613-
614- // Open menu
615- fireEvent . click ( trigger ) ;
616-
617- await waitForTime ( ) ;
618-
619- expect (
620- container
621- . querySelector ( '.rc-dropdown' )
622- . classList . contains ( 'rc-dropdown-hidden' ) ,
623- ) . toBeFalsy ( ) ;
624- expect ( document . activeElement . className ) . toContain ( 'menu' ) ;
625- expect ( focusSpy ) . toHaveBeenCalledWith ( { preventScroll : true } ) ;
626-
627- // Close menu with Tab
628- window . dispatchEvent ( new KeyboardEvent ( 'keydown' , { keyCode : 9 } ) ) ; // Tab
629-
630- await waitForTime ( ) ;
631-
632- expect ( document . activeElement . className ) . toContain ( 'my-button' ) ;
633- } finally {
634- focusSpy . mockRestore ( ) ;
635- jest . useRealTimers ( ) ;
636- }
637- } ) ;
620+ // Open menu
621+ fireEvent . click ( trigger ) ;
622+
623+ await waitForTime ( ) ;
624+
625+ expect (
626+ container
627+ . querySelector ( '.rc-dropdown' )
628+ . classList . contains ( 'rc-dropdown-hidden' ) ,
629+ ) . toBeFalsy ( ) ;
630+ expect ( document . activeElement . className ) . toContain ( 'menu' ) ;
631+ expect ( focusSpy ) . toHaveBeenLastCalledWith ( { preventScroll : true } ) ;
632+
633+ // Close menu with Tab
634+ window . dispatchEvent ( new KeyboardEvent ( 'keydown' , { keyCode : 9 } ) ) ; // Tab
635+
636+ await waitForTime ( ) ;
637+
638+ expect ( document . activeElement . className ) . toContain ( 'my-button' ) ;
639+ } finally {
640+ focusSpy . mockRestore ( ) ;
641+ jest . useRealTimers ( ) ;
642+ }
643+ } ,
644+ ) ;
638645
639646 it ( 'children cannot be given ref should not throw' , ( ) => {
640647 const errorSpy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
0 commit comments