From b7cf451c8c2ca306ac6a65e1e66b31b997d2d0e8 Mon Sep 17 00:00:00 2001 From: sethbarton Date: Thu, 5 Oct 2023 20:58:52 -0600 Subject: [PATCH 1/2] fix: `userEvent.click()` ignores hidden elements (#1145) --- .gitignore | 3 +++ src/convenience/click.ts | 9 +++++++++ src/utils/click/selector.ts | 1 + tests/convenience/click.ts | 17 +++++++++++++++++ 4 files changed, 30 insertions(+) create mode 100644 src/utils/click/selector.ts 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/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(`
`, { From 2de0188a194037276a26bf15e565dd80e9b556c5 Mon Sep 17 00:00:00 2001 From: sethbarton Date: Thu, 5 Oct 2023 21:14:47 -0600 Subject: [PATCH 2/2] Fix broken egghead course link. --- CONTRIBUTING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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