@@ -53,6 +53,19 @@ const items = [
53
53
] ;
54
54
55
55
describe ( 'OuiContextMenuPanel' , ( ) => {
56
+ // Note: mounting to document because activeElement requires being part of document
57
+ let container : HTMLDivElement | null ;
58
+
59
+ beforeEach ( ( ) => {
60
+ container = document . createElement ( 'div' ) ;
61
+ document . body . appendChild ( container ) ;
62
+ } ) ;
63
+
64
+ afterEach ( ( ) => {
65
+ container ?. parentNode ?. removeChild ( container ) ;
66
+ container = null ;
67
+ } ) ;
68
+
56
69
test ( 'is rendered' , ( ) => {
57
70
const component = render (
58
71
< OuiContextMenuPanel { ...requiredProps } > Hello</ OuiContextMenuPanel >
@@ -187,7 +200,8 @@ describe('OuiContextMenuPanel', () => {
187
200
describe ( 'initialFocusedItemIndex' , ( ) => {
188
201
it ( 'sets focus on the item occupying that index' , async ( ) => {
189
202
const component = mount (
190
- < OuiContextMenuPanel items = { items } initialFocusedItemIndex = { 1 } />
203
+ < OuiContextMenuPanel items = { items } initialFocusedItemIndex = { 1 } /> ,
204
+ { attachTo : container }
191
205
) ;
192
206
193
207
await tick ( 20 ) ;
@@ -199,7 +213,8 @@ describe('OuiContextMenuPanel', () => {
199
213
200
214
it ( 'sets focus on the panel when set to `-1`' , async ( ) => {
201
215
const component = mount (
202
- < OuiContextMenuPanel items = { items } initialFocusedItemIndex = { - 1 } />
216
+ < OuiContextMenuPanel items = { items } initialFocusedItemIndex = { - 1 } /> ,
217
+ { attachTo : container }
203
218
) ;
204
219
205
220
await tick ( 20 ) ;
@@ -310,23 +325,37 @@ describe('OuiContextMenuPanel', () => {
310
325
</ OuiContextMenuPanel >
311
326
) ;
312
327
328
+ // Use the spy approach instead of checking document.activeElement
329
+ const buttonElement = findTestSubject (
330
+ component ,
331
+ 'button'
332
+ ) . getDOMNode ( ) as HTMLButtonElement ;
333
+ const focusSpy = jest . spyOn ( buttonElement , 'focus' ) ;
334
+
313
335
await tick ( 20 ) ;
314
336
315
- expect ( findTestSubject ( component , 'button' ) . getDOMNode ( ) ) . toBe (
316
- document . activeElement
317
- ) ;
337
+ // Check if focus was called
338
+ expect ( focusSpy ) . toHaveBeenCalled ( ) ;
318
339
} ) ;
319
340
320
- it ( 'is not set on anything if hasFocus is false' , ( ) => {
341
+ it ( 'is not set on anything if hasFocus is false' , async ( ) => {
321
342
const component = mount (
322
343
< OuiContextMenuPanel hasFocus = { false } >
323
344
< button data-test-subj = "button" />
324
345
</ OuiContextMenuPanel >
325
346
) ;
326
347
327
- expect ( findTestSubject ( component , 'button' ) . getDOMNode ( ) ) . not . toBe (
328
- document . activeElement
329
- ) ;
348
+ // Use the spy approach instead of checking document.activeElement
349
+ const buttonElement = findTestSubject (
350
+ component ,
351
+ 'button'
352
+ ) . getDOMNode ( ) as HTMLButtonElement ;
353
+ const focusSpy = jest . spyOn ( buttonElement , 'focus' ) ;
354
+
355
+ await tick ( 20 ) ;
356
+
357
+ // Check if focus was called
358
+ expect ( focusSpy ) . not . toHaveBeenCalled ( ) ;
330
359
} ) ;
331
360
} ) ;
332
361
@@ -344,7 +373,8 @@ describe('OuiContextMenuPanel', () => {
344
373
items = { items }
345
374
showNextPanel = { showNextPanelHandler }
346
375
showPreviousPanel = { showPreviousPanelHandler }
347
- />
376
+ /> ,
377
+ { attachTo : container }
348
378
) ;
349
379
} ) ;
350
380
0 commit comments