@testing-library/user-event 12.0.17 → 12.1.3

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
@@ -23,7 +23,7 @@
23
23
  [![MIT License][license-badge]][license]
24
24
 
25
25
  <!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
26
- [![All Contributors](https://img.shields.io/badge/all_contributors-56-orange.svg?style=flat-square)](#contributors-)
26
+ [![All Contributors](https://img.shields.io/badge/all_contributors-58-orange.svg?style=flat-square)](#contributors-)
27
27
  <!-- ALL-CONTRIBUTORS-BADGE:END -->
28
28
  [![PRs Welcome][prs-badge]][prs]
29
29
  [![Code of Conduct][coc-badge]][coc]
@@ -175,6 +175,10 @@ are typed. By default it's 0. You can use this option if your component has a
175
175
  different behavior for fast or slow users. If you do this, you need to make sure
176
176
  to `await`!
177
177
 
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.
181
+
178
182
  `type` will click the element before typing. To disable this, set the
179
183
  `skipClick` option to `true`.
180
184
 
@@ -580,6 +584,10 @@ Thanks goes to these people ([emoji key][emojis]):
580
584
  <td align="center"><a href="https://ko-fi.com/thislucas"><img src="https://avatars0.githubusercontent.com/u/8645841?v=4" width="100px;" alt=""/><br /><sub><b>Lucas Bernalte</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=lucbpz" title="Documentation">📖</a></td>
581
585
  <td align="center"><a href="https://github.com/maxnewlands"><img src="https://avatars3.githubusercontent.com/u/1304166?v=4" width="100px;" alt=""/><br /><sub><b>Maxwell Newlands</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=maxnewlands" title="Code">💻</a> <a href="https://github.com/testing-library/user-event/commits?author=maxnewlands" title="Tests">⚠️</a></td>
582
586
  </tr>
587
+ <tr>
588
+ <td align="center"><a href="https://github.com/ph-fritsche"><img src="https://avatars3.githubusercontent.com/u/39068198?v=4" width="100px;" alt=""/><br /><sub><b>ph-fritsche</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=ph-fritsche" title="Code">💻</a> <a href="https://github.com/testing-library/user-event/commits?author=ph-fritsche" title="Tests">⚠️</a></td>
589
+ <td align="center"><a href="https://github.com/reywright"><img src="https://avatars3.githubusercontent.com/u/708820?v=4" width="100px;" alt=""/><br /><sub><b>Rey Wright</b></sub></a><br /><a href="https://github.com/testing-library/user-event/issues?q=author%3Areywright" title="Bug reports">🐛</a> <a href="https://github.com/testing-library/user-event/commits?author=reywright" title="Code">💻</a></td>
590
+ </tr>
583
591
  </table>
584
592
 
585
593
  <!-- markdownlint-enable -->
package/dist/click.js CHANGED
@@ -71,11 +71,12 @@ function clickElement(element, init, {
71
71
  if (!element.disabled) {
72
72
  const continueDefaultHandling = _dom.fireEvent.mouseDown(element, (0, _utils.getMouseEventOptions)('mousedown', init, clickCount));
73
73
 
74
- const shouldFocus = element.ownerDocument.activeElement !== element;
75
-
76
- if (continueDefaultHandling) {
77
- if (previousElement) (0, _blur.blur)(previousElement, init);
78
- if (shouldFocus) (0, _focus.focus)(element, init);
74
+ if (continueDefaultHandling && element !== element.ownerDocument.activeElement) {
75
+ if (previousElement && !(0, _utils.isFocusable)(element)) {
76
+ (0, _blur.blur)(previousElement, init);
77
+ } else {
78
+ (0, _focus.focus)(element, init);
79
+ }
79
80
  }
80
81
  }
81
82
 
package/dist/tab.js CHANGED
@@ -94,16 +94,16 @@ function tab({
94
94
  });
95
95
  continueToTab = _dom.fireEvent.keyDown(previousElement, { ...tabKeyInit
96
96
  });
97
-
98
- if (continueToTab) {
99
- (0, _blur.blur)(previousElement);
100
- }
101
97
  }
102
98
 
103
99
  const keyUpTarget = !continueToTab && previousElement ? previousElement : nextElement;
104
100
 
105
101
  if (continueToTab) {
106
- (0, _focus.focus)(nextElement);
102
+ if (nextElement === document.body) {
103
+ (0, _blur.blur)(previousElement);
104
+ } else {
105
+ (0, _focus.focus)(nextElement);
106
+ }
107
107
  }
108
108
 
109
109
  _dom.fireEvent.keyUp(keyUpTarget, { ...tabKeyInit
package/dist/type.js CHANGED
@@ -87,7 +87,15 @@ async function typeImpl(element, text, {
87
87
  initialSelectionEnd
88
88
  }) {
89
89
  if (element.disabled) return;
90
- if (!skipClick) (0, _click.click)(element); // The focused element could change between each event, so get the currently active element each time
90
+ if (!skipClick) (0, _click.click)(element);
91
+
92
+ if ((0, _utils.isContentEditable)(element) && document.getSelection().rangeCount === 0) {
93
+ const range = document.createRange();
94
+ range.setStart(element, 0);
95
+ range.setEnd(element, 0);
96
+ document.getSelection().addRange(range);
97
+ } // The focused element could change between each event, so get the currently active element each time
98
+
91
99
 
92
100
  const currentElement = () => (0, _utils.getActiveElement)(element.ownerDocument); // by default, a new element has it's selection start and end at 0
93
101
  // but most of the time when people call "type", they expect it to type
@@ -99,9 +107,13 @@ async function typeImpl(element, text, {
99
107
  // explicitely start typing with the cursor at 0. Not super common.
100
108
 
101
109
 
102
- const value = currentElement().value;
110
+ const value = (0, _utils.getValue)(currentElement());
111
+ const {
112
+ selectionStart,
113
+ selectionEnd
114
+ } = (0, _utils.getSelectionRange)(element);
103
115
 
104
- if (value != null && currentElement().selectionStart === 0 && currentElement().selectionEnd === 0) {
116
+ if (value != null && selectionStart === 0 && selectionEnd === 0) {
105
117
  (0, _utils.setSelectionRangeIfNecessary)(currentElement(), initialSelectionStart != null ? initialSelectionStart : value.length, initialSelectionEnd != null ? initialSelectionEnd : value.length);
106
118
  }
107
119
 
@@ -217,7 +229,7 @@ function setSelectionRange({
217
229
  // The reason we have to do this at all is because it actually *is*
218
230
  // programmatically changed by fireEvent.input, so we have to simulate the
219
231
  // browser's default behavior
220
- const value = currentElement().value;
232
+ const value = (0, _utils.getValue)(currentElement());
221
233
 
222
234
  if (value === newValue) {
223
235
  (0, _utils.setSelectionRangeIfNecessary)(currentElement(), newSelectionStart, newSelectionStart);
@@ -235,15 +247,24 @@ function fireInputEventIfNeeded({
235
247
  newSelectionStart,
236
248
  eventOverrides
237
249
  }) {
238
- const prevValue = currentElement().value;
250
+ const prevValue = (0, _utils.getValue)(currentElement());
239
251
 
240
252
  if (!currentElement().readOnly && !(0, _utils.isClickable)(currentElement()) && newValue !== prevValue) {
241
- _dom.fireEvent.input(currentElement(), {
242
- target: {
243
- value: newValue
244
- },
245
- ...eventOverrides
246
- });
253
+ if ((0, _utils.isContentEditable)(currentElement())) {
254
+ _dom.fireEvent.input(currentElement(), {
255
+ target: {
256
+ textContent: newValue
257
+ },
258
+ ...eventOverrides
259
+ });
260
+ } else {
261
+ _dom.fireEvent.input(currentElement(), {
262
+ target: {
263
+ value: newValue
264
+ },
265
+ ...eventOverrides
266
+ });
267
+ }
247
268
 
248
269
  setSelectionRange({
249
270
  currentElement,
@@ -286,7 +307,7 @@ function typeCharacter(char, {
286
307
  ...eventOverrides
287
308
  });
288
309
 
289
- if (currentElement().value != null && keyPressDefaultNotPrevented) {
310
+ if ((0, _utils.getValue)(currentElement()) != null && keyPressDefaultNotPrevented) {
290
311
  let newEntry = char;
291
312
 
292
313
  if (prevWasMinus) {
@@ -324,7 +345,7 @@ function typeCharacter(char, {
324
345
 
325
346
 
326
347
  if (currentElement().type === 'number') {
327
- const newValue = currentElement().value;
348
+ const newValue = (0, _utils.getValue)(currentElement());
328
349
 
329
350
  if (newValue === prevValue && newEntry !== '-') {
330
351
  nextPrevWasMinus = prevWasMinus;
@@ -363,9 +384,9 @@ function typeCharacter(char, {
363
384
  function calculateNewBackspaceValue(element) {
364
385
  const {
365
386
  selectionStart,
366
- selectionEnd,
367
- value
368
- } = element;
387
+ selectionEnd
388
+ } = (0, _utils.getSelectionRange)(element);
389
+ const value = (0, _utils.getValue)(element);
369
390
  let newValue, newSelectionStart;
370
391
 
371
392
  if (selectionStart === null) {
@@ -403,9 +424,9 @@ function calculateNewBackspaceValue(element) {
403
424
  function calculateNewDeleteValue(element) {
404
425
  const {
405
426
  selectionStart,
406
- selectionEnd,
407
- value
408
- } = element;
427
+ selectionEnd
428
+ } = (0, _utils.getSelectionRange)(element);
429
+ const value = (0, _utils.getValue)(element);
409
430
  let newValue;
410
431
 
411
432
  if (selectionStart === null) {
@@ -539,7 +560,7 @@ function handleEnter({
539
560
  });
540
561
  }
541
562
 
542
- if (currentElement().tagName === 'INPUT' && currentElement().form) {
563
+ if (currentElement().tagName === 'INPUT' && currentElement().form && (currentElement().form.querySelectorAll('input').length === 1 || currentElement().form.querySelector('input[type="submit"]') || currentElement().form.querySelector('button[type="submit"]'))) {
543
564
  _dom.fireEvent.submit(currentElement().form);
544
565
  }
545
566
 
@@ -641,9 +662,7 @@ function handleBackspace({
641
662
  function handleSelectall({
642
663
  currentElement
643
664
  }) {
644
- // the user can actually select in several different ways
645
- // we're not going to choose, so we'll *only* set the selection range
646
- currentElement().setSelectionRange(0, currentElement().value.length);
665
+ currentElement().setSelectionRange(0, (0, _utils.getValue)(currentElement()).length);
647
666
  }
648
667
 
649
668
  function handleSpace(context) {
package/dist/utils.js CHANGED
@@ -12,6 +12,9 @@ exports.calculateNewValue = calculateNewValue;
12
12
  exports.setSelectionRangeIfNecessary = setSelectionRangeIfNecessary;
13
13
  exports.eventWrapper = eventWrapper;
14
14
  exports.isValidDateValue = isValidDateValue;
15
+ exports.getValue = getValue;
16
+ exports.getSelectionRange = getSelectionRange;
17
+ exports.isContentEditable = isContentEditable;
15
18
  exports.FOCUSABLE_SELECTOR = void 0;
16
19
 
17
20
  var _dom = require("@testing-library/dom");
@@ -113,14 +116,42 @@ function supportsMaxLength(element) {
113
116
  return false;
114
117
  }
115
118
 
119
+ function getSelectionRange(element) {
120
+ if (isContentEditable(element)) {
121
+ const range = element.ownerDocument.getSelection().getRangeAt(0);
122
+ return {
123
+ selectionStart: range.startOffset,
124
+ selectionEnd: range.endOffset
125
+ };
126
+ }
127
+
128
+ return {
129
+ selectionStart: element.selectionStart,
130
+ selectionEnd: element.selectionEnd
131
+ };
132
+ } //jsdom is not supporting isContentEditable
133
+
134
+
135
+ function isContentEditable(element) {
136
+ return element.hasAttribute('contenteditable') && (element.getAttribute('contenteditable') == 'true' || element.getAttribute('contenteditable') == '');
137
+ }
138
+
139
+ function getValue(element) {
140
+ if (isContentEditable(element)) {
141
+ return element.textContent;
142
+ }
143
+
144
+ return element.value;
145
+ }
146
+
116
147
  function calculateNewValue(newEntry, element) {
117
148
  var _element$getAttribute;
118
149
 
119
150
  const {
120
151
  selectionStart,
121
- selectionEnd,
122
- value
123
- } = element; // can't use .maxLength property because of a jsdom bug:
152
+ selectionEnd
153
+ } = getSelectionRange(element);
154
+ const value = getValue(element); // can't use .maxLength property because of a jsdom bug:
124
155
  // https://github.com/jsdom/jsdom/issues/2927
125
156
 
126
157
  const maxLength = Number((_element$getAttribute = element.getAttribute('maxlength')) != null ? _element$getAttribute : -1);
@@ -171,19 +202,28 @@ function setSelectionRangeIfNecessary(element, newSelectionStart, newSelectionEn
171
202
  const {
172
203
  selectionStart,
173
204
  selectionEnd
174
- } = element;
205
+ } = getSelectionRange(element);
175
206
 
176
- if (!element.setSelectionRange || selectionStart === null) {
207
+ if (!isContentEditable(element) && (!element.setSelectionRange || selectionStart === null)) {
177
208
  // cannot set selection
178
209
  return;
179
210
  }
180
211
 
181
212
  if (selectionStart !== newSelectionStart || selectionEnd !== newSelectionStart) {
182
- element.setSelectionRange(newSelectionStart, newSelectionEnd);
213
+ if (isContentEditable(element)) {
214
+ const range = element.ownerDocument.createRange();
215
+ range.selectNodeContents(element);
216
+ range.setStart(element.firstChild, newSelectionStart);
217
+ range.setEnd(element.firstChild, newSelectionEnd);
218
+ element.ownerDocument.getSelection().removeAllRanges();
219
+ element.ownerDocument.getSelection().addRange(range);
220
+ } else {
221
+ element.setSelectionRange(newSelectionStart, newSelectionEnd);
222
+ }
183
223
  }
184
224
  }
185
225
 
186
- const FOCUSABLE_SELECTOR = ['input:not([disabled])', 'button:not([disabled])', 'select:not([disabled])', 'textarea:not([disabled])', 'a[href]', '[tabindex]:not([disabled])'].join(', ');
226
+ const FOCUSABLE_SELECTOR = ['input:not([disabled])', 'button:not([disabled])', 'select:not([disabled])', 'textarea:not([disabled])', '[contenteditable=""]', '[contenteditable="true"]', 'a[href]', '[tabindex]:not([disabled])'].join(', ');
187
227
  exports.FOCUSABLE_SELECTOR = FOCUSABLE_SELECTOR;
188
228
 
189
229
  function isFocusable(element) {
@@ -193,7 +233,7 @@ function isFocusable(element) {
193
233
  const CLICKABLE_INPUT_TYPES = ['button', 'color', 'file', 'image', 'reset', 'submit'];
194
234
 
195
235
  function isClickable(element) {
196
- return element.tagName === 'BUTTON' || element instanceof HTMLInputElement && CLICKABLE_INPUT_TYPES.includes(element.type);
236
+ return element.tagName === 'BUTTON' || element instanceof element.ownerDocument.defaultView.HTMLInputElement && CLICKABLE_INPUT_TYPES.includes(element.type);
197
237
  }
198
238
 
199
239
  function eventWrapper(cb) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@testing-library/user-event",
3
- "version": "12.0.17",
3
+ "version": "12.1.3",
4
4
  "description": "Fire events the same way the user does",
5
5
  "main": "dist/index.js",
6
6
  "typings": "typings/index.d.ts",