Skip to content

Commit 00478de

Browse files
committed
Add offsetParent to focus trap tests
1 parent 7a7faa9 commit 00478de

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

src/__tests__/focus-trap.test.tsx

+26-6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,26 @@ beforeAll(() => {
1717
getClientRects: {
1818
get: () => () => [42],
1919
},
20+
offsetParent: {
21+
get() {
22+
// eslint-disable-next-line @typescript-eslint/no-this-alias
23+
for (let element = this; element; element = element.parentNode) {
24+
if (element.style?.display?.toLowerCase() === 'none') {
25+
return null
26+
}
27+
}
28+
29+
if (this.style?.position?.toLowerCase() === 'fixed') {
30+
return null
31+
}
32+
33+
if (this.tagName.toLowerCase() in ['html', 'body']) {
34+
return null
35+
}
36+
37+
return this.parentNode
38+
},
39+
},
2040
})
2141
} catch {
2242
// ignore
@@ -40,7 +60,7 @@ it('Should initially focus the first element when activated', () => {
4060
const controller = focusTrap(trapContainer)
4161
expect(document.activeElement).toEqual(firstButton)
4262

43-
controller.abort()
63+
controller?.abort()
4464
})
4565

4666
it('Should initially focus the initialFocus element when specified', () => {
@@ -57,7 +77,7 @@ it('Should initially focus the initialFocus element when specified', () => {
5777
const controller = focusTrap(trapContainer, secondButton)
5878
expect(document.activeElement).toEqual(secondButton)
5979

60-
controller.abort()
80+
controller?.abort()
6181
})
6282

6383
it('Should prevent focus from exiting the trap, returns focus to first element', async () => {
@@ -86,7 +106,7 @@ it('Should prevent focus from exiting the trap, returns focus to first element',
86106
await user.tab()
87107
expect(document.activeElement).toEqual(firstButton)
88108

89-
controller.abort()
109+
controller?.abort()
90110

91111
lastButton.focus()
92112
await user.tab()
@@ -125,7 +145,7 @@ it('Should cycle focus from last element to first element and vice-versa', async
125145
await user.tab({shift: true})
126146
expect(document.activeElement).toEqual(lastButton)
127147

128-
controller.abort()
148+
controller?.abort()
129149
})
130150

131151
it('Should should release the trap when the signal is aborted', async () => {
@@ -154,7 +174,7 @@ it('Should should release the trap when the signal is aborted', async () => {
154174
await user.tab()
155175
expect(document.activeElement).toEqual(firstButton)
156176

157-
controller.abort()
177+
controller?.abort()
158178

159179
lastButton.focus()
160180
await user.tab()
@@ -218,5 +238,5 @@ it('Should handle dynamic content', async () => {
218238
await user.tab({shift: true})
219239
expect(document.activeElement).toEqual(secondButton)
220240

221-
controller.abort()
241+
controller?.abort()
222242
})

0 commit comments

Comments
 (0)