Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/document/prepareDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ export function prepareDocument(document: Document) {
const initialValue = getInitialValue(el)
if (initialValue !== undefined) {
if (el.value !== initialValue) {
dispatchDOMEvent(el, 'change')
// a call to blur should already be wrapped in an act
el.dispatchEvent(new (document.defaultView ?? window).Event('change'))
}
clearInitialValue(el)
}
Expand Down
5 changes: 5 additions & 0 deletions src/utils/focus/getActiveElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ export function getActiveElement(
if (activeElementInShadowTree) {
return activeElementInShadowTree
}
} else if (activeElement?.tagName === 'IFRAME') {
let contentWindow = (activeElement as HTMLIFrameElement).contentWindow
if (contentWindow) {
return getActiveElement(contentWindow.document)
}
}
// Browser does not yield disabled elements as document.activeElement - jsdom does
if (isDisabled(activeElement)) {
Expand Down
23 changes: 23 additions & 0 deletions tests/keyboard/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,26 @@ customElements.define(
}
},
)

test('typing on focused element with iframe', async () => {
let iframe: HTMLIFrameElement
let iframeButton: HTMLElement
const {user} = setup(
'<div id="iframe-container"></div>',
)
iframe = document.createElement('iframe')
window.document.body.appendChild(iframe)
iframeButton = iframe.contentWindow!.document.createElement('button')
iframe.contentWindow!.document.body.appendChild(iframeButton)
let keydown = jest.fn()
let keyup = jest.fn()
iframeButton.addEventListener('keydown', keydown)
iframeButton.addEventListener('keyup', keyup)

iframeButton.focus()
await user.keyboard('[Space]')
expect(keydown).toHaveBeenCalled()
expect(keyup).toHaveBeenCalled()

iframe.remove()
})
Loading