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: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const CLICKABLE_SELECTOR = ['*:not([style*="hidden"])'].join(', ')
17 changes: 17 additions & 0 deletions tests/convenience/click.ts
Original file line number Diff line number Diff line change
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