@testing-library/user-event 12.6.0 → 12.7.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
@@ -47,7 +47,6 @@ change the state of the checkbox.
47
47
  <!-- START doctoc generated TOC please keep comment here to allow auto update -->
48
48
  <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
49
49
 
50
-
51
50
  - [Installation](#installation)
52
51
  - [API](#api)
53
52
  - [`click(element, eventInit, options)`](#clickelement-eventinit-options)
@@ -120,7 +119,7 @@ test('click', () => {
120
119
  )
121
120
 
122
121
  userEvent.click(screen.getByText('Check'))
123
- expect(screen.getByLabelText('Check')).toHaveAttribute('checked', true)
122
+ expect(screen.getByLabelText('Check')).toBeChecked()
124
123
  })
125
124
  ```
126
125
 
@@ -149,8 +148,8 @@ import userEvent from '@testing-library/user-event'
149
148
 
150
149
  test('double click', () => {
151
150
  const onChange = jest.fn()
152
- render(<input type="checkbox" id="checkbox" onChange={onChange} />)
153
- const checkbox = screen.getByTestId('checkbox')
151
+ render(<input type="checkbox" onChange={onChange} />)
152
+ const checkbox = screen.getByRole('checkbox')
154
153
  userEvent.dblClick(checkbox)
155
154
  expect(onChange).toHaveBeenCalledTimes(2)
156
155
  expect(checkbox).not.toBeChecked()
@@ -202,6 +201,8 @@ The following special character strings are supported:
202
201
  | `{arrowright}` | ArrowRight | N/A | |
203
202
  | `{arrowup}` | ArrowUp | N/A | |
204
203
  | `{arrowdown}` | ArrowDown | N/A | |
204
+ | `{home}` | Home | N/A | |
205
+ | `{end}` | End | N/A | |
205
206
  | `{shift}` | Shift | `shiftKey` | Does **not** capitalize following characters. |
206
207
  | `{ctrl}` | Control | `ctrlKey` | |
207
208
  | `{alt}` | Alt | `altKey` | |
@@ -640,7 +641,7 @@ Thanks goes to these people ([emoji key][emojis]):
640
641
  <td align="center"><a href="https://github.com/nanivijay"><img src="https://avatars0.githubusercontent.com/u/5945591?v=4" width="100px;" alt=""/><br /><sub><b>Vijay Kumar Otti</b></sub></a><br /><a href="https://github.com/testing-library/user-event/issues?q=author%3Ananivijay" title="Bug reports">🐛</a></td>
641
642
  <td align="center"><a href="http://tompicton.com"><img src="https://avatars2.githubusercontent.com/u/12588098?v=4" width="100px;" alt=""/><br /><sub><b>Tom Picton</b></sub></a><br /><a href="https://github.com/testing-library/user-event/issues?q=author%3Atpict" title="Bug reports">🐛</a> <a href="https://github.com/testing-library/user-event/commits?author=tpict" title="Code">💻</a> <a href="https://github.com/testing-library/user-event/commits?author=tpict" title="Tests">⚠️</a></td>
642
643
  <td align="center"><a href="https://hung.dev"><img src="https://avatars3.githubusercontent.com/u/8603085?v=4" width="100px;" alt=""/><br /><sub><b>Hung Viet Nguyen</b></sub></a><br /><a href="https://github.com/testing-library/user-event/issues?q=author%3Anvh95" title="Bug reports">🐛</a></td>
643
- <td align="center"><a href="https://nickmccurdy.com/"><img src="https://avatars0.githubusercontent.com/u/927220?v=4" width="100px;" alt=""/><br /><sub><b>Nick McCurdy</b></sub></a><br /><a href="#projectManagement-nickmccurdy" title="Project Management">📆</a> <a href="#question-nickmccurdy" title="Answering Questions">💬</a> <a href="https://github.com/testing-library/user-event/commits?author=nickmccurdy" title="Code">💻</a> <a href="https://github.com/testing-library/user-event/commits?author=nickmccurdy" title="Tests">⚠️</a></td>
644
+ <td align="center"><a href="https://nickmccurdy.com/"><img src="https://avatars0.githubusercontent.com/u/927220?v=4" width="100px;" alt=""/><br /><sub><b>Nick McCurdy</b></sub></a><br /><a href="#projectManagement-nickmccurdy" title="Project Management">📆</a> <a href="#question-nickmccurdy" title="Answering Questions">💬</a> <a href="https://github.com/testing-library/user-event/commits?author=nickmccurdy" title="Code">💻</a> <a href="https://github.com/testing-library/user-event/commits?author=nickmccurdy" title="Tests">⚠️</a> <a href="https://github.com/testing-library/user-event/commits?author=nickmccurdy" title="Documentation">📖</a></td>
644
645
  </tr>
645
646
  <tr>
646
647
  <td align="center"><a href="http://timdeschryver.dev"><img src="https://avatars1.githubusercontent.com/u/28659384?v=4" width="100px;" alt=""/><br /><sub><b>Tim Deschryver</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=timdeschryver" title="Tests">⚠️</a></td>
@@ -680,11 +681,13 @@ Thanks goes to these people ([emoji key][emojis]):
680
681
  </tr>
681
682
  <tr>
682
683
  <td align="center"><a href="https://github.com/vasilii-kovalev"><img src="https://avatars0.githubusercontent.com/u/10310491?v=4" width="100px;" alt=""/><br /><sub><b>Vasilii Kovalev</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=vasilii-kovalev" title="Code">💻</a> <a href="https://github.com/testing-library/user-event/commits?author=vasilii-kovalev" title="Documentation">📖</a></td>
684
+ <td align="center"><a href="https://www.daleseo.com"><img src="https://avatars1.githubusercontent.com/u/5466341?v=4" width="100px;" alt=""/><br /><sub><b>Dale Seo</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=daleseo" title="Documentation">📖</a></td>
683
685
  </tr>
684
686
  </table>
685
687
 
686
688
  <!-- markdownlint-enable -->
687
689
  <!-- prettier-ignore-end -->
690
+
688
691
  <!-- ALL-CONTRIBUTORS-LIST:END -->
689
692
 
690
693
  This project follows the [all-contributors][all-contributors] specification.
@@ -10,6 +10,12 @@ var _dom = require("@testing-library/dom");
10
10
  var _utils = require("../utils");
11
11
 
12
12
  const keys = {
13
+ Home: {
14
+ keyCode: 36
15
+ },
16
+ End: {
17
+ keyCode: 35
18
+ },
13
19
  ArrowLeft: {
14
20
  keyCode: 37
15
21
  },
@@ -23,6 +29,21 @@ function getSelectionRange(currentElement, key) {
23
29
  selectionStart,
24
30
  selectionEnd
25
31
  } = currentElement();
32
+
33
+ if (key === 'Home') {
34
+ return {
35
+ selectionStart: 0,
36
+ selectionEnd: 0
37
+ };
38
+ }
39
+
40
+ if (key === 'End') {
41
+ return {
42
+ selectionStart: selectionEnd + 1,
43
+ selectionEnd: selectionEnd + 1
44
+ };
45
+ }
46
+
26
47
  const cursorChange = Number(key in keys) * (key === 'ArrowLeft' ? -1 : 1);
27
48
  return {
28
49
  selectionStart: selectionStart + cursorChange,
@@ -7,6 +7,8 @@ exports.deselectOptions = exports.selectOptions = void 0;
7
7
 
8
8
  var _dom = require("@testing-library/dom");
9
9
 
10
+ var _utils = require("./utils");
11
+
10
12
  var _click = require("./click");
11
13
 
12
14
  var _focus = require("./focus");
@@ -35,59 +37,78 @@ function selectOptionsBase(newValue, select, values, init) {
35
37
  }).filter(option => !option.disabled);
36
38
  if (select.disabled || !selectedOptions.length) return;
37
39
 
38
- if (select.multiple) {
39
- for (const option of selectedOptions) {
40
- // events fired for multiple select are weird. Can't use hover...
41
- _dom.fireEvent.pointerOver(option, init);
40
+ if ((0, _utils.isInstanceOfElement)(select, 'HTMLSelectElement')) {
41
+ if (select.multiple) {
42
+ for (const option of selectedOptions) {
43
+ // events fired for multiple select are weird. Can't use hover...
44
+ _dom.fireEvent.pointerOver(option, init);
42
45
 
43
- _dom.fireEvent.pointerEnter(select, init);
46
+ _dom.fireEvent.pointerEnter(select, init);
44
47
 
45
- _dom.fireEvent.mouseOver(option);
48
+ _dom.fireEvent.mouseOver(option);
46
49
 
47
- _dom.fireEvent.mouseEnter(select);
50
+ _dom.fireEvent.mouseEnter(select);
51
+
52
+ _dom.fireEvent.pointerMove(option, init);
53
+
54
+ _dom.fireEvent.mouseMove(option, init);
55
+
56
+ _dom.fireEvent.pointerDown(option, init);
48
57
 
49
- _dom.fireEvent.pointerMove(option, init);
58
+ _dom.fireEvent.mouseDown(option, init);
50
59
 
51
- _dom.fireEvent.mouseMove(option, init);
60
+ (0, _focus.focus)(select, init);
61
+
62
+ _dom.fireEvent.pointerUp(option, init);
63
+
64
+ _dom.fireEvent.mouseUp(option, init);
65
+
66
+ selectOption(option);
67
+
68
+ _dom.fireEvent.click(option, init);
69
+ }
70
+ } else if (selectedOptions.length === 1) {
71
+ // the click to open the select options
72
+ (0, _click.click)(select, init);
73
+ selectOption(selectedOptions[0]); // the browser triggers another click event on the select for the click on the option
74
+ // this second click has no 'down' phase
52
75
 
53
- _dom.fireEvent.pointerDown(option, init);
76
+ _dom.fireEvent.pointerOver(select, init);
54
77
 
55
- _dom.fireEvent.mouseDown(option, init);
78
+ _dom.fireEvent.pointerEnter(select, init);
56
79
 
57
- (0, _focus.focus)(select, init);
80
+ _dom.fireEvent.mouseOver(select);
58
81
 
59
- _dom.fireEvent.pointerUp(option, init);
82
+ _dom.fireEvent.mouseEnter(select);
60
83
 
61
- _dom.fireEvent.mouseUp(option, init);
84
+ _dom.fireEvent.pointerUp(select, init);
62
85
 
63
- selectOption(option);
86
+ _dom.fireEvent.mouseUp(select, init);
64
87
 
65
- _dom.fireEvent.click(option, init);
88
+ _dom.fireEvent.click(select, init);
89
+ } else {
90
+ throw (0, _dom.getConfig)().getElementError(`Cannot select multiple options on a non-multiple select`, select);
66
91
  }
67
- } else if (selectedOptions.length === 1) {
68
- (0, _click.click)(select, init);
69
- selectOption(selectedOptions[0]);
92
+ } else if (select.getAttribute('role') === 'listbox') {
93
+ selectedOptions.forEach(option => {
94
+ (0, _hover.hover)(option, init);
95
+ (0, _click.click)(option, init);
96
+ (0, _hover.unhover)(option, init);
97
+ });
70
98
  } else {
71
- throw (0, _dom.getConfig)().getElementError(`Cannot select multiple options on a non-multiple select`, select);
99
+ throw (0, _dom.getConfig)().getElementError(`Cannot select options on elements that are neither select nor listbox elements`, select);
72
100
  }
73
101
 
74
102
  function selectOption(option) {
75
- if (option.getAttribute('role') === 'option') {
76
- option == null ? void 0 : option.setAttribute == null ? void 0 : option.setAttribute('aria-selected', newValue);
77
- (0, _hover.hover)(option, init);
78
- (0, _click.click)(option, init);
79
- (0, _hover.unhover)(option, init);
80
- } else {
81
- option.selected = newValue;
82
- (0, _dom.fireEvent)(select, (0, _dom.createEvent)('input', select, {
83
- bubbles: true,
84
- cancelable: false,
85
- composed: true,
86
- ...init
87
- }));
88
-
89
- _dom.fireEvent.change(select, init);
90
- }
103
+ option.selected = newValue;
104
+ (0, _dom.fireEvent)(select, (0, _dom.createEvent)('input', select, {
105
+ bubbles: true,
106
+ cancelable: false,
107
+ composed: true,
108
+ ...init
109
+ }));
110
+
111
+ _dom.fireEvent.change(select, init);
91
112
  }
92
113
  }
93
114
 
package/dist/type.js CHANGED
@@ -40,7 +40,7 @@ const modifierCallbackMap = { ...createModifierCallbackEntries({
40
40
  modifierProperty: 'metaKey'
41
41
  }),
42
42
  // capslock is inline because of the need to fire both keydown and keyup on use, while preserving the modifier state.
43
- '{capslock}': function ({
43
+ '{capslock}': function capslockOn({
44
44
  currentElement,
45
45
  eventOverrides
46
46
  }) {
@@ -68,7 +68,7 @@ const modifierCallbackMap = { ...createModifierCallbackEntries({
68
68
  eventOverrides: newEventOverrides
69
69
  };
70
70
  },
71
- '{/capslock}': function ({
71
+ '{/capslock}': function capslockOff({
72
72
  currentElement,
73
73
  eventOverrides
74
74
  }) {
@@ -106,6 +106,8 @@ const specialCharMap = {
106
106
  escape: '{esc}',
107
107
  delete: '{del}',
108
108
  backspace: '{backspace}',
109
+ home: '{home}',
110
+ end: '{end}',
109
111
  selectAll: '{selectall}',
110
112
  space: '{space}',
111
113
  whitespace: ' '
@@ -116,6 +118,8 @@ const specialCharCallbackMap = {
116
118
  [specialCharMap.arrowRight]: (0, _navigationKey.navigationKey)('ArrowRight'),
117
119
  [specialCharMap.arrowDown]: handleArrowDown,
118
120
  [specialCharMap.arrowUp]: handleArrowUp,
121
+ [specialCharMap.home]: (0, _navigationKey.navigationKey)('Home'),
122
+ [specialCharMap.end]: (0, _navigationKey.navigationKey)('End'),
119
123
  [specialCharMap.enter]: handleEnter,
120
124
  [specialCharMap.escape]: handleEsc,
121
125
  [specialCharMap.delete]: handleDel,
@@ -195,7 +199,10 @@ async function typeImpl(element, text, {
195
199
  (0, _utils.setSelectionRangeIfNecessary)(currentElement(), initialSelectionStart != null ? initialSelectionStart : value.length, initialSelectionEnd != null ? initialSelectionEnd : value.length);
196
200
  }
197
201
 
198
- const eventCallbacks = function () {
202
+ const eventCallbacks = queueCallbacks();
203
+ await runCallbacks(eventCallbacks);
204
+
205
+ function queueCallbacks() {
199
206
  const callbacks = [];
200
207
  let remainingString = text;
201
208
 
@@ -209,9 +216,9 @@ async function typeImpl(element, text, {
209
216
  }
210
217
 
211
218
  return callbacks;
212
- }();
219
+ }
213
220
 
214
- await async function (callbacks) {
221
+ async function runCallbacks(callbacks) {
215
222
  const eventOverrides = {};
216
223
  let prevWasMinus, prevWasPeriod, prevValue, typedValue;
217
224
 
@@ -234,7 +241,7 @@ async function typeImpl(element, text, {
234
241
  typedValue = returnValue == null ? void 0 : returnValue.typedValue;
235
242
  }
236
243
  }
237
- }(eventCallbacks);
244
+ }
238
245
  }
239
246
 
240
247
  function getNextCallback(remainingString, skipAutoClose) {
@@ -290,8 +297,11 @@ function getSpecialCharCallback(remainingString) {
290
297
 
291
298
  function getTypeCallback(remainingString) {
292
299
  const character = remainingString[0];
300
+
301
+ const callback = context => typeCharacter(character, context);
302
+
293
303
  return {
294
- callback: context => typeCharacter(character, context),
304
+ callback,
295
305
  remainingString: remainingString.slice(1)
296
306
  };
297
307
  }
@@ -557,6 +567,7 @@ function createModifierCallbackEntries({
557
567
  keyCode,
558
568
  modifierProperty
559
569
  }) {
570
+ const openName = `{${name}}`;
560
571
  const closeName = `{/${name}}`;
561
572
 
562
573
  function open({
@@ -581,28 +592,31 @@ function createModifierCallbackEntries({
581
592
  }
582
593
 
583
594
  open.closeName = closeName;
584
- return {
585
- [`{${name}}`]: open,
586
- [closeName]: function ({
587
- currentElement,
588
- eventOverrides
589
- }) {
590
- const newEventOverrides = {
591
- [modifierProperty]: false
592
- };
593
-
594
- _dom.fireEvent.keyUp(currentElement(), {
595
- key,
596
- keyCode,
597
- which: keyCode,
598
- ...eventOverrides,
599
- ...newEventOverrides
600
- });
601
595
 
602
- return {
603
- eventOverrides: newEventOverrides
604
- };
605
- }
596
+ function close({
597
+ currentElement,
598
+ eventOverrides
599
+ }) {
600
+ const newEventOverrides = {
601
+ [modifierProperty]: false
602
+ };
603
+
604
+ _dom.fireEvent.keyUp(currentElement(), {
605
+ key,
606
+ keyCode,
607
+ which: keyCode,
608
+ ...eventOverrides,
609
+ ...newEventOverrides
610
+ });
611
+
612
+ return {
613
+ eventOverrides: newEventOverrides
614
+ };
615
+ }
616
+
617
+ return {
618
+ [openName]: open,
619
+ [closeName]: close
606
620
  };
607
621
  }
608
622
 
package/dist/upload.js CHANGED
@@ -15,17 +15,9 @@ var _focus = require("./focus");
15
15
 
16
16
  function upload(element, fileOrFiles, init) {
17
17
  if (element.disabled) return;
18
- let files;
19
- let input = element;
20
18
  (0, _click.click)(element, init);
21
-
22
- if (element.tagName === 'LABEL') {
23
- files = element.control.multiple ? fileOrFiles : [fileOrFiles];
24
- input = element.control;
25
- } else {
26
- files = element.multiple ? fileOrFiles : [fileOrFiles];
27
- } // blur fires when the file selector pops up
28
-
19
+ const input = element.tagName === 'LABEL' ? element.control : element;
20
+ const files = (Array.isArray(fileOrFiles) ? fileOrFiles : [fileOrFiles]).slice(0, input.multiple ? undefined : 1); // blur fires when the file selector pops up
29
21
 
30
22
  (0, _blur.blur)(element, init); // focus fires when they make their selection
31
23
 
package/dist/utils.js CHANGED
@@ -17,10 +17,42 @@ exports.buildTimeValue = buildTimeValue;
17
17
  exports.getValue = getValue;
18
18
  exports.getSelectionRange = getSelectionRange;
19
19
  exports.isContentEditable = isContentEditable;
20
+ exports.isInstanceOfElement = isInstanceOfElement;
20
21
  exports.FOCUSABLE_SELECTOR = void 0;
21
22
 
22
23
  var _dom = require("@testing-library/dom");
23
24
 
25
+ var _helpers = require("@testing-library/dom/dist/helpers");
26
+
27
+ // isInstanceOfElement can be removed once the peerDependency for @testing-library/dom is bumped to a version that includes https://github.com/testing-library/dom-testing-library/pull/885
28
+
29
+ /**
30
+ * Check if an element is of a given type.
31
+ *
32
+ * @param Element The element to test
33
+ * @param string Constructor name. E.g. 'HTMLSelectElement'
34
+ */
35
+ function isInstanceOfElement(element, elementType) {
36
+ try {
37
+ const window = (0, _helpers.getWindowFromNode)(element); // Window usually has the element constructors as properties but is not required to do so per specs
38
+
39
+ if (typeof window[elementType] === 'function') {
40
+ return element instanceof window[elementType];
41
+ }
42
+ } catch (e) {// The document might not be associated with a window
43
+ } // Fall back to the constructor name as workaround for test environments that
44
+ // a) not associate the document with a window
45
+ // b) not provide the constructor as property of window
46
+
47
+
48
+ if (/^HTML(\w+)Element$/.test(element.constructor.name)) {
49
+ return element.constructor.name === elementType;
50
+ } // The user passed some node that is not created in a browser-like environment
51
+
52
+
53
+ throw new Error(`Unable to verify if element is instance of ${elementType}. Please file an issue describing your test environment: https://github.com/testing-library/dom-testing-library/issues/new`);
54
+ }
55
+
24
56
  function isMousePressEvent(event) {
25
57
  return event === 'mousedown' || event === 'mouseup' || event === 'click' || event === 'dblclick';
26
58
  }
@@ -243,7 +275,7 @@ function isFocusable(element) {
243
275
  const CLICKABLE_INPUT_TYPES = ['button', 'color', 'file', 'image', 'reset', 'submit'];
244
276
 
245
277
  function isClickable(element) {
246
- return element.tagName === 'BUTTON' || element instanceof element.ownerDocument.defaultView.HTMLInputElement && CLICKABLE_INPUT_TYPES.includes(element.type);
278
+ return element.tagName === 'BUTTON' || isInstanceOfElement(element, 'HTMLInputElement') && CLICKABLE_INPUT_TYPES.includes(element.type);
247
279
  }
248
280
 
249
281
  function eventWrapper(cb) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@testing-library/user-event",
3
- "version": "12.6.0",
3
+ "version": "12.7.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",