@testing-library/user-event 12.1.10 → 12.3.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/README.md CHANGED
@@ -21,10 +21,7 @@
21
21
  [![version][version-badge]][package]
22
22
  [![downloads][downloads-badge]][npmtrends]
23
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-64-orange.svg?style=flat-square)](#contributors-)
27
- <!-- ALL-CONTRIBUTORS-BADGE:END -->
24
+ [![All Contributors][all-contributors-badge]](#contributors)
28
25
  [![PRs Welcome][prs-badge]][prs]
29
26
  [![Code of Conduct][coc-badge]][coc]
30
27
  <!-- prettier-ignore-end -->
@@ -92,11 +89,16 @@ import userEvent from '@testing-library/user-event'
92
89
 
93
90
  // or
94
91
 
95
- var userEvent = require('@testing-library/user-event')
92
+ const {default: userEvent} = require('@testing-library/user-event')
96
93
  ```
97
94
 
98
95
  ## API
99
96
 
97
+ Note: All userEvent methods are synchronous with one exception: when `delay`
98
+ with `userEvent.type` as described below). We also discourage using `userEvent`
99
+ inside `before/after` blocks at all, for important reasons described in
100
+ ["Avoid Nesting When You're Testing"](https://kentcdodds.com/blog/avoid-nesting-when-youre-testing).
101
+
100
102
  ### `click(element, eventInit, options)`
101
103
 
102
104
  Clicks `element`, depending on what `element` is it can have different side
@@ -175,9 +177,9 @@ are typed. By default it's 0. You can use this option if your component has a
175
177
  different behavior for fast or slow users. If you do this, you need to make sure
176
178
  to `await`!
177
179
 
178
- > To be clear, `userEvent.type` *always* returns a promise, but you *only* need
179
- > to `await` the promise it returns if you're using the `delay` option. Otherwise
180
- > everything runs synchronously and you can ignore the promise.
180
+ > To be clear, `userEvent.type` _always_ returns a promise, but you _only_ need
181
+ > to `await` the promise it returns if you're using the `delay` option.
182
+ > Otherwise everything runs synchronously and you can ignore the promise.
181
183
 
182
184
  `type` will click the element before typing. To disable this, set the
183
185
  `skipClick` option to `true`.
@@ -186,18 +188,21 @@ to `await`!
186
188
 
187
189
  The following special character strings are supported:
188
190
 
189
- | Text string | Key | Modifier | Notes |
190
- | ------------- | --------- | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
191
- | `{enter}` | Enter | N/A | Will insert a newline character (`<textarea />` only). |
192
- | `{space}` | `' '` | N/A | |
193
- | `{esc}` | Escape | N/A | |
194
- | `{backspace}` | Backspace | N/A | Will delete the previous character (or the characters within the `selectedRange`, see example below). |
195
- | `{del}` | Delete | N/A | Will delete the next character (or the characters within the `selectedRange`, see example below) |
196
- | `{selectall}` | N/A | N/A | Selects all the text of the element. Note that this will only work for elements that support selection ranges (so, not `email`, `password`, `number`, among others) |
197
- | `{shift}` | Shift | `shiftKey` | Does **not** capitalize following characters. |
198
- | `{ctrl}` | Control | `ctrlKey` | |
199
- | `{alt}` | Alt | `altKey` | |
200
- | `{meta}` | OS | `metaKey` | |
191
+ | Text string | Key | Modifier | Notes |
192
+ | -------------- | ---------- | ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
193
+ | `{enter}` | Enter | N/A | Will insert a newline character (`<textarea />` only). |
194
+ | `{space}` | `' '` | N/A | |
195
+ | `{esc}` | Escape | N/A | |
196
+ | `{backspace}` | Backspace | N/A | Will delete the previous character (or the characters within the `selectedRange`, see example below). |
197
+ | `{del}` | Delete | N/A | Will delete the next character (or the characters within the `selectedRange`, see example below) |
198
+ | `{selectall}` | N/A | N/A | Selects all the text of the element. Note that this will only work for elements that support selection ranges (so, not `email`, `password`, `number`, among others) |
199
+ | `{arrowleft}` | ArrowLeft | N/A | |
200
+ | `{arrowright}` | ArrowRight | N/A | |
201
+ | `{shift}` | Shift | `shiftKey` | Does **not** capitalize following characters. |
202
+ | `{ctrl}` | Control | `ctrlKey` | |
203
+ | `{alt}` | Alt | `altKey` | |
204
+ | `{meta}` | OS | `metaKey` | |
205
+ | `{capslock}` | CapsLock | `modifierCapsLock` | Fires both keydown and keyup when used (simulates a user clicking their "Caps Lock" button to enable caps lock). |
201
206
 
202
207
  > **A note about modifiers:** Modifier keys (`{shift}`, `{ctrl}`, `{alt}`,
203
208
  > `{meta}`) will activate their corresponding event modifiers for the duration
@@ -595,6 +600,11 @@ Thanks goes to these people ([emoji key][emojis]):
595
600
  </tr>
596
601
  <tr>
597
602
  <td align="center"><a href="https://www.linkedin.com/in/luis-takahashi/"><img src="https://avatars0.githubusercontent.com/u/19766035?v=4" width="100px;" alt=""/><br /><sub><b>Luís Takahashi</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=luistak" title="Code">💻</a> <a href="https://github.com/testing-library/user-event/commits?author=luistak" title="Tests">⚠️</a></td>
603
+ <td align="center"><a href="https://github.com/jesujcastillom"><img src="https://avatars3.githubusercontent.com/u/7827281?v=4" width="100px;" alt=""/><br /><sub><b>Jesu Castillo</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=jesujcastillom" title="Code">💻</a> <a href="https://github.com/testing-library/user-event/commits?author=jesujcastillom" title="Tests">⚠️</a></td>
604
+ <td align="center"><a href="https://sarahdayan.dev"><img src="https://avatars1.githubusercontent.com/u/5370675?v=4" width="100px;" alt=""/><br /><sub><b>Sarah Dayan</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=sarahdayan" title="Documentation">📖</a></td>
605
+ <td align="center"><a href="http://saul-mirone.github.io/"><img src="https://avatars0.githubusercontent.com/u/10047788?v=4" width="100px;" alt=""/><br /><sub><b>Mirone</b></sub></a><br /><a href="https://github.com/testing-library/user-event/issues?q=author%3ASaul-Mirone" title="Bug reports">🐛</a></td>
606
+ <td align="center"><a href="https://github.com/amandapouget"><img src="https://avatars3.githubusercontent.com/u/12855692?v=4" width="100px;" alt=""/><br /><sub><b>Amanda Pouget</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=amandapouget" title="Documentation">📖</a></td>
607
+ <td align="center"><a href="https://github.com/Sonic12040"><img src="https://avatars3.githubusercontent.com/u/21055893?v=4" width="100px;" alt=""/><br /><sub><b>Sonic12040</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=Sonic12040" title="Code">💻</a> <a href="https://github.com/testing-library/user-event/commits?author=Sonic12040" title="Tests">⚠️</a> <a href="https://github.com/testing-library/user-event/commits?author=Sonic12040" title="Documentation">📖</a></td>
598
608
  </tr>
599
609
  </table>
600
610
 
@@ -612,8 +622,8 @@ MIT
612
622
  <!-- prettier-ignore-start -->
613
623
  [npm]: https://www.npmjs.com
614
624
  [node]: https://nodejs.org
615
- [build-badge]: https://img.shields.io/travis/testing-library/user-event.svg?style=flat-square
616
- [build]: https://travis-ci.org/testing-library/user-event
625
+ [build-badge]: https://img.shields.io/github/workflow/status/testing-library/user-event/validate/main?logo=github&style=flat-square
626
+ [build]: https://github.com/testing-library/user-event/actions?query=workflow%3Avalidate
617
627
  [coverage-badge]: https://img.shields.io/codecov/c/github/testing-library/user-event.svg?style=flat-square
618
628
  [coverage]: https://codecov.io/github/testing-library/user-event
619
629
  [version-badge]: https://img.shields.io/npm/v/@testing-library/user-event.svg?style=flat-square
@@ -628,6 +638,7 @@ MIT
628
638
  [coc]: https://github.com/testing-library/user-event/blob/master/other/CODE_OF_CONDUCT.md
629
639
  [emojis]: https://github.com/all-contributors/all-contributors#emoji-key
630
640
  [all-contributors]: https://github.com/all-contributors/all-contributors
641
+ [all-contributors-badge]: https://img.shields.io/github/all-contributors/testing-library/user-event?color=orange&style=flat-square
631
642
  [bugs]: https://github.com/testing-library/user-event/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+sort%3Acreated-desc+label%3Abug
632
643
  [requests]: https://github.com/testing-library/user-event/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+sort%3Areactions-%2B1-desc+label%3Aenhancement
633
644
  [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
package/dist/click.js CHANGED
@@ -88,7 +88,7 @@ function clickElement(element, init, {
88
88
  _dom.fireEvent.click(element, (0, _utils.getMouseEventOptions)('click', init, clickCount));
89
89
 
90
90
  const parentLabel = element.closest('label');
91
- if (parentLabel == null ? void 0 : parentLabel.control) (0, _focus.focus)(parentLabel.control, init);
91
+ if (parentLabel != null && parentLabel.control) (0, _focus.focus)(parentLabel.control, init);
92
92
  }
93
93
  }
94
94
 
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.navigationKey = navigationKey;
7
+
8
+ var _dom = require("@testing-library/dom");
9
+
10
+ var _utils = require("../utils");
11
+
12
+ const keys = {
13
+ ArrowLeft: {
14
+ keyCode: 37
15
+ },
16
+ ArrowRight: {
17
+ keyCode: 39
18
+ }
19
+ };
20
+
21
+ function getSelectionRange(currentElement, key) {
22
+ const {
23
+ selectionStart,
24
+ selectionEnd
25
+ } = currentElement();
26
+ const cursorChange = Number(key in keys) * (key === 'ArrowLeft' ? -1 : 1);
27
+ return {
28
+ selectionStart: selectionStart + cursorChange,
29
+ selectionEnd: selectionEnd + cursorChange
30
+ };
31
+ }
32
+
33
+ function navigationKey(key) {
34
+ const event = {
35
+ key,
36
+ keyCode: keys[key].keyCode,
37
+ which: keys[key].keyCode
38
+ };
39
+ return ({
40
+ currentElement,
41
+ eventOverrides
42
+ }) => {
43
+ _dom.fireEvent.keyDown(currentElement(), { ...event,
44
+ ...eventOverrides
45
+ });
46
+
47
+ const range = getSelectionRange(currentElement, key);
48
+ (0, _utils.setSelectionRangeIfNecessary)(currentElement(), range.selectionStart, range.selectionEnd);
49
+
50
+ _dom.fireEvent.keyUp(currentElement(), { ...event,
51
+ ...eventOverrides
52
+ });
53
+ };
54
+ }
package/dist/type.js CHANGED
@@ -11,6 +11,8 @@ var _utils = require("./utils");
11
11
 
12
12
  var _click = require("./click");
13
13
 
14
+ var _navigationKey = require("./keys/navigation-key");
15
+
14
16
  // TODO: wrap in asyncWrapper
15
17
  const modifierCallbackMap = { ...createModifierCallbackEntries({
16
18
  name: 'shift',
@@ -35,9 +37,68 @@ const modifierCallbackMap = { ...createModifierCallbackEntries({
35
37
  key: 'Meta',
36
38
  keyCode: 93,
37
39
  modifierProperty: 'metaKey'
38
- })
40
+ }),
41
+ // capslock is inline because of the need to fire both keydown and keyup on use, while preserving the modifier state.
42
+ '{capslock}': function ({
43
+ currentElement,
44
+ eventOverrides
45
+ }) {
46
+ const newEventOverrides = {
47
+ modifierCapsLock: true
48
+ };
49
+
50
+ _dom.fireEvent.keyDown(currentElement(), {
51
+ key: 'CapsLock',
52
+ keyCode: 20,
53
+ which: 20,
54
+ ...eventOverrides,
55
+ ...newEventOverrides
56
+ });
57
+
58
+ _dom.fireEvent.keyUp(currentElement(), {
59
+ key: 'CapsLock',
60
+ keyCode: 20,
61
+ which: 20,
62
+ ...eventOverrides,
63
+ ...newEventOverrides
64
+ });
65
+
66
+ return {
67
+ eventOverrides: newEventOverrides
68
+ };
69
+ },
70
+ '{/capslock}': function ({
71
+ currentElement,
72
+ eventOverrides
73
+ }) {
74
+ const newEventOverrides = {
75
+ modifierCapsLock: false
76
+ };
77
+
78
+ _dom.fireEvent.keyDown(currentElement(), {
79
+ key: 'CapsLock',
80
+ keyCode: 20,
81
+ which: 20,
82
+ ...eventOverrides,
83
+ ...newEventOverrides
84
+ });
85
+
86
+ _dom.fireEvent.keyUp(currentElement(), {
87
+ key: 'CapsLock',
88
+ keyCode: 20,
89
+ which: 20,
90
+ ...eventOverrides,
91
+ ...newEventOverrides
92
+ });
93
+
94
+ return {
95
+ eventOverrides: newEventOverrides
96
+ };
97
+ }
39
98
  };
40
99
  const specialCharCallbackMap = {
100
+ '{arrowleft}': (0, _navigationKey.navigationKey)('ArrowLeft'),
101
+ '{arrowright}': (0, _navigationKey.navigationKey)('ArrowRight'),
41
102
  '{enter}': handleEnter,
42
103
  '{esc}': handleEsc,
43
104
  '{del}': handleDel,
package/dist/utils.js CHANGED
@@ -95,7 +95,7 @@ function isLabelWithInternallyDisabledControl(element) {
95
95
  function getActiveElement(document) {
96
96
  const activeElement = document.activeElement;
97
97
 
98
- if (activeElement == null ? void 0 : activeElement.shadowRoot) {
98
+ if (activeElement != null && activeElement.shadowRoot) {
99
99
  return getActiveElement(activeElement.shadowRoot);
100
100
  } else {
101
101
  return activeElement;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@testing-library/user-event",
3
- "version": "12.1.10",
3
+ "version": "12.3.0",
4
4
  "description": "Fire events the same way the user does",
5
5
  "main": "dist/index.js",
6
6
  "typings": "typings/index.d.ts",
@@ -26,7 +26,7 @@
26
26
  "homepage": "https://github.com/testing-library/user-event#readme",
27
27
  "files": [
28
28
  "dist",
29
- "typings"
29
+ "typings/index.d.ts"
30
30
  ],
31
31
  "scripts": {
32
32
  "build": "kcd-scripts build",
@@ -35,7 +35,8 @@
35
35
  "test": "kcd-scripts test",
36
36
  "test:debug": "kcd-scripts --inspect-brk test --runInBand",
37
37
  "test:update": "npm test -- --updateSnapshot --coverage",
38
- "validate": "kcd-scripts validate"
38
+ "validate": "kcd-scripts validate",
39
+ "typecheck": "tsc --project typings"
39
40
  },
40
41
  "dependencies": {
41
42
  "@babel/runtime": "^7.10.2"
@@ -43,9 +44,11 @@
43
44
  "devDependencies": {
44
45
  "@testing-library/dom": "^7.21.4",
45
46
  "@testing-library/jest-dom": "^5.11.1",
47
+ "@types/estree": "0.0.45",
46
48
  "is-ci": "^2.0.0",
47
49
  "jest-serializer-ansi": "^1.0.3",
48
- "kcd-scripts": "^6.2.3"
50
+ "kcd-scripts": "^6.2.3",
51
+ "typescript": "^4.0.5"
49
52
  },
50
53
  "peerDependencies": {
51
54
  "@testing-library/dom": ">=7.21.4"
@@ -53,11 +53,11 @@ declare const userEvent: {
53
53
  files: FilesArgument,
54
54
  init?: UploadInitArgument,
55
55
  ) => void
56
- type: (
56
+ type: <T extends ITypeOpts>(
57
57
  element: TargetElement,
58
58
  text: string,
59
- userOpts?: ITypeOpts,
60
- ) => Promise<void>
59
+ userOpts?: T,
60
+ ) => T extends {delay: number} ? Promise<void> : void
61
61
  tab: (userOpts?: ITabUserOptions) => void
62
62
  paste: (
63
63
  element: TargetElement,