@testing-library/user-event 10.3.2 → 10.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # CHANGELOG
2
+
3
+ The changelog is automatically updated using
4
+ [semantic-release](https://github.com/semantic-release/semantic-release). You
5
+ can see it on the [releases page](../../releases).
package/LICENSE CHANGED
@@ -1,6 +1,5 @@
1
- MIT License
2
-
3
- Copyright (c) 2018 Giorgio Polvara
1
+ The MIT License (MIT)
2
+ Copyright (c) 2020 Giorgio Polvara
4
3
 
5
4
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
5
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  <div align="center">
2
- <h1>user-event</h1>
2
+ <h1>@testing-library/user-event</h1>
3
3
 
4
- <a href="https://www.emojione.com/emoji/1f415">
4
+ <a href="https://www.joypixels.com/profiles/emoji/1f415">
5
5
  <img
6
6
  height="80"
7
7
  width="80"
@@ -10,17 +10,24 @@
10
10
  />
11
11
  </a>
12
12
 
13
- <p>Simulate user events for <a href="https://github.com/testing-library/react-testing-library">react-testing-library</a>.</p>
14
-
15
- <br />
13
+ <p>Fire events the same way the user does</p>
16
14
  </div>
17
15
 
18
- <hr />
16
+ ---
19
17
 
20
- [![Build Status](https://travis-ci.org/testing-library/user-event.svg?branch=master)](https://travis-ci.org/testing-library/user-event)
21
- [![Maintainability](https://api.codeclimate.com/v1/badges/75f1ff4397e994c6004e/maintainability)](https://codeclimate.com/github/testing-library/user-event/maintainability)
22
- [![Test Coverage](https://api.codeclimate.com/v1/badges/75f1ff4397e994c6004e/test_coverage)](https://codeclimate.com/github/testing-library/user-event/test_coverage)
23
- [![All Contributors](https://img.shields.io/badge/all_contributors-13-orange.svg?style=flat-square)](#contributors-)
18
+ <!-- prettier-ignore-start -->
19
+ [![Build Status][build-badge]][build]
20
+ [![Code Coverage][coverage-badge]][coverage]
21
+ [![version][version-badge]][package]
22
+ [![downloads][downloads-badge]][npmtrends]
23
+ [![MIT License][license-badge]][license]
24
+
25
+ <!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
26
+ [![All Contributors](https://img.shields.io/badge/all_contributors-32-orange.svg?style=flat-square)](#contributors-)
27
+ <!-- ALL-CONTRIBUTORS-BADGE:END -->
28
+ [![PRs Welcome][prs-badge]][prs]
29
+ [![Code of Conduct][coc-badge]][coc]
30
+ <!-- prettier-ignore-end -->
24
31
 
25
32
  ## The problem
26
33
 
@@ -38,6 +45,28 @@ change the state of the checkbox.
38
45
 
39
46
  **The library is still a work in progress and any help is appreciated.**
40
47
 
48
+ ## Table of Contents
49
+
50
+ <!-- START doctoc generated TOC please keep comment here to allow auto update -->
51
+ <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
52
+
53
+ - [Installation](#installation)
54
+ - [API](#api)
55
+ - [`click(element)`](#clickelement)
56
+ - [`dblClick(element)`](#dblclickelement)
57
+ - [`async type(element, text, [options])`](#async-typeelement-text-options)
58
+ - [`upload(element, file, [{ clickInit, changeInit }])`](#uploadelement-file--clickinit-changeinit-)
59
+ - [`clear(element)`](#clearelement)
60
+ - [`selectOptions(element, values)`](#selectoptionselement-values)
61
+ - [`tab({shift, focusTrap})`](#tabshift-focustrap)
62
+ - [Issues](#issues)
63
+ - [🐛 Bugs](#-bugs)
64
+ - [💡 Feature Requests](#-feature-requests)
65
+ - [Contributors ✨](#contributors-)
66
+ - [LICENSE](#license)
67
+
68
+ <!-- END doctoc generated TOC please keep comment here to allow auto update -->
69
+
41
70
  ## Installation
42
71
 
43
72
  With NPM:
@@ -55,11 +84,11 @@ yarn add @testing-library/user-event --dev
55
84
  Now simply import it in your tests:
56
85
 
57
86
  ```js
58
- import userEvent from "@testing-library/user-event";
87
+ import userEvent from '@testing-library/user-event'
59
88
 
60
89
  // or
61
90
 
62
- var userEvent = require("@testing-library/user-event");
91
+ var userEvent = require('@testing-library/user-event')
63
92
  ```
64
93
 
65
94
  ## API
@@ -70,27 +99,27 @@ Clicks `element`, depending on what `element` is it can have different side
70
99
  effects.
71
100
 
72
101
  ```jsx
73
- import React from "react";
74
- import { render, screen } from "@testing-library/react";
75
- import userEvent from "@testing-library/user-event";
102
+ import React from 'react'
103
+ import {render, screen} from '@testing-library/react'
104
+ import userEvent from '@testing-library/user-event'
76
105
 
77
- test("click", () => {
106
+ test('click', () => {
78
107
  render(
79
108
  <div>
80
109
  <label htmlFor="checkbox">Check</label>
81
- <input id="checkbox" data-testid="checkbox" type="checkbox" />
82
- </div>
83
- );
110
+ <input id="checkbox" type="checkbox" />
111
+ </div>,
112
+ )
84
113
 
85
- userEvent.click(screen.getByText("Check"));
86
- expect(screen.getByTestId("checkbox")).toHaveAttribute("checked", true);
87
- });
114
+ userEvent.click(screen.getByText('Check'))
115
+ expect(screen.getByLabelText('Check')).toHaveAttribute('checked', true)
116
+ })
88
117
  ```
89
118
 
90
119
  You can also ctrlClick / shiftClick etc with
91
120
 
92
121
  ```js
93
- userEvent.click(elem, { ctrlKey: true, shiftKey: true });
122
+ userEvent.click(elem, {ctrlKey: true, shiftKey: true})
94
123
  ```
95
124
 
96
125
  See the
@@ -103,18 +132,18 @@ Clicks `element` twice, depending on what `element` is it can have different
103
132
  side effects.
104
133
 
105
134
  ```jsx
106
- import React from "react";
107
- import { render, screen } from "@testing-library/react";
108
- import userEvent from "@testing-library/user-event";
109
-
110
- test("double click", () => {
111
- const onChange = jest.fn();
112
- render(<input type="checkbox" id="checkbox" onChange={onChange} />);
113
- const checkbox = screen.getByTestId("checkbox");
114
- userEvent.dblClick(checkbox);
115
- expect(onChange).toHaveBeenCalledTimes(2);
116
- expect(checkbox).toHaveProperty("checked", false);
117
- });
135
+ import React from 'react'
136
+ import {render, screen} from '@testing-library/react'
137
+ import userEvent from '@testing-library/user-event'
138
+
139
+ test('double click', () => {
140
+ const onChange = jest.fn()
141
+ render(<input type="checkbox" id="checkbox" onChange={onChange} />)
142
+ const checkbox = screen.getByTestId('checkbox')
143
+ userEvent.dblClick(checkbox)
144
+ expect(onChange).toHaveBeenCalledTimes(2)
145
+ expect(checkbox).toHaveProperty('checked', false)
146
+ })
118
147
  ```
119
148
 
120
149
  ### `async type(element, text, [options])`
@@ -122,16 +151,16 @@ test("double click", () => {
122
151
  Writes `text` inside an `<input>` or a `<textarea>`.
123
152
 
124
153
  ```jsx
125
- import React from "react";
126
- import { render, screen } from "@testing-library/react";
127
- import userEvent from "@testing-library/user-event";
154
+ import React from 'react'
155
+ import {render, screen} from '@testing-library/react'
156
+ import userEvent from '@testing-library/user-event'
128
157
 
129
- test("type", async () => {
130
- render(<textarea data-testid="email" />);
158
+ test('type', async () => {
159
+ render(<textarea />)
131
160
 
132
- await userEvent.type(screen.getByTestId("email"), "Hello, World!");
133
- expect(screen.getByTestId("email")).toHaveAttribute("value", "Hello, World!");
134
- });
161
+ await userEvent.type(screen.getByRole('textbox'), 'Hello, World!')
162
+ expect(screen.getByRole('textbox')).toHaveAttribute('value', 'Hello, World!')
163
+ })
135
164
  ```
136
165
 
137
166
  If `options.allAtOnce` is `true`, `type` will write `text` at once rather than
@@ -148,36 +177,46 @@ Uploads file to an `<input>`. For uploading multiple files use `<input>` with
148
177
  it's possible to initialize click or change event with using third argument.
149
178
 
150
179
  ```jsx
151
- import React from "react";
152
- import { render, screen } from "@testing-library/react";
153
- import userEvent from "@testing-library/user-event";
180
+ import React from 'react'
181
+ import {render, screen} from '@testing-library/react'
182
+ import userEvent from '@testing-library/user-event'
154
183
 
155
- test("upload file", () => {
156
- const file = new File(["hello"], "hello.png", { type: "image/png" });
184
+ test('upload file', () => {
185
+ const file = new File(['hello'], 'hello.png', {type: 'image/png'})
157
186
 
158
- render(<input type="file" data-testid="upload" />);
187
+ render(
188
+ <div>
189
+ <label htmlFor="file-uploader">Upload file:</label>
190
+ <input id="file-uploader" type="file" />
191
+ </div>,
192
+ )
159
193
 
160
- userEvent.upload(screen.getByTestId("upload"), file);
194
+ userEvent.upload(screen.getByLabelText(/upload file/i), file)
161
195
 
162
- expect(input.files[0]).toStrictEqual(file);
163
- expect(input.files.item(0)).toStrictEqual(file);
164
- expect(input.files).toHaveLength(1);
165
- });
196
+ expect(input.files[0]).toStrictEqual(file)
197
+ expect(input.files.item(0)).toStrictEqual(file)
198
+ expect(input.files).toHaveLength(1)
199
+ })
166
200
 
167
- test("upload multiple files", () => {
201
+ test('upload multiple files', () => {
168
202
  const files = [
169
- new File(["hello"], "hello.png", { type: "image/png" }),
170
- new File(["there"], "there.png", { type: "image/png" }),
171
- ];
203
+ new File(['hello'], 'hello.png', {type: 'image/png'}),
204
+ new File(['there'], 'there.png', {type: 'image/png'}),
205
+ ]
172
206
 
173
- render(<input type="file" multiple data-testid="upload" />);
207
+ render(
208
+ <div>
209
+ <label htmlFor="file-uploader">Upload file:</label>
210
+ <input id="file-uploader" type="file" multiple />
211
+ </div>,
212
+ )
174
213
 
175
- userEvent.upload(screen.getByTestId("upload"), files);
214
+ userEvent.upload(screen.getByLabelText(/upload file/i), files)
176
215
 
177
- expect(input.files).toHaveLength(2);
178
- expect(input.files[0]).toStrictEqual(files[0]);
179
- expect(input.files[1]).toStrictEqual(files[1]);
180
- });
216
+ expect(input.files).toHaveLength(2)
217
+ expect(input.files[0]).toStrictEqual(files[0])
218
+ expect(input.files[1]).toStrictEqual(files[1])
219
+ })
181
220
  ```
182
221
 
183
222
  ### `clear(element)`
@@ -185,16 +224,16 @@ test("upload multiple files", () => {
185
224
  Selects the text inside an `<input>` or `<textarea>` and deletes it.
186
225
 
187
226
  ```jsx
188
- import React from "react";
189
- import { render, screen } from "@testing-library/react";
190
- import userEvent from "@testing-library/user-event";
227
+ import React from 'react'
228
+ import {render, screen} from '@testing-library/react'
229
+ import userEvent from '@testing-library/user-event'
191
230
 
192
- test("clear", () => {
193
- render(<textarea data-testid="email" value="Hello, World!" />);
231
+ test('clear', () => {
232
+ render(<textarea value="Hello, World!" />)
194
233
 
195
- userEvent.clear(screen.getByTestId("email"));
196
- expect(screen.getByTestId("email")).toHaveAttribute("value", "");
197
- });
234
+ userEvent.clear(screen.getByRole('textbox', 'email'))
235
+ expect(screen.getByRole('textbox', 'email')).toHaveAttribute('value', '')
236
+ })
198
237
  ```
199
238
 
200
239
  ### `selectOptions(element, values)`
@@ -203,11 +242,11 @@ Selects the specified option(s) of a `<select>` or a `<select multiple>`
203
242
  element.
204
243
 
205
244
  ```jsx
206
- import React from "react";
207
- import { render, screen } from "@testing-library/react";
208
- import userEvent from "@testing-library/user-event";
245
+ import React from 'react'
246
+ import {render, screen} from '@testing-library/react'
247
+ import userEvent from '@testing-library/user-event'
209
248
 
210
- test("selectOptions", () => {
249
+ test('selectOptions', () => {
211
250
  render(
212
251
  <select multiple data-testid="select-multiple">
213
252
  <option data-testid="val1" value="1">
@@ -219,20 +258,29 @@ test("selectOptions", () => {
219
258
  <option data-testid="val3" value="3">
220
259
  C
221
260
  </option>
222
- </select>
223
- );
261
+ </select>,
262
+ )
224
263
 
225
- userEvent.selectOptions(screen.getByTestId("select-multiple"), ["1", "3"]);
264
+ userEvent.selectOptions(screen.getByTestId('select-multiple'), ['1', '3'])
226
265
 
227
- expect(screen.getByTestId("val1").selected).toBe(true);
228
- expect(screen.getByTestId("val2").selected).toBe(false);
229
- expect(screen.getByTestId("val3").selected).toBe(true);
230
- });
266
+ expect(screen.getByTestId('val1').selected).toBe(true)
267
+ expect(screen.getByTestId('val2').selected).toBe(false)
268
+ expect(screen.getByTestId('val3').selected).toBe(true)
269
+ })
231
270
  ```
232
271
 
233
272
  The `values` parameter can be either an array of values or a singular scalar
234
273
  value.
235
274
 
275
+ It also accepts option nodes:
276
+
277
+ ```js
278
+ userEvent.selectOptions(screen.getByTestId('select-multiple'), [
279
+ screen.getByText('A'),
280
+ screen.getByText('B'),
281
+ ])
282
+ ```
283
+
236
284
  ### `tab({shift, focusTrap})`
237
285
 
238
286
  Fires a tab event changing the document.activeElement in the same way the
@@ -253,47 +301,64 @@ Options:
253
301
  > is available to let you ensure your user is restricted within a focus-trap.
254
302
 
255
303
  ```jsx
256
- import React from "react";
257
- import { render, screen } from "@testing-library/react";
258
- import "@testing-library/jest-dom/extend-expect";
259
- import userEvent from "@testing-library/user-event";
304
+ import React from 'react'
305
+ import {render, screen} from '@testing-library/react'
306
+ import '@testing-library/jest-dom/extend-expect'
307
+ import userEvent from '@testing-library/user-event'
260
308
 
261
- it("should cycle elements in document tab order", () => {
309
+ it('should cycle elements in document tab order', () => {
262
310
  render(
263
311
  <div>
264
312
  <input data-testid="element" type="checkbox" />
265
313
  <input data-testid="element" type="radio" />
266
314
  <input data-testid="element" type="number" />
267
- </div>
268
- );
315
+ </div>,
316
+ )
269
317
 
270
- const [checkbox, radio, number] = screen.getAllByTestId("element");
318
+ const [checkbox, radio, number] = screen.getAllByTestId('element')
271
319
 
272
- expect(document.body).toHaveFocus();
320
+ expect(document.body).toHaveFocus()
273
321
 
274
- userEvent.tab();
322
+ userEvent.tab()
275
323
 
276
- expect(checkbox).toHaveFocus();
324
+ expect(checkbox).toHaveFocus()
277
325
 
278
- userEvent.tab();
326
+ userEvent.tab()
279
327
 
280
- expect(radio).toHaveFocus();
328
+ expect(radio).toHaveFocus()
281
329
 
282
- userEvent.tab();
330
+ userEvent.tab()
283
331
 
284
- expect(number).toHaveFocus();
332
+ expect(number).toHaveFocus()
285
333
 
286
- userEvent.tab();
334
+ userEvent.tab()
287
335
 
288
336
  // cycle goes back to first element
289
- expect(checkbox).toHaveFocus();
290
- });
337
+ expect(checkbox).toHaveFocus()
338
+ })
291
339
  ```
292
340
 
293
- ## Contributors
341
+ ## Issues
342
+
343
+ _Looking to contribute? Look for the [Good First Issue][good-first-issue]
344
+ label._
345
+
346
+ ### 🐛 Bugs
347
+
348
+ Please file an issue for bugs, missing documentation, or unexpected behavior.
349
+
350
+ [**See Bugs**][bugs]
351
+
352
+ ### 💡 Feature Requests
353
+
354
+ Please file an issue to suggest new features. Vote on feature requests by adding
355
+ a 👍. This helps maintainers prioritize what to work on.
294
356
 
295
- Thanks goes to these wonderful people
296
- ([emoji key](https://github.com/all-contributors/all-contributors#emoji-key)):
357
+ [**See Feature Requests**][requests]
358
+
359
+ ## Contributors ✨
360
+
361
+ Thanks goes to these people ([emoji key][emojis]):
297
362
 
298
363
  <!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
299
364
  <!-- prettier-ignore-start -->
@@ -338,6 +403,8 @@ Thanks goes to these wonderful people
338
403
  <tr>
339
404
  <td align="center"><a href="https://proling.ru/"><img src="https://avatars2.githubusercontent.com/u/16336572?v=4" width="100px;" alt=""/><br /><sub><b>Vadim Shvetsov</b></sub></a><br /><a href="#ideas-vadimshvetsov" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/testing-library/user-event/commits?author=vadimshvetsov" title="Code">💻</a> <a href="https://github.com/testing-library/user-event/commits?author=vadimshvetsov" title="Tests">⚠️</a></td>
340
405
  <td align="center"><a href="https://github.com/9still"><img src="https://avatars0.githubusercontent.com/u/4924760?v=4" width="100px;" alt=""/><br /><sub><b>Greg Shtilman</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=9still" title="Code">💻</a> <a href="https://github.com/testing-library/user-event/commits?author=9still" title="Tests">⚠️</a> <a href="https://github.com/testing-library/user-event/issues?q=author%3A9still" title="Bug reports">🐛</a></td>
406
+ <td align="center"><a href="https://github.com/rbusquet"><img src="https://avatars1.githubusercontent.com/u/7198302?v=4" width="100px;" alt=""/><br /><sub><b>Ricardo Busquet</b></sub></a><br /><a href="https://github.com/testing-library/user-event/issues?q=author%3Arbusquet" title="Bug reports">🐛</a> <a href="https://github.com/testing-library/user-event/commits?author=rbusquet" title="Code">💻</a> <a href="https://github.com/testing-library/user-event/commits?author=rbusquet" title="Tests">⚠️</a></td>
407
+ <td align="center"><a href="https://www.linkedin.com/in/dougbacelar/en"><img src="https://avatars3.githubusercontent.com/u/9267678?v=4" width="100px;" alt=""/><br /><sub><b>Doug Bacelar</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=dougbacelar" title="Code">💻</a> <a href="https://github.com/testing-library/user-event/commits?author=dougbacelar" title="Tests">⚠️</a></td>
341
408
  </tr>
342
409
  </table>
343
410
 
@@ -345,6 +412,33 @@ Thanks goes to these wonderful people
345
412
  <!-- prettier-ignore-end -->
346
413
  <!-- ALL-CONTRIBUTORS-LIST:END -->
347
414
 
348
- This project follows the
349
- [all-contributors](https://github.com/all-contributors/all-contributors)
350
- specification. Contributions of any kind welcome!
415
+ This project follows the [all-contributors][all-contributors] specification.
416
+ Contributions of any kind welcome!
417
+
418
+ ## LICENSE
419
+
420
+ MIT
421
+
422
+ <!-- prettier-ignore-start -->
423
+ [npm]: https://www.npmjs.com
424
+ [node]: https://nodejs.org
425
+ [build-badge]: https://img.shields.io/travis/com/testing-library/user-event.svg?style=flat-square
426
+ [build]: https://travis-ci.com/testing-library/user-event
427
+ [coverage-badge]: https://img.shields.io/codecov/c/github/testing-library/user-event.svg?style=flat-square
428
+ [coverage]: https://codecov.io/github/testing-library/user-event
429
+ [version-badge]: https://img.shields.io/npm/v/@testing-library/user-event.svg?style=flat-square
430
+ [package]: https://www.npmjs.com/package/@testing-library/user-event
431
+ [downloads-badge]: https://img.shields.io/npm/dm/@testing-library/user-event.svg?style=flat-square
432
+ [npmtrends]: http://www.npmtrends.com/@testing-library/user-event
433
+ [license-badge]: https://img.shields.io/npm/l/@testing-library/user-event.svg?style=flat-square
434
+ [license]: https://github.com/testing-library/user-event/blob/master/LICENSE
435
+ [prs-badge]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square
436
+ [prs]: http://makeapullrequest.com
437
+ [coc-badge]: https://img.shields.io/badge/code%20of-conduct-ff69b4.svg?style=flat-square
438
+ [coc]: https://github.com/testing-library/user-event/blob/master/other/CODE_OF_CONDUCT.md
439
+ [emojis]: https://github.com/all-contributors/all-contributors#emoji-key
440
+ [all-contributors]: https://github.com/all-contributors/all-contributors
441
+ [bugs]: https://github.com/testing-library/user-event/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+sort%3Acreated-desc+label%3Abug
442
+ [requests]: https://github.com/testing-library/user-event/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+sort%3Areactions-%2B1-desc+label%3Aenhancement
443
+ [good-first-issue]: https://github.com/testing-library/user-event/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+sort%3Areactions-%2B1-desc+label%3Aenhancement+label%3A%22good+first+issue%22
444
+ <!-- prettier-ignore-end -->