Skip to content

Commit 67858c1

Browse files
committed
Improve focus handling for setup Add test for keyboard input on open shadow dom
1 parent 63d5586 commit 67858c1

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

tests/_helpers/setup.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,11 @@ export function getFocusableElement(
115115
const children = Array.from(parent.children)
116116
for (const child of children) {
117117
if ('shadowRoot' in child && child.shadowRoot) {
118-
//JSDOM currently does not support delegatesFocus, find the focusable element ourselves, if delegatesFocus is undefined
119-
120-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
121-
if (child.shadowRoot.delegatesFocus === undefined) {
122-
const possibleFocusableChildElement = getFocusableElement(
123-
child.shadowRoot,
124-
)
125-
if (possibleFocusableChildElement) {
126-
return possibleFocusableChildElement
127-
}
128-
} else if (child.shadowRoot.delegatesFocus) {
129-
return child as HTMLElement
118+
const possibleFocusableChildElement = getFocusableElement(
119+
child.shadowRoot,
120+
)
121+
if (possibleFocusableChildElement) {
122+
return possibleFocusableChildElement
130123
}
131124
}
132125
}

tests/keyboard/index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import userEvent from '#src'
22
import {addListeners, render, setup} from '#testHelpers'
3+
import {
4+
defineShadowInputCustomElementIfNotDefined,
5+
ShadowInput,
6+
} from '../_helpers/shadow-input'
37

48
it('type without focus', async () => {
59
const {element, user} = setup('<input/>', {focus: false})
@@ -163,3 +167,14 @@ test('disabling activeElement moves action to HTMLBodyElement', async () => {
163167
body - keyup: c
164168
`)
165169
})
170+
171+
describe('on shadow DOM', () => {
172+
test('type into an input element', async () => {
173+
defineShadowInputCustomElementIfNotDefined()
174+
const {element, user} = setup<ShadowInput>('<shadow-input></shadow-input>')
175+
176+
await user.keyboard('test')
177+
178+
expect(element.value).toEqual('test')
179+
})
180+
})

0 commit comments

Comments
 (0)