diff --git a/.gitignore b/.gitignore index 8e0c70cb..032f7562 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,6 @@ dist # when working with contributors package-lock.json yarn.lock + +# don't commit JetBrains IDE configs +.idea \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 43bc1c16..653a2785 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -48,7 +48,7 @@ Also, please watch the repo and respond to questions/bug reports/feature requests! Thanks! -[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 diff --git a/src/convenience/click.ts b/src/convenience/click.ts index ae18a622..c16c0d5f 100644 --- a/src/convenience/click.ts +++ b/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 { + if (!isClickableElement(element)) return + const pointerIn: PointerInput = [] if (!this.config.skipHover) { pointerIn.push({target: element}) @@ -15,6 +18,7 @@ export async function dblClick( this: Instance, element: Element, ): Promise { + if (!isClickableElement(element)) return return this.pointer([{target: element}, '[MouseLeft][MouseLeft]']) } @@ -22,5 +26,10 @@ export async function tripleClick( this: Instance, element: Element, ): Promise { + if (!isClickableElement(element)) return return this.pointer([{target: element}, '[MouseLeft][MouseLeft][MouseLeft]']) } + +export function isClickableElement(element: Element) { + return element.matches(CLICKABLE_SELECTOR) +} diff --git a/src/utils/click/selector.ts b/src/utils/click/selector.ts new file mode 100644 index 00000000..bd500d79 --- /dev/null +++ b/src/utils/click/selector.ts @@ -0,0 +1 @@ +export const CLICKABLE_SELECTOR = ['*:not([style*="hidden"])'].join(', ') diff --git a/tests/convenience/click.ts b/tests/convenience/click.ts index fd1ae5ee..0a45fbe0 100644 --- a/tests/convenience/click.ts +++ b/tests/convenience/click.ts @@ -38,6 +38,23 @@ describe.each([ expect(getEvents('click')).toHaveLength(clickCount) }) + test('can not click hidden elements', async () => { + const {element, getEvents, user} = setup( + ` `, + { + pointerEventsCheck: PointerEventsCheckLevel.Never, + }, + ) + + await user[method](element) + + expect(getEvents('click')).toHaveLength(0) + }) + if (method === 'click') { test('skip hover', async () => { const {element, getEvents, user} = setup(`
`, {