@testing-library/user-event 14.0.0-alpha.1 → 14.0.0-alpha.5

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.
Files changed (135) hide show
  1. package/dist/{src/clear.d.ts → clear.d.ts} +0 -0
  2. package/dist/{src/click.d.ts → click.d.ts} +0 -0
  3. package/dist/{src/document → document}/index.d.ts +2 -3
  4. package/dist/document/index.js +8 -10
  5. package/dist/document/interceptor.d.ts +15 -0
  6. package/dist/document/interceptor.js +7 -4
  7. package/dist/{src/document → document}/selection.d.ts +0 -1
  8. package/dist/document/selection.js +22 -15
  9. package/dist/{src/document → document}/value.d.ts +4 -0
  10. package/dist/document/value.js +26 -5
  11. package/dist/{src/hover.d.ts → hover.d.ts} +0 -0
  12. package/dist/{src/index.d.ts → index.d.ts} +0 -0
  13. package/dist/{src/keyboard → keyboard}/getEventProps.d.ts +0 -0
  14. package/dist/{src/keyboard → keyboard}/getNextKeyDef.d.ts +0 -0
  15. package/dist/{src/keyboard → keyboard}/index.d.ts +0 -0
  16. package/dist/{src/keyboard → keyboard}/keyMap.d.ts +0 -0
  17. package/dist/keyboard/keyMap.js +4 -0
  18. package/dist/{src/keyboard → keyboard}/keyboardImplementation.d.ts +0 -0
  19. package/dist/keyboard/keyboardImplementation.js +3 -1
  20. package/dist/{src/keyboard → keyboard}/plugins/arrow.d.ts +0 -0
  21. package/dist/{src/keyboard → keyboard}/plugins/character.d.ts +0 -0
  22. package/dist/{src/keyboard → keyboard}/plugins/control.d.ts +0 -0
  23. package/dist/{src/keyboard → keyboard}/plugins/functional.d.ts +0 -0
  24. package/dist/keyboard/plugins/functional.js +17 -0
  25. package/dist/{src/keyboard → keyboard}/plugins/index.d.ts +0 -0
  26. package/dist/{src/keyboard → keyboard}/shared/fireChangeForInputTimeIfValid.d.ts +0 -0
  27. package/dist/{src/keyboard → keyboard}/shared/index.d.ts +0 -0
  28. package/dist/{src/keyboard → keyboard}/specialCharMap.d.ts +0 -0
  29. package/dist/{src/keyboard → keyboard}/types.d.ts +0 -0
  30. package/dist/{src/paste.d.ts → paste.d.ts} +0 -0
  31. package/dist/{src/pointer → pointer}/index.d.ts +0 -0
  32. package/dist/{src/pointer → pointer}/keyMap.d.ts +0 -0
  33. package/dist/{src/pointer → pointer}/parseKeyDef.d.ts +0 -0
  34. package/dist/{src/pointer → pointer}/pointerAction.d.ts +2 -6
  35. package/dist/pointer/pointerAction.js +2 -26
  36. package/dist/pointer/pointerMove.d.ts +5 -0
  37. package/dist/pointer/pointerMove.js +38 -5
  38. package/dist/pointer/pointerPress.d.ts +7 -0
  39. package/dist/pointer/pointerPress.js +133 -29
  40. package/dist/pointer/resolveSelectionTarget.d.ts +5 -0
  41. package/dist/pointer/resolveSelectionTarget.js +74 -0
  42. package/dist/{src/pointer → pointer}/types.d.ts +18 -4
  43. package/dist/{src/selectOptions.d.ts → selectOptions.d.ts} +0 -0
  44. package/dist/{src/setup.d.ts → setup.d.ts} +1 -1
  45. package/dist/setup.js +0 -7
  46. package/dist/tab.d.ts +5 -0
  47. package/dist/tab.js +3 -132
  48. package/dist/{src/type.d.ts → type.d.ts} +0 -0
  49. package/dist/type.js +3 -19
  50. package/dist/{src/upload.d.ts → upload.d.ts} +0 -0
  51. package/dist/{src/utils → utils}/click/isClickableInput.d.ts +0 -0
  52. package/dist/{src/utils → utils}/edit/buildTimeValue.d.ts +0 -0
  53. package/dist/{src/utils → utils}/edit/calculateNewValue.d.ts +0 -0
  54. package/dist/{src/utils → utils}/edit/cursorPosition.d.ts +0 -0
  55. package/dist/{src/utils → utils}/edit/fireInputEvent.d.ts +0 -0
  56. package/dist/utils/edit/fireInputEvent.js +20 -16
  57. package/dist/{src/utils → utils}/edit/getValue.d.ts +0 -0
  58. package/dist/utils/edit/getValue.js +1 -3
  59. package/dist/{src/utils → utils}/edit/isContentEditable.d.ts +0 -0
  60. package/dist/{src/utils → utils}/edit/isEditable.d.ts +0 -0
  61. package/dist/{src/utils → utils}/edit/isValidDateValue.d.ts +0 -0
  62. package/dist/{src/utils → utils}/edit/isValidInputTimeValue.d.ts +0 -0
  63. package/dist/{src/utils → utils}/edit/maxLength.d.ts +0 -0
  64. package/dist/{src/utils → utils}/edit/selectionRange.d.ts +0 -0
  65. package/dist/{src/utils → utils}/focus/blur.d.ts +0 -0
  66. package/dist/{src/utils → utils}/focus/focus.d.ts +0 -0
  67. package/dist/{src/utils → utils}/focus/getActiveElement.d.ts +0 -0
  68. package/dist/utils/focus/getTabDestination.d.ts +1 -0
  69. package/dist/utils/focus/getTabDestination.js +71 -0
  70. package/dist/{src/utils → utils}/focus/isFocusable.d.ts +0 -0
  71. package/dist/{src/utils → utils}/focus/selector.d.ts +0 -0
  72. package/dist/{src/utils → utils}/index.d.ts +1 -1
  73. package/dist/utils/index.js +13 -13
  74. package/dist/{src/utils → utils}/keyDef/readNextDescriptor.d.ts +0 -0
  75. package/dist/{src/utils → utils}/misc/eventWrapper.d.ts +0 -0
  76. package/dist/{src/utils → utils}/misc/findClosest.d.ts +0 -0
  77. package/dist/{src/utils → utils}/misc/hasFormSubmit.d.ts +0 -0
  78. package/dist/{src/utils → utils}/misc/hasPointerEvents.d.ts +0 -0
  79. package/dist/{src/utils → utils}/misc/isDescendantOrSelf.d.ts +0 -0
  80. package/dist/{src/utils → utils}/misc/isDisabled.d.ts +0 -0
  81. package/dist/{src/utils → utils}/misc/isElementType.d.ts +0 -0
  82. package/dist/{src/utils → utils}/misc/isVisible.d.ts +0 -0
  83. package/dist/{src/utils → utils}/misc/wait.d.ts +0 -0
  84. package/dist/utils/pointer/fakeEvent.d.ts +20 -0
  85. package/dist/utils/pointer/fakeEvent.js +18 -9
  86. package/dist/{src/utils → utils}/pointer/firePointerEvents.d.ts +2 -11
  87. package/dist/{src/utils → utils}/pointer/mouseButtons.d.ts +0 -0
  88. package/package.json +2 -2
  89. package/dist/document/applyNative.js +0 -28
  90. package/dist/src/document/applyNative.d.ts +0 -8
  91. package/dist/src/document/interceptor.d.ts +0 -5
  92. package/dist/src/pointer/pointerMove.d.ts +0 -5
  93. package/dist/src/pointer/pointerPress.d.ts +0 -7
  94. package/dist/src/tab.d.ts +0 -6
  95. package/dist/src/utils/misc/isDocument.d.ts +0 -1
  96. package/dist/src/utils/pointer/fakeEvent.d.ts +0 -16
  97. package/dist/tests/_helpers/custom-element.d.ts +0 -8
  98. package/dist/tests/_helpers/utils.d.ts +0 -73
  99. package/dist/tests/_setup-env.d.ts +0 -1
  100. package/dist/tests/clear.d.ts +0 -1
  101. package/dist/tests/click/click.d.ts +0 -1
  102. package/dist/tests/click/dblclick.d.ts +0 -1
  103. package/dist/tests/document/index.d.ts +0 -1
  104. package/dist/tests/document/selectionRange.d.ts +0 -1
  105. package/dist/tests/hover/hover.d.ts +0 -1
  106. package/dist/tests/hover/unhover.d.ts +0 -1
  107. package/dist/tests/keyboard/getNextKeyDef.d.ts +0 -1
  108. package/dist/tests/keyboard/index.d.ts +0 -1
  109. package/dist/tests/keyboard/keyboardImplementation.d.ts +0 -1
  110. package/dist/tests/keyboard/plugin/arrow.d.ts +0 -1
  111. package/dist/tests/keyboard/plugin/character.d.ts +0 -1
  112. package/dist/tests/keyboard/plugin/control.d.ts +0 -1
  113. package/dist/tests/keyboard/plugin/functional.d.ts +0 -1
  114. package/dist/tests/keyboard/shared/fireInputEvent.d.ts +0 -1
  115. package/dist/tests/paste.d.ts +0 -1
  116. package/dist/tests/pointer/index.d.ts +0 -1
  117. package/dist/tests/react/keyboard.d.ts +0 -1
  118. package/dist/tests/react/type.d.ts +0 -1
  119. package/dist/tests/selectOptions/deselect.d.ts +0 -1
  120. package/dist/tests/selectOptions/select.d.ts +0 -1
  121. package/dist/tests/setup.d.ts +0 -10
  122. package/dist/tests/tab.d.ts +0 -1
  123. package/dist/tests/type/index.d.ts +0 -1
  124. package/dist/tests/type/modifiers.d.ts +0 -1
  125. package/dist/tests/upload.d.ts +0 -1
  126. package/dist/tests/utils/edit/calculateNewValue.d.ts +0 -1
  127. package/dist/tests/utils/edit/isContentEditable.d.ts +0 -1
  128. package/dist/tests/utils/focus/blur.d.ts +0 -1
  129. package/dist/tests/utils/focus/focus.d.ts +0 -1
  130. package/dist/tests/utils/misc/hasPointerEvents.d.ts +0 -1
  131. package/dist/tests/utils/misc/isDescendantOrSelf.d.ts +0 -1
  132. package/dist/tests/utils/misc/isElementType.d.ts +0 -1
  133. package/dist/tests/utils/misc/isVisible.d.ts +0 -1
  134. package/dist/utils/misc/dom-helpers.d.ts +0 -3
  135. package/dist/utils/misc/isDocument.js +0 -10
@@ -9,27 +9,31 @@ var _dom = require("@testing-library/dom");
9
9
 
10
10
  var _utils = require("../utils");
11
11
 
12
+ var _document = require("../document");
13
+
14
+ var _resolveSelectionTarget = require("./resolveSelectionTarget");
15
+
12
16
  /* eslint-disable complexity */
13
- async function pointerPress({
14
- keyDef,
15
- releasePrevious,
16
- releaseSelf,
17
- target,
18
- coords
19
- }, state) {
17
+ async function pointerPress(action, state) {
18
+ const {
19
+ keyDef,
20
+ target,
21
+ releasePrevious,
22
+ releaseSelf
23
+ } = action;
20
24
  const previous = state.pointerState.pressed.find(p => p.keyDef === keyDef);
21
25
  const pointerName = keyDef.pointerType === 'touch' ? keyDef.name : keyDef.pointerType;
22
26
  const targetIsDisabled = (0, _utils.isDisabled)(target);
23
27
 
24
28
  if (previous) {
25
- up(state, pointerName, keyDef, targetIsDisabled, target, coords, previous);
29
+ up(state, pointerName, action, previous, targetIsDisabled);
26
30
  }
27
31
 
28
32
  if (!releasePrevious) {
29
- const press = down(state, pointerName, keyDef, targetIsDisabled, target, coords);
33
+ const press = down(state, pointerName, action, targetIsDisabled);
30
34
 
31
35
  if (releaseSelf) {
32
- up(state, pointerName, keyDef, targetIsDisabled, target, coords, press);
36
+ up(state, pointerName, action, press, targetIsDisabled);
33
37
  }
34
38
  }
35
39
  }
@@ -42,7 +46,13 @@ function getNextPointerId(state) {
42
46
  function down({
43
47
  pointerState,
44
48
  keyboardState
45
- }, pointerName, keyDef, targetIsDisabled, target, coords) {
49
+ }, pointerName, {
50
+ keyDef,
51
+ node,
52
+ offset,
53
+ target,
54
+ coords
55
+ }, targetIsDisabled) {
46
56
  var _pointerState$activeC, _pointerState$activeC2, _pointerState$activeC3;
47
57
 
48
58
  const {
@@ -51,7 +61,7 @@ function down({
51
61
  button
52
62
  } = keyDef;
53
63
  const pointerId = pointerType === 'mouse' ? 1 : getNextPointerId(pointerState);
54
- pointerState.position[pointerName] = {
64
+ pointerState.position[pointerName] = { ...pointerState.position[pointerName],
55
65
  pointerId,
56
66
  pointerType,
57
67
  target,
@@ -106,9 +116,14 @@ function down({
106
116
  }
107
117
 
108
118
  if (pointerType === 'mouse' && pressObj.unpreventedDefault) {
109
- var _findClosest;
110
-
111
- (0, _utils.focus)((_findClosest = (0, _utils.findClosest)(target, _utils.isFocusable)) != null ? _findClosest : target.ownerDocument.body);
119
+ mousedownDefaultBehavior({
120
+ target,
121
+ targetIsDisabled,
122
+ clickCount,
123
+ position: pointerState.position[pointerName],
124
+ node,
125
+ offset
126
+ });
112
127
  }
113
128
 
114
129
  return pressObj;
@@ -131,9 +146,15 @@ function up({
131
146
  pointerState,
132
147
  keyboardState
133
148
  }, pointerName, {
134
- pointerType,
135
- button
136
- }, targetIsDisabled, target, coords, pressed) {
149
+ keyDef: {
150
+ pointerType,
151
+ button
152
+ },
153
+ target,
154
+ coords,
155
+ node,
156
+ offset
157
+ }, pressed, targetIsDisabled) {
137
158
  pointerState.pressed = pointerState.pressed.filter(p => p !== pressed);
138
159
  const {
139
160
  isMultiTouch,
@@ -144,9 +165,7 @@ function up({
144
165
  let {
145
166
  unpreventedDefault
146
167
  } = pressed;
147
- pointerState.position[pointerName] = {
148
- pointerId,
149
- pointerType,
168
+ pointerState.position[pointerName] = { ...pointerState.position[pointerName],
150
169
  target,
151
170
  coords
152
171
  }; // TODO: pointerleave for touch device
@@ -173,14 +192,18 @@ function up({
173
192
  }
174
193
 
175
194
  if (unpreventedDefault && pointerType !== 'mouse' && !isMultiTouch) {
176
- var _findClosest2;
177
-
178
- // The closest focusable element is focused when a `mousedown` would have been fired.
179
- // Even if there was no `mousedown` because the element was disabled.
180
- // A `mousedown` that preventsDefault cancels this though.
181
- (0, _utils.focus)((_findClosest2 = (0, _utils.findClosest)(target, _utils.isFocusable)) != null ? _findClosest2 : target.ownerDocument.body);
195
+ mousedownDefaultBehavior({
196
+ target,
197
+ targetIsDisabled,
198
+ clickCount,
199
+ position: pointerState.position[pointerName],
200
+ node,
201
+ offset
202
+ });
182
203
  }
183
204
 
205
+ delete pointerState.position[pointerName].selectionRange;
206
+
184
207
  if (!targetIsDisabled) {
185
208
  if (pointerType === 'mouse' || !isMultiTouch) {
186
209
  unpreventedDefault = fire('mouseup') && unpreventedDefault;
@@ -197,9 +220,9 @@ function up({
197
220
  const label = target.closest('label');
198
221
 
199
222
  if (label != null && label.control) {
200
- var _findClosest3;
223
+ var _findClosest;
201
224
 
202
- (0, _utils.focus)((_findClosest3 = (0, _utils.findClosest)(label.control, _utils.isFocusable)) != null ? _findClosest3 : target.ownerDocument.body);
225
+ (0, _utils.focus)((_findClosest = (0, _utils.findClosest)(label.control, _utils.isFocusable)) != null ? _findClosest : target.ownerDocument.body);
203
226
  }
204
227
  }
205
228
 
@@ -221,4 +244,85 @@ function up({
221
244
  pointerType
222
245
  });
223
246
  }
247
+ }
248
+
249
+ function mousedownDefaultBehavior({
250
+ position,
251
+ target,
252
+ targetIsDisabled,
253
+ clickCount,
254
+ node,
255
+ offset
256
+ }) {
257
+ var _findClosest2;
258
+
259
+ // The closest focusable element is focused when a `mousedown` would have been fired.
260
+ // Even if there was no `mousedown` because the element was disabled.
261
+ // A `mousedown` that preventsDefault cancels this though.
262
+ (0, _utils.focus)((_findClosest2 = (0, _utils.findClosest)(target, _utils.isFocusable)) != null ? _findClosest2 : target.ownerDocument.body); // TODO: What happens if a focus event handler interfers?
263
+ // An unprevented mousedown moves the cursor to the closest character.
264
+ // We try to approximate the behavior for a no-layout environment.
265
+
266
+ if (!targetIsDisabled) {
267
+ const hasValue = (0, _utils.isElementType)(target, ['input', 'textarea']); // On non-input elements the text selection per multiple click
268
+ // can extend beyond the target boundaries.
269
+ // The exact mechanism what is considered in the same line is unclear.
270
+ // Looks it might be every inline element.
271
+ // TODO: Check what might be considered part of the same line of text.
272
+
273
+ const text = String(hasValue ? (0, _document.getUIValue)(target) : target.textContent);
274
+ const [start, end] = node ? // As offset is describing a DOMOffset it is non-trivial to determine
275
+ // which elements might be considered in the same line of text.
276
+ // TODO: support expanding initial range on multiple clicks if node is given
277
+ [offset, offset] : getTextRange(text, offset, clickCount);
278
+
279
+ if (hasValue) {
280
+ (0, _document.setUISelection)(target, start != null ? start : text.length, end != null ? end : text.length);
281
+ position.selectionRange = {
282
+ node: target,
283
+ start: start != null ? start : 0,
284
+ end: end != null ? end : text.length
285
+ };
286
+ } else {
287
+ const {
288
+ node: startNode,
289
+ offset: startOffset
290
+ } = (0, _resolveSelectionTarget.resolveSelectionTarget)({
291
+ target,
292
+ node,
293
+ offset: start
294
+ });
295
+ const {
296
+ node: endNode,
297
+ offset: endOffset
298
+ } = (0, _resolveSelectionTarget.resolveSelectionTarget)({
299
+ target,
300
+ node,
301
+ offset: end
302
+ });
303
+ const range = new Range();
304
+ range.setStart(startNode, startOffset);
305
+ range.setEnd(endNode, endOffset);
306
+ position.selectionRange = range; // TODO: support multiple ranges
307
+
308
+ const selection = target.ownerDocument.getSelection();
309
+ selection.removeAllRanges();
310
+ selection.addRange(range.cloneRange());
311
+ }
312
+ }
313
+ }
314
+
315
+ function getTextRange(text, pos, clickCount) {
316
+ if (clickCount % 3 === 1 || text.length === 0) {
317
+ return [pos, pos];
318
+ }
319
+
320
+ const textPos = pos != null ? pos : text.length;
321
+
322
+ if (clickCount % 3 === 2) {
323
+ return [textPos - text.substr(0, pos).match(/(\w+|\s+|\W)?$/)[0].length, pos === undefined ? pos : pos + text.substr(pos).match(/^(\w+|\s+|\W)?/)[0].length];
324
+ } // triple click
325
+
326
+
327
+ return [textPos - text.substr(0, pos).match(/[^\r\n]*$/)[0].length, pos === undefined ? pos : pos + text.substr(pos).match(/^[^\r\n]*/)[0].length];
224
328
  }
@@ -0,0 +1,5 @@
1
+ import { PointerTarget, SelectionTarget } from './types';
2
+ export declare function resolveSelectionTarget({ target, node, offset, }: PointerTarget & SelectionTarget): {
3
+ node: Node;
4
+ offset: number;
5
+ };
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.resolveSelectionTarget = resolveSelectionTarget;
7
+
8
+ var _document = require("../document");
9
+
10
+ var _utils = require("../utils");
11
+
12
+ function resolveSelectionTarget({
13
+ target,
14
+ node,
15
+ offset
16
+ }) {
17
+ if ((0, _utils.isElementType)(target, ['input', 'textarea'])) {
18
+ var _getUIValue;
19
+
20
+ return {
21
+ node: target,
22
+ offset: offset != null ? offset : ((_getUIValue = (0, _document.getUIValue)(target)) != null ? _getUIValue :
23
+ /* istanbul ignore next */
24
+ '').length
25
+ };
26
+ } else if (node) {
27
+ return {
28
+ node,
29
+ offset: offset != null ? offset : node.nodeType === 3 ? node.nodeValue.length : node.childNodes.length
30
+ };
31
+ }
32
+
33
+ return findNodeAtTextOffset(target, offset);
34
+ }
35
+
36
+ function findNodeAtTextOffset(node, offset, isRoot = true) {
37
+ // When clicking after the content the browser behavior can be complicated:
38
+ // 1. If there is textContent after the last element child,
39
+ // the cursor is moved there.
40
+ // 2. If there is textContent in the last element child,
41
+ // the browser moves the cursor to the last non-empty text node inside this element.
42
+ // 3. Otherwise the cursor is moved to the end of the target.
43
+ let i = offset === undefined ? node.childNodes.length - 1 : 0;
44
+ const step = offset === undefined ? -1 : +1;
45
+
46
+ while (offset === undefined ? i >= (isRoot ? Math.max(node.childNodes.length - 1, 0) : 0) : i <= node.childNodes.length) {
47
+ const c = node.childNodes.item(i);
48
+ const text = String(c.textContent);
49
+
50
+ if (text.length) {
51
+ if (offset !== undefined && text.length < offset) {
52
+ offset -= text.length;
53
+ } else if (c.nodeType === 1) {
54
+ return findNodeAtTextOffset(c, offset, false);
55
+ } else
56
+ /* istanbul ignore else */
57
+ if (c.nodeType === 3) {
58
+ var _offset;
59
+
60
+ return {
61
+ node: c,
62
+ offset: (_offset = offset) != null ? _offset : c.nodeValue.length
63
+ };
64
+ }
65
+ }
66
+
67
+ i += step;
68
+ }
69
+
70
+ return {
71
+ node,
72
+ offset: node.childNodes.length
73
+ };
74
+ }
@@ -1,5 +1,5 @@
1
1
  import { keyboardState } from '../keyboard/types';
2
- import { Coords, MouseButton } from '../utils';
2
+ import { PointerCoords, MouseButton } from '../utils';
3
3
  /**
4
4
  * @internal Do not create/alter this by yourself as this type might be subject to changes.
5
5
  */
@@ -26,8 +26,8 @@ export declare type pointerState = {
26
26
  position: Record<string, {
27
27
  pointerId: number;
28
28
  pointerType: 'mouse' | 'pen' | 'touch';
29
- target?: Element;
30
- coords: Coords;
29
+ } & Partial<PointerTarget> & {
30
+ selectionRange?: Range | SelectionInputRange;
31
31
  }>;
32
32
  /**
33
33
  * Last applied pointer id
@@ -50,7 +50,21 @@ export interface pointerKey {
50
50
  }
51
51
  export interface PointerTarget {
52
52
  target: Element;
53
- coords: Coords;
53
+ coords?: PointerCoords;
54
+ }
55
+ export interface SelectionTarget {
56
+ node?: Node;
57
+ /**
58
+ * If `node` is set, this is the DOM offset.
59
+ * Otherwise this is the `textContent`/`value` offset on the `target`.
60
+ */
61
+ offset?: number;
62
+ }
63
+ /** Describes a selection inside `HTMLInputElement`/`HTMLTextareaElement` */
64
+ export interface SelectionInputRange {
65
+ node: HTMLInputElement | HTMLTextAreaElement;
66
+ start: number;
67
+ end: number;
54
68
  }
55
69
  export interface inputDeviceState {
56
70
  pointerState: pointerState;
@@ -77,7 +77,7 @@ export declare function setup(options?: SetupOptions): {
77
77
  upload: typeof upload;
78
78
  } & any;
79
79
  };
80
- declare function _setup({ applyAccept, autoModify, delay, document, focusTrap, keyboardMap, pointerMap, skipAutoClose, skipClick, skipHover, skipPointerEventsCheck, }: SetupOptions, { keyboardState, pointerState, }: {
80
+ declare function _setup({ applyAccept, autoModify, delay, document, keyboardMap, pointerMap, skipAutoClose, skipClick, skipHover, skipPointerEventsCheck, }: SetupOptions, { keyboardState, pointerState, }: {
81
81
  keyboardState: keyboardState;
82
82
  pointerState: pointerState;
83
83
  }): UserEventApis & {
package/dist/setup.js CHANGED
@@ -64,7 +64,6 @@ function _setup({
64
64
  autoModify,
65
65
  delay = 0,
66
66
  document,
67
- focusTrap,
68
67
  keyboardMap,
69
68
  pointerMap,
70
69
  skipAutoClose,
@@ -91,9 +90,6 @@ function _setup({
91
90
  const clickDefaults = {
92
91
  skipHover
93
92
  };
94
- const tabDefaults = {
95
- focusTrap
96
- };
97
93
  const typeDefaults = {
98
94
  delay,
99
95
  skipAutoClose,
@@ -177,9 +173,6 @@ function _setup({
177
173
  });
178
174
  },
179
175
  tab: (...args) => {
180
- args[0] = { ...tabDefaults,
181
- ...args[0]
182
- };
183
176
  return _tab.tab.call(userEvent, ...args);
184
177
  },
185
178
  // type needs typecasting because of the overloading
package/dist/tab.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ import type { UserEvent } from './setup';
2
+ export interface tabOptions {
3
+ shift?: boolean;
4
+ }
5
+ export declare function tab(this: UserEvent, { shift }?: tabOptions): void;
package/dist/tab.js CHANGED
@@ -5,137 +5,8 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.tab = tab;
7
7
 
8
- var _dom = require("@testing-library/dom");
9
-
10
- var _utils = require("./utils");
11
-
12
- function getNextElement(currentIndex, shift, elements, focusTrap) {
13
- if ((0, _utils.isDocument)(focusTrap) && (currentIndex === 0 && shift || currentIndex === elements.length - 1 && !shift)) {
14
- return focusTrap.body;
15
- }
16
-
17
- const nextIndex = shift ? currentIndex - 1 : currentIndex + 1;
18
- const defaultIndex = shift ? elements.length - 1 : 0;
19
- return elements[nextIndex] || elements[defaultIndex];
20
- }
21
-
22
8
  function tab({
23
- shift = false,
24
- focusTrap
9
+ shift
25
10
  } = {}) {
26
- var _focusTrap$ownerDocum, _focusTrap;
27
-
28
- const doc = (_focusTrap$ownerDocum = (_focusTrap = focusTrap) == null ? void 0 : _focusTrap.ownerDocument) != null ? _focusTrap$ownerDocum : document;
29
- const previousElement = (0, _utils.getActiveElement)(doc);
30
-
31
- if (!focusTrap) {
32
- focusTrap = doc;
33
- }
34
-
35
- const focusableElements = focusTrap.querySelectorAll(_utils.FOCUSABLE_SELECTOR);
36
- const enabledElements = Array.from(focusableElements).filter(el => el === previousElement || el.getAttribute('tabindex') !== '-1' && !(0, _utils.isDisabled)(el) && // Hidden elements are not tabable
37
- (0, _utils.isVisible)(el));
38
- if (enabledElements.length === 0) return;
39
- const orderedElements = enabledElements.map((el, idx) => ({
40
- el,
41
- idx
42
- })).sort((a, b) => {
43
- // tabindex has no effect if the active element has tabindex="-1"
44
- if (previousElement && previousElement.getAttribute('tabindex') === '-1') {
45
- return a.idx - b.idx;
46
- }
47
-
48
- const tabIndexA = Number(a.el.getAttribute('tabindex'));
49
- const tabIndexB = Number(b.el.getAttribute('tabindex'));
50
- const diff = tabIndexA - tabIndexB;
51
- return diff === 0 ? a.idx - b.idx : diff;
52
- }).map(({
53
- el
54
- }) => el); // TODO: verify/remove type casts
55
-
56
- const checkedRadio = {};
57
- let prunedElements = [];
58
- orderedElements.forEach(currentElement => {
59
- // For radio groups keep only the active radio
60
- // If there is no active radio, keep only the checked radio
61
- // If there is no checked radio, treat like everything else
62
- const el = currentElement;
63
-
64
- if (el.type === 'radio' && el.name) {
65
- // If the active element is part of the group, add only that
66
- const prev = previousElement;
67
-
68
- if (prev && prev.type === el.type && prev.name === el.name) {
69
- if (el === prev) {
70
- prunedElements.push(el);
71
- }
72
-
73
- return;
74
- } // If we stumble upon a checked radio, remove the others
75
-
76
-
77
- if (el.checked) {
78
- prunedElements = prunedElements.filter(e => e.type !== el.type || e.name !== el.name);
79
- prunedElements.push(el);
80
- checkedRadio[el.name] = el;
81
- return;
82
- } // If we already found the checked one, skip
83
-
84
-
85
- if (typeof checkedRadio[el.name] !== 'undefined') {
86
- return;
87
- }
88
- }
89
-
90
- prunedElements.push(el);
91
- });
92
- const index = prunedElements.findIndex(el => el === previousElement);
93
- const nextElement = getNextElement(index, shift, prunedElements, focusTrap);
94
- const shiftKeyInit = {
95
- key: 'Shift',
96
- keyCode: 16,
97
- shiftKey: true
98
- };
99
- const tabKeyInit = {
100
- key: 'Tab',
101
- keyCode: 9,
102
- shiftKey: shift
103
- };
104
- let continueToTab = true; // not sure how to make it so there's no previous element...
105
- // istanbul ignore else
106
-
107
- if (previousElement) {
108
- // preventDefault on the shift key makes no difference
109
- if (shift) _dom.fireEvent.keyDown(previousElement, { ...shiftKeyInit
110
- });
111
- continueToTab = _dom.fireEvent.keyDown(previousElement, { ...tabKeyInit
112
- });
113
- }
114
-
115
- const keyUpTarget = !continueToTab && previousElement ? previousElement : nextElement;
116
-
117
- if (continueToTab) {
118
- if (nextElement === doc.body) {
119
- /* istanbul ignore else */
120
- if (previousElement) {
121
- (0, _utils.blur)(previousElement);
122
- }
123
- } else {
124
- (0, _utils.focus)(nextElement);
125
- }
126
- }
127
-
128
- _dom.fireEvent.keyUp(keyUpTarget, { ...tabKeyInit
129
- });
130
-
131
- if (shift) {
132
- _dom.fireEvent.keyUp(keyUpTarget, { ...shiftKeyInit,
133
- shiftKey: false
134
- });
135
- }
136
- }
137
- /*
138
- eslint
139
- complexity: "off",
140
- max-statements: "off",
141
- */
11
+ this.keyboard(shift === true ? '{Shift>}{Tab}{/Shift}' : shift === false ? '[/ShiftLeft][/ShiftRight]{Tab}' : '{Tab}');
12
+ }
File without changes
package/dist/type.js CHANGED
@@ -47,26 +47,10 @@ async function typeImplementation(userEvent, element, text, {
47
47
  // TODO: properly type guard
48
48
  // we use this workaround for now to prevent changing behavior
49
49
  if (element.disabled) return;
50
- if (!skipClick) userEvent.click(element); // The focused element could change between each event, so get the currently active element each time
50
+ if (!skipClick) userEvent.click(element);
51
51
 
52
- const currentElement = () => (0, _utils.getActiveElement)(element.ownerDocument); // by default, a new element has its selection start and end at 0
53
- // but most of the time when people call "type", they expect it to type
54
- // at the end of the current input value. So, if the selection start
55
- // and end are both the default of 0, then we'll go ahead and change
56
- // them to the length of the current value.
57
- // the only time it would make sense to pass the initialSelectionStart or
58
- // initialSelectionEnd is if you have an input with a value and want to
59
- // explicitly start typing with the cursor at 0. Not super common.
60
-
61
-
62
- const value = (0, _utils.getValue)(currentElement());
63
- const {
64
- selectionStart,
65
- selectionEnd
66
- } = (0, _utils.getSelectionRange)(element);
67
-
68
- if (value != null && (selectionStart === null || selectionStart === 0) && (selectionEnd === null || selectionEnd === 0)) {
69
- (0, _utils.setSelectionRange)(currentElement(), initialSelectionStart != null ? initialSelectionStart : value.length, initialSelectionEnd != null ? initialSelectionEnd : value.length);
52
+ if (initialSelectionStart !== undefined) {
53
+ (0, _utils.setSelectionRange)(element, initialSelectionStart, initialSelectionEnd != null ? initialSelectionEnd : initialSelectionStart);
70
54
  }
71
55
 
72
56
  const {
File without changes
@@ -20,36 +20,40 @@ function fireInputEvent(element, {
20
20
  newSelectionStart,
21
21
  eventOverrides
22
22
  }) {
23
- // apply the changes before firing the input event, so that input handlers can access the altered dom and selection
23
+ const oldValue = element.value; // apply the changes before firing the input event, so that input handlers can access the altered dom and selection
24
+
24
25
  if ((0, _isContentEditable.isContentEditable)(element)) {
25
- (0, _document.applyNative)(element, 'textContent', newValue);
26
- } else
26
+ element.textContent = newValue;
27
+ } else {
28
+ // The pre-commit hooks keeps changing this
29
+ // See https://github.com/kentcdodds/kcd-scripts/issues/218
30
+
27
31
  /* istanbul ignore else */
32
+ // eslint-disable-next-line no-lonely-if
28
33
  if ((0, _isElementType.isElementType)(element, ['input', 'textarea'])) {
29
34
  (0, _document.setUIValue)(element, newValue);
30
35
  } else {
31
36
  // TODO: properly type guard
32
37
  throw new Error('Invalid Element');
33
38
  }
39
+ }
34
40
 
35
- setSelectionRangeAfterInput(element, newSelectionStart);
41
+ (0, _selectionRange.setSelectionRange)(element, newSelectionStart, newSelectionStart); // When the input event happens in the browser, React executes all event handlers
42
+ // and if they change state of a controlled value, nothing happens.
43
+ // But when we trigger the event handlers in test environment,
44
+ // the changes are rolled back by React before the state update is applied.
45
+ // Then the updated state is applied which results in a resetted cursor.
46
+ // There is probably a better way to work around if we figure out
47
+ // why the batched update is executed differently in our test environment.
48
+
49
+ (0, _document.startTrackValue)(element);
36
50
 
37
51
  _dom.fireEvent.input(element, { ...eventOverrides
38
52
  });
39
53
 
40
- setSelectionRangeAfterInputHandler(element, newSelectionStart);
41
- }
42
-
43
- function setSelectionRangeAfterInput(element, newSelectionStart) {
44
- (0, _selectionRange.setSelectionRange)(element, newSelectionStart, newSelectionStart);
45
- }
54
+ const tracked = (0, _document.endTrackValue)(element);
46
55
 
47
- function setSelectionRangeAfterInputHandler(element, newSelectionStart) {
48
- // On controlled inputs the selection changes without a call to
49
- // either the `value` setter or the `setSelectionRange` method.
50
- // So if our tracked position for UI still exists and derives from a valid selectionStart,
51
- // the cursor was moved due to an input being controlled.
52
- if ((0, _isElementType.isElementType)(element, ['input', 'textarea']) && typeof element.selectionStart === 'number' && element.selectionStart !== newSelectionStart && (0, _document.hasUISelection)(element)) {
56
+ if ((tracked == null ? void 0 : tracked.length) === 2 && tracked[0] === oldValue && tracked[1] === newValue) {
53
57
  (0, _selectionRange.setSelectionRange)(element, newSelectionStart, newSelectionStart);
54
58
  }
55
59
  }
File without changes
@@ -10,8 +10,6 @@ var _document = require("../../document");
10
10
  var _isContentEditable = require("./isContentEditable");
11
11
 
12
12
  function getValue(element) {
13
- var _getUIValue;
14
-
15
13
  // istanbul ignore if
16
14
  if (!element) {
17
15
  return null;
@@ -21,5 +19,5 @@ function getValue(element) {
21
19
  return element.textContent;
22
20
  }
23
21
 
24
- return (_getUIValue = (0, _document.getUIValue)(element)) != null ? _getUIValue : null;
22
+ return (0, _document.getUIValue)(element);
25
23
  }
File without changes