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

fix: hidden elements clickable #1172

Open
wants to merge 2 commits 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -7,3 +7,6 @@ dist
# when working with contributors
package-lock.json
yarn.lock

# don't commit JetBrains IDE configs
.idea
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Expand Up @@ -48,7 +48,7 @@ Also, please watch the repo and respond to questions/bug reports/feature
requests! Thanks!

<!-- prettier-ignore-start -->
[egghead]: https://egghead.io/series/how-to-contribute-to-an-open-source-project-on-github
[egghead]: https://egghead.io/courses/how-to-contribute-to-an-open-source-project-on-github
[all-contributors]: https://github.com/all-contributors/all-contributors
[issues]: https://github.com/testing-library/user-event/issues
<!-- prettier-ignore-end -->
9 changes: 9 additions & 0 deletions src/convenience/click.ts
@@ -1,7 +1,10 @@
import {type PointerInput} from '../pointer'
import {type Instance} from '../setup'
import {CLICKABLE_SELECTOR} from '../utils/click/selector'

export async function click(this: Instance, element: Element): Promise<void> {
if (!isClickableElement(element)) return

const pointerIn: PointerInput = []
if (!this.config.skipHover) {
pointerIn.push({target: element})
Expand All @@ -15,12 +18,18 @@ export async function dblClick(
this: Instance,
element: Element,
): Promise<void> {
if (!isClickableElement(element)) return
return this.pointer([{target: element}, '[MouseLeft][MouseLeft]'])
}

export async function tripleClick(
this: Instance,
element: Element,
): Promise<void> {
if (!isClickableElement(element)) return
return this.pointer([{target: element}, '[MouseLeft][MouseLeft][MouseLeft]'])
}

export function isClickableElement(element: Element) {
return element.matches(CLICKABLE_SELECTOR)
}
1 change: 1 addition & 0 deletions src/utils/click/selector.ts
@@ -0,0 +1 @@
export const CLICKABLE_SELECTOR = ['*:not([style*="hidden"])'].join(', ')
17 changes: 17 additions & 0 deletions tests/convenience/click.ts
Expand Up @@ -38,6 +38,23 @@ describe.each([
expect(getEvents('click')).toHaveLength(clickCount)
})

test('can not click hidden elements', async () => {
const {element, getEvents, user} = setup(
` <button
style="visibility: hidden"
>
Click me!
</button>`,
{
pointerEventsCheck: PointerEventsCheckLevel.Never,
},
)

await user[method](element)

expect(getEvents('click')).toHaveLength(0)
})

if (method === 'click') {
test('skip hover', async () => {
const {element, getEvents, user} = setup(`<div></div>`, {
Expand Down