Skip to content
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

jest.spyOn() does not work with type() in some cases #986

Closed
JesseChain opened this issue Jul 14, 2022 · 5 comments · Fixed by #987
Closed

jest.spyOn() does not work with type() in some cases #986

JesseChain opened this issue Jul 14, 2022 · 5 comments · Fixed by #987
Labels
bug Something isn't working released

Comments

@JesseChain
Copy link

JesseChain commented Jul 14, 2022

Reproduction example

https://codesandbox.io/s/boring-mountain-pjvlih?file=/src/App.test.ts

User-event version

14.2.1

@JesseChain JesseChain added bug Something isn't working needs assessment This needs to be looked at by a team member labels Jul 14, 2022
@JesseChain
Copy link
Author

I guess this code is the cause

export function prepareSelectionInterceptor(
element: HTMLInputElement | HTMLTextAreaElement,
) {
prepareInterceptor(
element,
'setSelectionRange',
function interceptorImpl(
this: HTMLInputElement | HTMLTextAreaElement,
start: number | Value | null,
end: number | null,
direction: 'forward' | 'backward' | 'none' = 'none',
) {
const isUI = start && typeof start === 'object' && start[UISelection]
if (!isUI) {
this[UISelection] = undefined
}
return {
realArgs: [Number(start), end, direction] as [
number,
number,
typeof direction,
],
}
},
)
prepareInterceptor(
element,
'selectionStart',
function interceptorImpl(this, v) {
this[UISelection] = undefined
return {realArgs: v}
},
)
prepareInterceptor(
element,
'selectionEnd',
function interceptorImpl(this, v) {
this[UISelection] = undefined
return {realArgs: v}
},
)
prepareInterceptor(
element,
'select',
function interceptorImpl(this: HTMLInputElement | HTMLTextAreaElement) {
this[UISelection] = {
anchorOffset: 0,
focusOffset: getUIValue(element).length,
}
return {realArgs: [] as []}
},
)
}

@ph-fritsche ph-fritsche removed the needs assessment This needs to be looked at by a team member label Jul 14, 2022
@ph-fritsche
Copy link
Member

Thanks for the report ❤️

@ph-fritsche
Copy link
Member

@all-contributors add @dnt1996 bug

@allcontributors
Copy link
Contributor

@ph-fritsche

I've put up a pull request to add @dnt1996! 🎉

@github-actions
Copy link

🎉 This issue has been resolved in version 14.2.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working released
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants