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

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 (178) hide show
  1. package/dist/{src/clear.d.ts → clear.d.ts} +0 -0
  2. package/dist/clear.js +14 -27
  3. package/dist/{src/click.d.ts → click.d.ts} +3 -2
  4. package/dist/click.js +23 -7
  5. package/dist/document/index.d.ts +10 -0
  6. package/dist/document/index.js +8 -10
  7. package/dist/document/interceptor.d.ts +15 -0
  8. package/dist/document/interceptor.js +7 -4
  9. package/dist/document/selection.d.ts +27 -0
  10. package/dist/document/selection.js +51 -28
  11. package/dist/{src/document → document}/value.d.ts +5 -1
  12. package/dist/document/value.js +27 -6
  13. package/dist/{src/hover.d.ts → hover.d.ts} +2 -2
  14. package/dist/hover.js +2 -2
  15. package/dist/{src/index.d.ts → index.d.ts} +0 -2
  16. package/dist/index.js +0 -8
  17. package/dist/{src/keyboard → keyboard}/getEventProps.d.ts +0 -2
  18. package/dist/keyboard/getEventProps.js +1 -7
  19. package/dist/{src/keyboard → keyboard}/getNextKeyDef.d.ts +0 -1
  20. package/dist/keyboard/getNextKeyDef.js +2 -32
  21. package/dist/{src/keyboard → keyboard}/index.d.ts +0 -1
  22. package/dist/keyboard/index.js +0 -8
  23. package/dist/{src/keyboard → keyboard}/keyMap.d.ts +0 -0
  24. package/dist/keyboard/keyMap.js +26 -46
  25. package/dist/{src/keyboard → keyboard}/keyboardImplementation.d.ts +0 -0
  26. package/dist/keyboard/keyboardImplementation.js +15 -17
  27. package/dist/{src/keyboard → keyboard}/plugins/arrow.d.ts +0 -0
  28. package/dist/keyboard/plugins/arrow.js +7 -12
  29. package/dist/{src/keyboard → keyboard}/plugins/character.d.ts +0 -0
  30. package/dist/keyboard/plugins/character.js +27 -45
  31. package/dist/keyboard/plugins/combination.d.ts +5 -0
  32. package/dist/keyboard/plugins/combination.js +17 -0
  33. package/dist/{src/keyboard → keyboard}/plugins/control.d.ts +0 -0
  34. package/dist/keyboard/plugins/control.js +4 -12
  35. package/dist/{src/keyboard → keyboard}/plugins/functional.d.ts +0 -0
  36. package/dist/keyboard/plugins/functional.js +23 -11
  37. package/dist/{src/keyboard → keyboard}/plugins/index.d.ts +0 -1
  38. package/dist/keyboard/plugins/index.js +4 -11
  39. package/dist/{src/keyboard → keyboard}/shared/fireChangeForInputTimeIfValid.d.ts +0 -0
  40. package/dist/{src/keyboard → keyboard}/shared/index.d.ts +0 -0
  41. package/dist/{src/keyboard → keyboard}/types.d.ts +0 -2
  42. package/dist/paste.d.ts +6 -0
  43. package/dist/paste.js +40 -43
  44. package/dist/{src/pointer → pointer}/index.d.ts +0 -0
  45. package/dist/{src/pointer → pointer}/keyMap.d.ts +0 -0
  46. package/dist/{src/pointer → pointer}/parseKeyDef.d.ts +0 -0
  47. package/dist/{src/pointer → pointer}/pointerAction.d.ts +2 -6
  48. package/dist/pointer/pointerAction.js +2 -26
  49. package/dist/pointer/pointerMove.d.ts +5 -0
  50. package/dist/pointer/pointerMove.js +49 -5
  51. package/dist/pointer/pointerPress.d.ts +7 -0
  52. package/dist/pointer/pointerPress.js +135 -29
  53. package/dist/pointer/resolveSelectionTarget.d.ts +5 -0
  54. package/dist/pointer/resolveSelectionTarget.js +75 -0
  55. package/dist/{src/pointer → pointer}/types.d.ts +18 -4
  56. package/dist/{src/selectOptions.d.ts → selectOptions.d.ts} +2 -2
  57. package/dist/selectOptions.js +25 -26
  58. package/dist/{src/setup.d.ts → setup.d.ts} +5 -2
  59. package/dist/setup.js +32 -21
  60. package/dist/tab.d.ts +5 -0
  61. package/dist/tab.js +3 -132
  62. package/dist/{src/type.d.ts → type.d.ts} +0 -0
  63. package/dist/type.js +3 -19
  64. package/dist/{src/upload.d.ts → upload.d.ts} +0 -1
  65. package/dist/upload.js +1 -1
  66. package/dist/{src/utils → utils}/click/isClickableInput.d.ts +0 -0
  67. package/dist/utils/dataTransfer/Blob.d.ts +1 -0
  68. package/dist/utils/dataTransfer/Blob.js +21 -0
  69. package/dist/utils/dataTransfer/Clipboard.d.ts +7 -0
  70. package/dist/utils/dataTransfer/Clipboard.js +143 -0
  71. package/dist/utils/dataTransfer/DataTransfer.d.ts +1 -0
  72. package/dist/utils/dataTransfer/DataTransfer.js +143 -0
  73. package/dist/utils/dataTransfer/FileList.d.ts +1 -0
  74. package/dist/utils/dataTransfer/FileList.js +13 -0
  75. package/dist/{src/utils → utils}/edit/buildTimeValue.d.ts +0 -0
  76. package/dist/utils/edit/calculateNewValue.d.ts +14 -0
  77. package/dist/utils/edit/calculateNewValue.js +19 -21
  78. package/dist/utils/edit/editInputElement.d.ts +16 -0
  79. package/dist/utils/edit/editInputElement.js +53 -0
  80. package/dist/{src/utils → utils}/edit/getValue.d.ts +0 -0
  81. package/dist/utils/edit/getValue.js +1 -3
  82. package/dist/utils/edit/isContentEditable.d.ts +7 -0
  83. package/dist/utils/edit/isContentEditable.js +14 -0
  84. package/dist/{src/utils → utils}/edit/isEditable.d.ts +1 -0
  85. package/dist/{src/utils → utils}/edit/isValidDateValue.d.ts +0 -0
  86. package/dist/{src/utils → utils}/edit/isValidInputTimeValue.d.ts +0 -0
  87. package/dist/{src/utils → utils}/edit/maxLength.d.ts +0 -0
  88. package/dist/utils/edit/prepareInput.d.ts +7 -0
  89. package/dist/utils/edit/prepareInput.js +77 -0
  90. package/dist/{src/utils → utils}/focus/blur.d.ts +0 -0
  91. package/dist/{src/utils → utils}/focus/focus.d.ts +0 -0
  92. package/dist/utils/focus/focus.js +3 -0
  93. package/dist/{src/utils → utils}/focus/getActiveElement.d.ts +0 -0
  94. package/dist/utils/focus/getTabDestination.d.ts +1 -0
  95. package/dist/utils/focus/getTabDestination.js +71 -0
  96. package/dist/{src/utils → utils}/focus/isFocusable.d.ts +0 -0
  97. package/dist/utils/focus/selectAll.d.ts +5 -0
  98. package/dist/utils/focus/selectAll.js +51 -0
  99. package/dist/utils/focus/selection.d.ts +42 -0
  100. package/dist/utils/focus/selection.js +190 -0
  101. package/dist/{src/utils → utils}/focus/selector.d.ts +0 -0
  102. package/dist/{src/utils → utils}/index.d.ts +9 -5
  103. package/dist/utils/index.js +98 -46
  104. package/dist/{src/utils → utils}/keyDef/readNextDescriptor.d.ts +0 -0
  105. package/dist/{src/utils → utils}/misc/eventWrapper.d.ts +0 -0
  106. package/dist/{src/utils → utils}/misc/findClosest.d.ts +0 -0
  107. package/dist/{src/utils → utils}/misc/hasFormSubmit.d.ts +0 -0
  108. package/dist/{src/utils → utils}/misc/hasPointerEvents.d.ts +0 -0
  109. package/dist/{src/utils → utils}/misc/isDescendantOrSelf.d.ts +0 -0
  110. package/dist/{src/utils → utils}/misc/isDisabled.d.ts +0 -0
  111. package/dist/{src/utils → utils}/misc/isElementType.d.ts +0 -0
  112. package/dist/{src/utils → utils}/misc/isVisible.d.ts +0 -0
  113. package/dist/{src/utils → utils}/misc/wait.d.ts +0 -0
  114. package/dist/{src/utils → utils}/pointer/firePointerEvents.d.ts +13 -11
  115. package/dist/utils/pointer/firePointerEvents.js +62 -5
  116. package/dist/{src/utils → utils}/pointer/mouseButtons.d.ts +0 -0
  117. package/package.json +2 -2
  118. package/dist/document/applyNative.js +0 -28
  119. package/dist/keyboard/specialCharMap.js +0 -30
  120. package/dist/src/document/applyNative.d.ts +0 -8
  121. package/dist/src/document/index.d.ts +0 -10
  122. package/dist/src/document/interceptor.d.ts +0 -5
  123. package/dist/src/document/selection.d.ts +0 -18
  124. package/dist/src/keyboard/specialCharMap.d.ts +0 -22
  125. package/dist/src/paste.d.ts +0 -7
  126. package/dist/src/pointer/pointerMove.d.ts +0 -5
  127. package/dist/src/pointer/pointerPress.d.ts +0 -7
  128. package/dist/src/tab.d.ts +0 -6
  129. package/dist/src/utils/edit/calculateNewValue.d.ts +0 -7
  130. package/dist/src/utils/edit/cursorPosition.d.ts +0 -2
  131. package/dist/src/utils/edit/fireInputEvent.d.ts +0 -8
  132. package/dist/src/utils/edit/isContentEditable.d.ts +0 -3
  133. package/dist/src/utils/edit/selectionRange.d.ts +0 -5
  134. package/dist/src/utils/misc/isDocument.d.ts +0 -1
  135. package/dist/src/utils/pointer/fakeEvent.d.ts +0 -16
  136. package/dist/tests/_helpers/custom-element.d.ts +0 -8
  137. package/dist/tests/_helpers/utils.d.ts +0 -73
  138. package/dist/tests/_setup-env.d.ts +0 -1
  139. package/dist/tests/clear.d.ts +0 -1
  140. package/dist/tests/click/click.d.ts +0 -1
  141. package/dist/tests/click/dblclick.d.ts +0 -1
  142. package/dist/tests/document/index.d.ts +0 -1
  143. package/dist/tests/document/selectionRange.d.ts +0 -1
  144. package/dist/tests/hover/hover.d.ts +0 -1
  145. package/dist/tests/hover/unhover.d.ts +0 -1
  146. package/dist/tests/keyboard/getNextKeyDef.d.ts +0 -1
  147. package/dist/tests/keyboard/index.d.ts +0 -1
  148. package/dist/tests/keyboard/keyboardImplementation.d.ts +0 -1
  149. package/dist/tests/keyboard/plugin/arrow.d.ts +0 -1
  150. package/dist/tests/keyboard/plugin/character.d.ts +0 -1
  151. package/dist/tests/keyboard/plugin/control.d.ts +0 -1
  152. package/dist/tests/keyboard/plugin/functional.d.ts +0 -1
  153. package/dist/tests/keyboard/shared/fireInputEvent.d.ts +0 -1
  154. package/dist/tests/paste.d.ts +0 -1
  155. package/dist/tests/pointer/index.d.ts +0 -1
  156. package/dist/tests/react/keyboard.d.ts +0 -1
  157. package/dist/tests/react/type.d.ts +0 -1
  158. package/dist/tests/selectOptions/deselect.d.ts +0 -1
  159. package/dist/tests/selectOptions/select.d.ts +0 -1
  160. package/dist/tests/setup.d.ts +0 -10
  161. package/dist/tests/tab.d.ts +0 -1
  162. package/dist/tests/type/index.d.ts +0 -1
  163. package/dist/tests/type/modifiers.d.ts +0 -1
  164. package/dist/tests/upload.d.ts +0 -1
  165. package/dist/tests/utils/edit/calculateNewValue.d.ts +0 -1
  166. package/dist/tests/utils/edit/isContentEditable.d.ts +0 -1
  167. package/dist/tests/utils/focus/blur.d.ts +0 -1
  168. package/dist/tests/utils/focus/focus.d.ts +0 -1
  169. package/dist/tests/utils/misc/hasPointerEvents.d.ts +0 -1
  170. package/dist/tests/utils/misc/isDescendantOrSelf.d.ts +0 -1
  171. package/dist/tests/utils/misc/isElementType.d.ts +0 -1
  172. package/dist/tests/utils/misc/isVisible.d.ts +0 -1
  173. package/dist/utils/edit/cursorPosition.js +0 -35
  174. package/dist/utils/edit/fireInputEvent.js +0 -55
  175. package/dist/utils/edit/selectionRange.js +0 -64
  176. package/dist/utils/misc/dom-helpers.d.ts +0 -3
  177. package/dist/utils/misc/isDocument.js +0 -10
  178. package/dist/utils/pointer/fakeEvent.js +0 -82
File without changes
package/dist/clear.js CHANGED
@@ -5,42 +5,29 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.clear = clear;
7
7
 
8
+ var _document = require("./document");
9
+
8
10
  var _utils = require("./utils");
9
11
 
10
12
  function clear(element) {
11
- var _element$selectionSta, _element$selectionEnd;
13
+ var _prepareInput;
12
14
 
13
- if (!(0, _utils.isElementType)(element, ['input', 'textarea'])) {
14
- // TODO: support contenteditable
15
- throw new Error('clear currently only supports input and textarea elements.');
15
+ if (!(0, _utils.isEditable)(element) || (0, _utils.isDisabled)(element)) {
16
+ throw new Error('clear()` is only supported on editable elements.');
16
17
  }
17
18
 
18
- if ((0, _utils.isDisabled)(element)) {
19
- return;
20
- } // TODO: track the selection range ourselves so we don't have to do this input "type" trickery
21
- // just like cypress does: https://github.com/cypress-io/cypress/blob/8d7f1a0bedc3c45a2ebf1ff50324b34129fdc683/packages/driver/src/dom/selection.ts#L16-L37
19
+ (0, _document.prepareDocument)(element.ownerDocument);
20
+ (0, _utils.focus)(element);
22
21
 
22
+ if (element.ownerDocument.activeElement !== element) {
23
+ throw new Error('The element to be cleared could not be focused.');
24
+ }
23
25
 
24
- const elementType = element.type;
26
+ (0, _utils.selectAll)(element);
25
27
 
26
- if (elementType !== 'textarea') {
27
- // setSelectionRange is not supported on certain types of inputs, e.g. "number" or "email"
28
- ;
29
- element.type = 'text';
28
+ if (!(0, _utils.isAllSelected)(element)) {
29
+ throw new Error('The element content to be cleared could not be selected.');
30
30
  }
31
31
 
32
- this.type(element, '{selectall}{del}', {
33
- delay: 0,
34
- initialSelectionStart: (_element$selectionSta = element.selectionStart) != null ? _element$selectionSta :
35
- /* istanbul ignore next */
36
- undefined,
37
- initialSelectionEnd: (_element$selectionEnd = element.selectionEnd) != null ? _element$selectionEnd :
38
- /* istanbul ignore next */
39
- undefined
40
- });
41
-
42
- if (elementType !== 'textarea') {
43
- ;
44
- element.type = elementType;
45
- }
32
+ (_prepareInput = (0, _utils.prepareInput)('', element, 'deleteContentBackward')) == null ? void 0 : _prepareInput.commit();
46
33
  }
@@ -4,5 +4,6 @@ export declare interface clickOptions {
4
4
  skipHover?: boolean;
5
5
  clickCount?: number;
6
6
  }
7
- export declare function click(this: UserEvent, element: Element, init?: MouseEventInit, { skipHover, skipPointerEventsCheck, }?: clickOptions & PointerOptions): void;
8
- export declare function dblClick(this: UserEvent, element: Element, init?: MouseEventInit, { skipPointerEventsCheck }?: clickOptions & PointerOptions): void;
7
+ export declare function click(this: UserEvent, element: Element, { skipHover, skipPointerEventsCheck, }?: clickOptions & PointerOptions): void;
8
+ export declare function dblClick(this: UserEvent, element: Element, { skipPointerEventsCheck }?: clickOptions & PointerOptions): void;
9
+ export declare function tripleClick(this: UserEvent, element: Element, { skipPointerEventsCheck }?: clickOptions & PointerOptions): void;
package/dist/click.js CHANGED
@@ -5,10 +5,11 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.click = click;
7
7
  exports.dblClick = dblClick;
8
+ exports.tripleClick = tripleClick;
8
9
 
9
10
  var _utils = require("./utils");
10
11
 
11
- function click(element, init, {
12
+ function click(element, {
12
13
  skipHover = false,
13
14
  skipPointerEventsCheck = false
14
15
  } = {}) {
@@ -18,28 +19,43 @@ function click(element, init, {
18
19
 
19
20
 
20
21
  if (!skipHover) // We just checked for `pointerEvents`. We can always skip this one in `hover`.
21
- this.hover(element, init, {
22
+ this.hover(element, {
22
23
  skipPointerEventsCheck: true
23
24
  });
24
- const keys = (init == null ? void 0 : init.button) === 2 || (init == null ? void 0 : init.buttons) === 2 ? '[MouseRight]' : '[MouseLeft]';
25
25
  this.pointer({
26
- keys,
26
+ keys: '[MouseLeft]',
27
27
  target: element
28
28
  });
29
29
  }
30
30
 
31
- function dblClick(element, init, {
31
+ function dblClick(element, {
32
32
  skipPointerEventsCheck = false
33
33
  } = {}) {
34
34
  if (!skipPointerEventsCheck && !(0, _utils.hasPointerEvents)(element)) {
35
35
  throw new Error('unable to double-click element as it has or inherits pointer-events set to "none".');
36
36
  }
37
37
 
38
- this.hover(element, init, {
39
- skipPointerEventsCheck
38
+ this.hover(element, {
39
+ skipPointerEventsCheck: true
40
40
  });
41
41
  this.pointer({
42
42
  keys: '[MouseLeft][MouseLeft]',
43
43
  target: element
44
44
  });
45
+ }
46
+
47
+ function tripleClick(element, {
48
+ skipPointerEventsCheck = false
49
+ } = {}) {
50
+ if (!skipPointerEventsCheck && !(0, _utils.hasPointerEvents)(element)) {
51
+ throw new Error('unable to triple-click element as it has or inherits pointer-events set to "none".');
52
+ }
53
+
54
+ this.hover(element, {
55
+ skipPointerEventsCheck: true
56
+ });
57
+ this.pointer({
58
+ keys: '[MouseLeft][MouseLeft][MouseLeft]',
59
+ target: element
60
+ });
45
61
  }
@@ -0,0 +1,10 @@
1
+ declare const isPrepared: unique symbol;
2
+ declare global {
3
+ interface Node {
4
+ [isPrepared]?: typeof isPrepared;
5
+ }
6
+ }
7
+ export declare function prepareDocument(document: Document): void;
8
+ export { getUIValue, setUIValue, startTrackValue, endTrackValue } from './value';
9
+ export { getUISelection, setUISelection } from './selection';
10
+ export type { UISelectionRange } from './selection';
@@ -3,10 +3,10 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- Object.defineProperty(exports, "applyNative", {
6
+ Object.defineProperty(exports, "endTrackValue", {
7
7
  enumerable: true,
8
8
  get: function () {
9
- return _applyNative.applyNative;
9
+ return _value.endTrackValue;
10
10
  }
11
11
  });
12
12
  Object.defineProperty(exports, "getUISelection", {
@@ -21,12 +21,6 @@ Object.defineProperty(exports, "getUIValue", {
21
21
  return _value.getUIValue;
22
22
  }
23
23
  });
24
- Object.defineProperty(exports, "hasUISelection", {
25
- enumerable: true,
26
- get: function () {
27
- return _selection.hasUISelection;
28
- }
29
- });
30
24
  exports.prepareDocument = prepareDocument;
31
25
  Object.defineProperty(exports, "setUISelection", {
32
26
  enumerable: true,
@@ -40,6 +34,12 @@ Object.defineProperty(exports, "setUIValue", {
40
34
  return _value.setUIValue;
41
35
  }
42
36
  });
37
+ Object.defineProperty(exports, "startTrackValue", {
38
+ enumerable: true,
39
+ get: function () {
40
+ return _value.startTrackValue;
41
+ }
42
+ });
43
43
 
44
44
  var _dom = require("@testing-library/dom");
45
45
 
@@ -47,8 +47,6 @@ var _selection = require("./selection");
47
47
 
48
48
  var _value = require("./value");
49
49
 
50
- var _applyNative = require("./applyNative");
51
-
52
50
  const isPrepared = Symbol('Node prepared with document state workarounds');
53
51
 
54
52
  function prepareDocument(document) {
@@ -0,0 +1,15 @@
1
+ declare type anyFunc = (...a: any[]) => any;
2
+ declare type Params<Prop> = Prop extends anyFunc ? Parameters<Prop> : [Prop];
3
+ declare type ImplReturn<Prop> = Prop extends anyFunc ? Parameters<Prop> : Prop;
4
+ export declare function prepareInterceptor<ElementType extends Node, PropName extends keyof ElementType>(element: ElementType, propName: PropName, interceptorImpl: (this: ElementType, ...args: Params<ElementType[PropName]>) => {
5
+ /**
6
+ * React tracks the changes on element properties.
7
+ * This workaround tries to alter the DOM element without React noticing,
8
+ * so that it later picks up the change.
9
+ *
10
+ * @see https://github.com/facebook/react/blob/148f8e497c7d37a3c7ab99f01dec2692427272b1/packages/react-dom/src/client/inputValueTracking.js#L51-L104
11
+ */
12
+ applyNative?: boolean;
13
+ realArgs?: ImplReturn<ElementType[PropName]>;
14
+ }): void;
15
+ export {};
@@ -8,16 +8,19 @@ const Interceptor = Symbol('Interceptor for programmatical calls');
8
8
 
9
9
  function prepareInterceptor(element, propName, interceptorImpl) {
10
10
  const prototypeDescriptor = Object.getOwnPropertyDescriptor(element.constructor.prototype, propName);
11
+ const objectDescriptor = Object.getOwnPropertyDescriptor(element, propName);
11
12
  const target = prototypeDescriptor != null && prototypeDescriptor.set ? 'set' : 'value';
12
13
 
13
14
  if (typeof (prototypeDescriptor == null ? void 0 : prototypeDescriptor[target]) !== 'function' || prototypeDescriptor[target][Interceptor]) {
14
15
  return;
15
16
  }
16
17
 
17
- const realFunc = prototypeDescriptor[target];
18
-
19
18
  function intercept(...args) {
20
- const realArgs = interceptorImpl.call(this, ...args);
19
+ const {
20
+ applyNative = true,
21
+ realArgs
22
+ } = interceptorImpl.call(this, ...args);
23
+ const realFunc = (!applyNative && objectDescriptor || prototypeDescriptor)[target];
21
24
 
22
25
  if (target === 'set') {
23
26
  realFunc.call(this, realArgs);
@@ -28,7 +31,7 @@ function prepareInterceptor(element, propName, interceptorImpl) {
28
31
 
29
32
  ;
30
33
  intercept[Interceptor] = Interceptor;
31
- Object.defineProperty(element.constructor.prototype, propName, { ...prototypeDescriptor,
34
+ Object.defineProperty(element, propName, { ...(objectDescriptor != null ? objectDescriptor : prototypeDescriptor),
32
35
  [target]: intercept
33
36
  });
34
37
  }
@@ -0,0 +1,27 @@
1
+ declare const UISelection: unique symbol;
2
+ export interface UISelectionRange {
3
+ startOffset: number;
4
+ endOffset: number;
5
+ }
6
+ export interface UISelection {
7
+ anchorOffset: number;
8
+ focusOffset: number;
9
+ }
10
+ declare global {
11
+ interface Element {
12
+ [UISelection]?: UISelection;
13
+ }
14
+ }
15
+ export declare function prepareSelectionInterceptor(element: HTMLInputElement | HTMLTextAreaElement): void;
16
+ export declare function setUISelection(element: HTMLInputElement | HTMLTextAreaElement, { focusOffset: focusOffsetParam, anchorOffset: anchorOffsetParam, }: {
17
+ anchorOffset?: number;
18
+ focusOffset: number;
19
+ }, mode?: 'replace' | 'modify'): void;
20
+ export declare function getUISelection(element: HTMLInputElement | HTMLTextAreaElement): {
21
+ startOffset: number;
22
+ endOffset: number;
23
+ anchorOffset: number;
24
+ focusOffset: number;
25
+ };
26
+ export declare function clearUISelection(element: HTMLInputElement | HTMLTextAreaElement): void;
27
+ export {};
@@ -5,63 +5,86 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.clearUISelection = clearUISelection;
7
7
  exports.getUISelection = getUISelection;
8
- exports.hasUISelection = hasUISelection;
9
8
  exports.prepareSelectionInterceptor = prepareSelectionInterceptor;
10
9
  exports.setUISelection = setUISelection;
11
10
 
11
+ var _ = require(".");
12
+
12
13
  var _interceptor = require("./interceptor");
13
14
 
14
15
  const UISelection = Symbol('Displayed selection in UI');
15
16
 
16
- function setSelectionInterceptor(start, end, direction = 'none') {
17
- const isUI = start && typeof start === 'object' && start[UISelection];
18
- this[UISelection] = isUI ? {
19
- start: start.valueOf(),
20
- end: Number(end)
21
- } : undefined;
22
- return [Number(start), end, direction];
23
- }
24
-
25
17
  function prepareSelectionInterceptor(element) {
26
- (0, _interceptor.prepareInterceptor)(element, 'setSelectionRange', setSelectionInterceptor);
18
+ (0, _interceptor.prepareInterceptor)(element, 'setSelectionRange', function interceptorImpl(start, end, direction = 'none') {
19
+ const isUI = start && typeof start === 'object' && start[UISelection];
20
+
21
+ if (!isUI) {
22
+ this[UISelection] = undefined;
23
+ }
24
+
25
+ return {
26
+ realArgs: [Number(start), end, direction]
27
+ };
28
+ });
29
+ (0, _interceptor.prepareInterceptor)(element, 'selectionStart', function interceptorImpl(v) {
30
+ this[UISelection] = undefined;
31
+ return {
32
+ realArgs: v
33
+ };
34
+ });
35
+ (0, _interceptor.prepareInterceptor)(element, 'selectionEnd', function interceptorImpl(v) {
36
+ this[UISelection] = undefined;
37
+ return {
38
+ realArgs: v
39
+ };
40
+ });
27
41
  }
28
42
 
29
- function setUISelection(element, start, end) {
43
+ function setUISelection(element, {
44
+ focusOffset: focusOffsetParam,
45
+ anchorOffset: anchorOffsetParam = focusOffsetParam
46
+ }, mode = 'replace') {
47
+ const valueLength = (0, _.getUIValue)(element).length;
48
+
49
+ const sanitizeOffset = o => Math.max(0, Math.min(valueLength, o));
50
+
51
+ const anchorOffset = mode === 'replace' || element[UISelection] === undefined ? sanitizeOffset(anchorOffsetParam) : element[UISelection].anchorOffset;
52
+ const focusOffset = sanitizeOffset(focusOffsetParam);
53
+ const startOffset = Math.min(anchorOffset, focusOffset);
54
+ const endOffset = Math.max(anchorOffset, focusOffset);
30
55
  element[UISelection] = {
31
- start,
32
- end
56
+ anchorOffset,
57
+ focusOffset
33
58
  };
34
59
 
35
- if (element.selectionStart === start && element.selectionEnd === end) {
60
+ if (element.selectionStart === startOffset && element.selectionEnd === endOffset) {
36
61
  return;
37
62
  } // eslint-disable-next-line no-new-wrappers
38
63
 
39
64
 
40
- const startObj = new Number(start);
65
+ const startObj = new Number(startOffset);
41
66
  startObj[UISelection] = UISelection;
42
67
 
43
68
  try {
44
- element.setSelectionRange(startObj, end);
69
+ element.setSelectionRange(startObj, endOffset);
45
70
  } catch {// DOMException for invalid state is expected when calling this
46
71
  // on an element without support for setSelectionRange
47
72
  }
48
73
  }
49
74
 
50
75
  function getUISelection(element) {
51
- const ui = element[UISelection];
52
- return ui === undefined ? {
53
- selectionStart: element.selectionStart,
54
- selectionEnd: element.selectionEnd
55
- } : {
56
- selectionStart: ui.start,
57
- selectionEnd: ui.end
76
+ var _element$UISelection, _element$selectionSta, _element$selectionEnd;
77
+
78
+ const sel = (_element$UISelection = element[UISelection]) != null ? _element$UISelection : {
79
+ anchorOffset: (_element$selectionSta = element.selectionStart) != null ? _element$selectionSta : 0,
80
+ focusOffset: (_element$selectionEnd = element.selectionEnd) != null ? _element$selectionEnd : 0
81
+ };
82
+ return { ...sel,
83
+ startOffset: Math.min(sel.anchorOffset, sel.focusOffset),
84
+ endOffset: Math.max(sel.anchorOffset, sel.focusOffset)
58
85
  };
59
86
  }
60
87
 
61
88
  function clearUISelection(element) {
62
89
  element[UISelection] = undefined;
63
- }
64
-
65
- function hasUISelection(element) {
66
- return Boolean(element[UISelection]);
67
90
  }
@@ -1,14 +1,18 @@
1
1
  declare const UIValue: unique symbol;
2
2
  declare const InitialValue: unique symbol;
3
+ declare const TrackChanges: unique symbol;
3
4
  declare global {
4
5
  interface Element {
5
6
  [UIValue]?: string;
6
7
  [InitialValue]?: string;
8
+ [TrackChanges]?: string[];
7
9
  }
8
10
  }
9
11
  export declare function prepareValueInterceptor(element: HTMLInputElement): void;
10
12
  export declare function setUIValue(element: HTMLInputElement | HTMLTextAreaElement, value: string): void;
11
- export declare function getUIValue(element: HTMLInputElement | HTMLTextAreaElement): string | undefined;
13
+ export declare function getUIValue(element: HTMLInputElement | HTMLTextAreaElement): string;
12
14
  export declare function setInitialValue(element: HTMLInputElement | HTMLTextAreaElement): void;
13
15
  export declare function getInitialValue(element: HTMLInputElement | HTMLTextAreaElement): string | undefined;
16
+ export declare function startTrackValue(element: HTMLInputElement | HTMLTextAreaElement): void;
17
+ export declare function endTrackValue(element: HTMLInputElement | HTMLTextAreaElement): string[] | undefined;
14
18
  export {};
@@ -3,13 +3,13 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ exports.endTrackValue = endTrackValue;
6
7
  exports.getInitialValue = getInitialValue;
7
8
  exports.getUIValue = getUIValue;
8
9
  exports.prepareValueInterceptor = prepareValueInterceptor;
9
10
  exports.setInitialValue = setInitialValue;
10
11
  exports.setUIValue = setUIValue;
11
-
12
- var _applyNative = require("./applyNative");
12
+ exports.startTrackValue = startTrackValue;
13
13
 
14
14
  var _interceptor = require("./interceptor");
15
15
 
@@ -17,19 +17,24 @@ var _selection = require("./selection");
17
17
 
18
18
  const UIValue = Symbol('Displayed value in UI');
19
19
  const InitialValue = Symbol('Initial value to compare on blur');
20
+ const TrackChanges = Symbol('Track programmatic changes for React workaround');
20
21
 
21
22
  function valueInterceptor(v) {
22
23
  const isUI = typeof v === 'object' && v[UIValue];
23
24
  this[UIValue] = isUI ? String(v) : undefined;
24
25
 
25
26
  if (!isUI) {
27
+ trackValue(this, String(v));
26
28
  this[InitialValue] = String(v); // Programmatically setting the value property
27
29
  // moves the cursor to the end of the input.
28
30
 
29
31
  (0, _selection.clearUISelection)(this);
30
32
  }
31
33
 
32
- return String(v);
34
+ return {
35
+ applyNative: !!isUI,
36
+ realArgs: String(v)
37
+ };
33
38
  }
34
39
 
35
40
  function prepareValueInterceptor(element) {
@@ -37,14 +42,14 @@ function prepareValueInterceptor(element) {
37
42
  }
38
43
 
39
44
  function setUIValue(element, value) {
40
- (0, _applyNative.applyNative)(element, 'value', {
45
+ element.value = {
41
46
  [UIValue]: UIValue,
42
47
  toString: () => value
43
- });
48
+ };
44
49
  }
45
50
 
46
51
  function getUIValue(element) {
47
- return element[UIValue] === undefined ? element.value : element[UIValue];
52
+ return element[UIValue] === undefined ? element.value : String(element[UIValue]);
48
53
  }
49
54
 
50
55
  function setInitialValue(element) {
@@ -53,4 +58,20 @@ function setInitialValue(element) {
53
58
 
54
59
  function getInitialValue(element) {
55
60
  return element[InitialValue];
61
+ }
62
+
63
+ function startTrackValue(element) {
64
+ element[TrackChanges] = [];
65
+ }
66
+
67
+ function trackValue(element, v) {
68
+ var _element$TrackChanges;
69
+
70
+ (_element$TrackChanges = element[TrackChanges]) == null ? void 0 : _element$TrackChanges.push(v);
71
+ }
72
+
73
+ function endTrackValue(element) {
74
+ const tracked = element[TrackChanges];
75
+ element[TrackChanges] = undefined;
76
+ return tracked;
56
77
  }
@@ -1,4 +1,4 @@
1
1
  import type { UserEvent } from './setup';
2
2
  import { PointerOptions } from './utils';
3
- export declare function hover(this: UserEvent, element: Element, init?: MouseEventInit, { skipPointerEventsCheck }?: PointerOptions): void;
4
- export declare function unhover(this: UserEvent, element: Element, init?: MouseEventInit, { skipPointerEventsCheck }?: PointerOptions): void;
3
+ export declare function hover(this: UserEvent, element: Element, { skipPointerEventsCheck }?: PointerOptions): void;
4
+ export declare function unhover(this: UserEvent, element: Element, { skipPointerEventsCheck }?: PointerOptions): void;
package/dist/hover.js CHANGED
@@ -10,7 +10,7 @@ var _pointer = require("./pointer");
10
10
 
11
11
  var _utils = require("./utils");
12
12
 
13
- function hover(element, init, {
13
+ function hover(element, {
14
14
  skipPointerEventsCheck = false
15
15
  } = {}) {
16
16
  if (!skipPointerEventsCheck && !(0, _utils.hasPointerEvents)(element)) {
@@ -26,7 +26,7 @@ function hover(element, init, {
26
26
  });
27
27
  }
28
28
 
29
- function unhover(element, init, {
29
+ function unhover(element, {
30
30
  skipPointerEventsCheck = false
31
31
  } = {}) {
32
32
  if (!skipPointerEventsCheck && !(0, _utils.hasPointerEvents)(element)) {
@@ -1,8 +1,6 @@
1
- import { specialCharMap } from './keyboard';
2
1
  import { UserEventApis, setup } from './setup';
3
2
  declare const userEvent: UserEventApis & {
4
3
  setup: typeof setup;
5
4
  };
6
5
  export default userEvent;
7
- export { specialCharMap as specialChars };
8
6
  export type { keyboardKey } from './keyboard';
package/dist/index.js CHANGED
@@ -4,14 +4,6 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
- Object.defineProperty(exports, "specialChars", {
8
- enumerable: true,
9
- get: function () {
10
- return _keyboard.specialCharMap;
11
- }
12
- });
13
-
14
- var _keyboard = require("./keyboard");
15
7
 
16
8
  var _setup = require("./setup");
17
9
 
@@ -6,8 +6,6 @@ export declare function getKeyEventProps(keyDef: keyboardKey, state: keyboardSta
6
6
  ctrlKey: boolean;
7
7
  metaKey: boolean;
8
8
  shiftKey: boolean;
9
- /** @deprecated use code instead */
10
- keyCode: number | undefined;
11
9
  };
12
10
  export declare function getMouseEventProps(state: keyboardState): {
13
11
  altKey: boolean;
@@ -7,19 +7,13 @@ exports.getKeyEventProps = getKeyEventProps;
7
7
  exports.getMouseEventProps = getMouseEventProps;
8
8
 
9
9
  function getKeyEventProps(keyDef, state) {
10
- var _keyDef$keyCode, _keyDef$key;
11
-
12
10
  return {
13
11
  key: keyDef.key,
14
12
  code: keyDef.code,
15
13
  altKey: state.modifiers.alt,
16
14
  ctrlKey: state.modifiers.ctrl,
17
15
  metaKey: state.modifiers.meta,
18
- shiftKey: state.modifiers.shift,
19
-
20
- /** @deprecated use code instead */
21
- keyCode: (_keyDef$keyCode = keyDef.keyCode) != null ? _keyDef$keyCode : // istanbul ignore next
22
- ((_keyDef$key = keyDef.key) == null ? void 0 : _keyDef$key.length) === 1 ? keyDef.key.charCodeAt(0) : undefined
16
+ shiftKey: state.modifiers.shift
23
17
  };
24
18
  }
25
19
 
@@ -8,7 +8,6 @@ import { keyboardKey, keyboardOptions } from './types';
8
8
  * Keeping the key pressed can be written as `{key>}`.
9
9
  * When keeping the key pressed you can choose how long (how many keydown and keypress) the key is pressed `{key>3}`.
10
10
  * You can then release the key per `{key>3/}` or keep it pressed and continue with the next key.
11
- * Modifiers like `{shift}` imply being kept pressed. This can be turned of per `{shift/}`.
12
11
  */
13
12
  export declare function getNextKeyDef(text: string, options: keyboardOptions): {
14
13
  keyDef: keyboardKey;
@@ -7,16 +7,6 @@ exports.getNextKeyDef = getNextKeyDef;
7
7
 
8
8
  var _utils = require("../utils");
9
9
 
10
- var legacyModifiers;
11
-
12
- (function (legacyModifiers) {
13
- legacyModifiers["alt"] = "alt";
14
- legacyModifiers["ctrl"] = "ctrl";
15
- legacyModifiers["meta"] = "meta";
16
- legacyModifiers["shift"] = "shift";
17
- })(legacyModifiers || (legacyModifiers = {}));
18
-
19
- var legacyKeyMap;
20
10
  /**
21
11
  * Get the next key from keyMap
22
12
  *
@@ -26,16 +16,7 @@ var legacyKeyMap;
26
16
  * Keeping the key pressed can be written as `{key>}`.
27
17
  * When keeping the key pressed you can choose how long (how many keydown and keypress) the key is pressed `{key>3}`.
28
18
  * You can then release the key per `{key>3/}` or keep it pressed and continue with the next key.
29
- * Modifiers like `{shift}` imply being kept pressed. This can be turned of per `{shift/}`.
30
19
  */
31
-
32
- (function (legacyKeyMap) {
33
- legacyKeyMap["ctrl"] = "Control";
34
- legacyKeyMap["del"] = "Delete";
35
- legacyKeyMap["esc"] = "Escape";
36
- legacyKeyMap["space"] = " ";
37
- })(legacyKeyMap || (legacyKeyMap = {}));
38
-
39
20
  function getNextKeyDef(text, options) {
40
21
  var _options$keyboardMap$;
41
22
 
@@ -44,7 +25,7 @@ function getNextKeyDef(text, options) {
44
25
  descriptor,
45
26
  consumedLength,
46
27
  releasePrevious,
47
- releaseSelf = !(type === '{' && getEnumValue(legacyModifiers, descriptor.toLowerCase())),
28
+ releaseSelf = true,
48
29
  repeat
49
30
  } = (0, _utils.readNextDescriptor)(text);
50
31
  const keyDef = (_options$keyboardMap$ = options.keyboardMap.find(def => {
@@ -55,8 +36,7 @@ function getNextKeyDef(text, options) {
55
36
  } else if (type === '{') {
56
37
  var _def$key;
57
38
 
58
- const key = mapLegacyKey(descriptor);
59
- return ((_def$key = def.key) == null ? void 0 : _def$key.toLowerCase()) === key.toLowerCase();
39
+ return ((_def$key = def.key) == null ? void 0 : _def$key.toLowerCase()) === descriptor.toLowerCase();
60
40
  }
61
41
 
62
42
  return def.key === descriptor;
@@ -72,14 +52,4 @@ function getNextKeyDef(text, options) {
72
52
  releaseSelf,
73
53
  repeat
74
54
  };
75
- }
76
-
77
- function getEnumValue(f, key) {
78
- return f[key];
79
- }
80
-
81
- function mapLegacyKey(descriptor) {
82
- var _getEnumValue;
83
-
84
- return (_getEnumValue = getEnumValue(legacyKeyMap, descriptor)) != null ? _getEnumValue : descriptor;
85
55
  }
@@ -1,5 +1,4 @@
1
1
  import { keyboardState, keyboardOptions, keyboardKey } from './types';
2
- export { specialCharMap } from './specialCharMap';
3
2
  export type { keyboardOptions, keyboardKey };
4
3
  export declare function keyboard(text: string, options?: Partial<keyboardOptions & {
5
4
  keyboardState: keyboardState;