Skip to content

Add iframe support and fix nested act #1275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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