diff --git a/README.md b/README.md index c87d47a0..809fb9a3 100644 --- a/README.md +++ b/README.md @@ -52,16 +52,17 @@ change the state of the checkbox. - [Installation](#installation) - [API](#api) - - [`click(element)`](#clickelement) - - [`dblClick(element)`](#dblclickelement) - - [`async type(element, text, [options])`](#async-typeelement-text-options) + - [`click(element, eventInit, options)`](#clickelement-eventinit-options) + - [`dblClick(element, eventInit, options)`](#dblclickelement-eventinit-options) + - [`type(element, text, [options])`](#typeelement-text-options) - [`upload(element, file, [{ clickInit, changeInit }])`](#uploadelement-file--clickinit-changeinit-) - [`clear(element)`](#clearelement) - [`selectOptions(element, values)`](#selectoptionselement-values) - - [`toggleSelectOptions(element, values)`](#toggleselectoptionselement-values) + - [`deselectOptions(element, values)`](#deselectoptionselement-values) - [`tab({shift, focusTrap})`](#tabshift-focustrap) - - [`async hover(element)`](#async-hoverelement) - - [`async unhover(element)`](#async-unhoverelement) + - [`hover(element)`](#hoverelement) + - [`unhover(element)`](#unhoverelement) + - [`paste(element, text, eventInit, options)`](#pasteelement-text-eventinit-options) - [Issues](#issues) - [Contributors ✨](#contributors-) - [LICENSE](#license) @@ -94,7 +95,7 @@ var userEvent = require('@testing-library/user-event') ## API -### `click(element)` +### `click(element, eventInit, options)` Clicks `element`, depending on what `element` is it can have different side effects. @@ -127,7 +128,10 @@ See the [`MouseEvent`](https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/MouseEvent) constructor documentation for more options. -### `dblClick(element)` +Note that `click` will trigger hover events before clicking. To disable this, +set the `skipHover` option to `true`. + +### `dblClick(element, eventInit, options)` Clicks `element` twice, depending on what `element` is it can have different side effects. @@ -147,7 +151,7 @@ test('double click', () => { }) ``` -### `async type(element, text, [options])` +### `type(element, text, [options])` Writes `text` inside an `` or a `') + userEvent.clear(element) + expect(element).toHaveValue('') +}) + test('does not clear text on disabled inputs', () => { - const {element, getEventCalls} = setup('') + const {element, getEventSnapshot} = setup('') userEvent.clear(element) expect(element).toHaveValue('hello') - expect(getEventCalls()).toMatchInlineSnapshot( + expect(getEventSnapshot()).toMatchInlineSnapshot( `No events were fired on: input[value="hello"]`, ) }) test('does not clear text on readonly inputs', () => { - const {element, getEventCalls} = setup('') + const {element, getEventSnapshot} = setup('') userEvent.clear(element) expect(element).toHaveValue('hello') - expect(getEventCalls()).toMatchInlineSnapshot(` + expect(getEventSnapshot()).toMatchInlineSnapshot(` Events fired on: input[value="hello"] - mouseover: Left (0) - mousemove: Left (0) - mousedown: Left (0) - focus - mouseup: Left (0) - click: Left (0) - mousedown: Left (0) - mouseup: Left (0) - click: Left (0) - dblclick: Left (0) - keydown: Backspace (8) - keyup: Backspace (8) + input[value="hello"] - pointerover + input[value="hello"] - pointerenter + input[value="hello"] - mouseover: Left (0) + input[value="hello"] - mouseenter: Left (0) + input[value="hello"] - pointermove + input[value="hello"] - mousemove: Left (0) + input[value="hello"] - pointerdown + input[value="hello"] - mousedown: Left (0) + input[value="hello"] - focus + input[value="hello"] - focusin + input[value="hello"] - pointerup + input[value="hello"] - mouseup: Left (0) + input[value="hello"] - click: Left (0) + input[value="hello"] - select + input[value="hello"] - keydown: Delete (46) + input[value="hello"] - keyup: Delete (46) `) }) @@ -70,3 +84,11 @@ test('clears even on inputs that cannot (programmatically) have a selection', () // jest-dom does funny stuff with toHaveValue on number inputs expect(number.value).toBe('') }) + +test('non-inputs/textareas are currently unsupported', () => { + const {element} = setup('
') + + expect(() => userEvent.clear(element)).toThrowErrorMatchingInlineSnapshot( + `"clear currently only supports input and textarea elements."`, + ) +}) diff --git a/src/__tests__/click.js b/src/__tests__/click.js index 3e2f628d..d416447f 100644 --- a/src/__tests__/click.js +++ b/src/__tests__/click.js @@ -1,115 +1,155 @@ -import userEvent from '..' +import userEvent from '../' import {setup, addEventListener, addListeners} from './helpers/utils' -test('click in input', () => { - const {element, getEventCalls} = setup('') +test('click in button', () => { + const {element, getEventSnapshot} = setup('`) + const {element, eventWasFired} = setup(`
`) userEvent.click(element.children[0]) - expect(getEventCalls()).toContain('submit') + expect(eventWasFired('submit')).toBe(true) }) test('does not submit a form when clicking on a `) userEvent.click(element.children[0]) - expect(getEventCalls()).not.toContain('submit') + expect(getEventSnapshot()).not.toContain('submit') }) test('does not fire blur on current element if is the same as previous', () => { - const {element, getEventCalls, clearEventCalls} = setup('