@@ -58,8 +58,7 @@ async function openWithKbd(props: DropdownMenuSetupProps = {}, key: string = kbd
5858
5959async function openSubmenu ( props : Awaited < ReturnType < typeof openWithKbd > > ) {
6060 const t = props ;
61- await userEvent . keyboard ( kbd . ARROW_DOWN ) ;
62- await expect . element ( page . getByTestId ( "sub-trigger" ) ) . toHaveFocus ( ) ;
61+ await focusSubTrigger ( ) ;
6362
6463 await expectNotExists ( t . getSubContent ( ) ) ;
6564 await userEvent . keyboard ( kbd . ARROW_RIGHT ) ;
@@ -71,6 +70,15 @@ async function openSubmenu(props: Awaited<ReturnType<typeof openWithKbd>>) {
7170 } ;
7271}
7372
73+ async function focusSubTrigger ( ) : Promise < void > {
74+ const subtrigger = page . getByTestId ( "sub-trigger" ) ;
75+ if ( subtrigger . element ( ) === document . activeElement ) return ;
76+ await userEvent . keyboard ( kbd . ARROW_DOWN ) ;
77+ if ( subtrigger . element ( ) === document . activeElement ) return ;
78+ await userEvent . keyboard ( kbd . ARROW_DOWN ) ;
79+ await expect . element ( subtrigger ) . toHaveFocus ( ) ;
80+ }
81+
7482afterEach ( ( ) => {
7583 vi . resetAllMocks ( ) ;
7684} ) ;
@@ -145,9 +153,7 @@ it("should manage focus correctly when opened with keyboard", async () => {
145153it ( "should open submenu with keyboard on subtrigger" , async ( ) => {
146154 await openWithKbd ( ) ;
147155
148- await userEvent . keyboard ( kbd . ARROW_DOWN ) ;
149- const subtrigger = page . getByTestId ( "sub-trigger" ) ;
150- await expect . element ( subtrigger ) . toHaveFocus ( ) ;
156+ await focusSubTrigger ( ) ;
151157 await expectNotExists ( page . getByTestId ( "sub-content" ) ) ;
152158 await userEvent . keyboard ( kbd . ARROW_RIGHT ) ;
153159 await expectExists ( page . getByTestId ( "sub-content" ) ) ;
@@ -201,8 +207,7 @@ it("should check the radio item when clicked & respects binding", async () => {
201207it ( "should skip over disabled items when navigating with the keyboard" , async ( ) => {
202208 await openWithKbd ( ) ;
203209 await expect . element ( page . getByTestId ( "item" ) ) . toHaveFocus ( ) ;
204- await userEvent . keyboard ( kbd . ARROW_DOWN ) ;
205- await expect . element ( page . getByTestId ( "sub-trigger" ) ) . toHaveFocus ( ) ;
210+ await focusSubTrigger ( ) ;
206211 await userEvent . keyboard ( kbd . ARROW_DOWN ) ;
207212 await expect . element ( page . getByTestId ( "checkbox-item" ) ) . toHaveFocus ( ) ;
208213 await expect . element ( page . getByTestId ( "disabled-item" ) ) . not . toHaveFocus ( ) ;
@@ -215,8 +220,7 @@ it("should not loop through the menu items when the `loop` prop is set to false/
215220 loop : false ,
216221 } ,
217222 } ) ;
218- await userEvent . keyboard ( kbd . ARROW_DOWN ) ;
219- await expect . element ( page . getByTestId ( "sub-trigger" ) ) . toHaveFocus ( ) ;
223+ await focusSubTrigger ( ) ;
220224 await userEvent . keyboard ( kbd . ARROW_DOWN ) ;
221225 await expect . element ( page . getByTestId ( "checkbox-item" ) ) . toHaveFocus ( ) ;
222226 await userEvent . keyboard ( kbd . ARROW_DOWN ) ;
@@ -235,8 +239,7 @@ it("should loop through the menu items when the `loop` prop is set to true", asy
235239 loop : true ,
236240 } ,
237241 } ) ;
238- await userEvent . keyboard ( kbd . ARROW_DOWN ) ;
239- await expect . element ( page . getByTestId ( "sub-trigger" ) ) . toHaveFocus ( ) ;
242+ await focusSubTrigger ( ) ;
240243 await userEvent . keyboard ( kbd . ARROW_DOWN ) ;
241244 await expect . element ( page . getByTestId ( "checkbox-item" ) ) . toHaveFocus ( ) ;
242245 await userEvent . keyboard ( kbd . ARROW_DOWN ) ;
0 commit comments