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

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 (161) 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} +1 -0
  4. package/dist/click.js +17 -0
  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} +0 -0
  14. package/dist/{src/index.d.ts → index.d.ts} +0 -0
  15. package/dist/{src/keyboard → keyboard}/getEventProps.d.ts +0 -0
  16. package/dist/{src/keyboard → keyboard}/getNextKeyDef.d.ts +0 -0
  17. package/dist/{src/keyboard → keyboard}/index.d.ts +0 -0
  18. package/dist/{src/keyboard → keyboard}/keyMap.d.ts +0 -0
  19. package/dist/keyboard/keyMap.js +4 -0
  20. package/dist/{src/keyboard → keyboard}/keyboardImplementation.d.ts +0 -0
  21. package/dist/keyboard/keyboardImplementation.js +3 -1
  22. package/dist/{src/keyboard → keyboard}/plugins/arrow.d.ts +0 -0
  23. package/dist/keyboard/plugins/arrow.js +7 -12
  24. package/dist/{src/keyboard → keyboard}/plugins/character.d.ts +0 -0
  25. package/dist/keyboard/plugins/character.js +27 -45
  26. package/dist/keyboard/plugins/combination.d.ts +5 -0
  27. package/dist/keyboard/plugins/combination.js +17 -0
  28. package/dist/{src/keyboard → keyboard}/plugins/control.d.ts +0 -0
  29. package/dist/keyboard/plugins/control.js +4 -12
  30. package/dist/{src/keyboard → keyboard}/plugins/functional.d.ts +0 -0
  31. package/dist/keyboard/plugins/functional.js +23 -11
  32. package/dist/{src/keyboard → keyboard}/plugins/index.d.ts +0 -0
  33. package/dist/keyboard/plugins/index.js +3 -1
  34. package/dist/{src/keyboard → keyboard}/shared/fireChangeForInputTimeIfValid.d.ts +0 -0
  35. package/dist/{src/keyboard → keyboard}/shared/index.d.ts +0 -0
  36. package/dist/{src/keyboard → keyboard}/specialCharMap.d.ts +0 -0
  37. package/dist/{src/keyboard → keyboard}/types.d.ts +0 -0
  38. package/dist/{src/paste.d.ts → paste.d.ts} +0 -0
  39. package/dist/paste.js +8 -15
  40. package/dist/{src/pointer → pointer}/index.d.ts +0 -0
  41. package/dist/{src/pointer → pointer}/keyMap.d.ts +0 -0
  42. package/dist/{src/pointer → pointer}/parseKeyDef.d.ts +0 -0
  43. package/dist/{src/pointer → pointer}/pointerAction.d.ts +2 -6
  44. package/dist/pointer/pointerAction.js +2 -26
  45. package/dist/pointer/pointerMove.d.ts +5 -0
  46. package/dist/pointer/pointerMove.js +49 -5
  47. package/dist/pointer/pointerPress.d.ts +7 -0
  48. package/dist/pointer/pointerPress.js +135 -29
  49. package/dist/pointer/resolveSelectionTarget.d.ts +5 -0
  50. package/dist/pointer/resolveSelectionTarget.js +75 -0
  51. package/dist/{src/pointer → pointer}/types.d.ts +18 -4
  52. package/dist/{src/selectOptions.d.ts → selectOptions.d.ts} +0 -0
  53. package/dist/{src/setup.d.ts → setup.d.ts} +5 -2
  54. package/dist/setup.js +4 -7
  55. package/dist/tab.d.ts +5 -0
  56. package/dist/tab.js +3 -132
  57. package/dist/{src/type.d.ts → type.d.ts} +0 -0
  58. package/dist/type.js +3 -19
  59. package/dist/{src/upload.d.ts → upload.d.ts} +0 -0
  60. package/dist/{src/utils → utils}/click/isClickableInput.d.ts +0 -0
  61. package/dist/{src/utils → utils}/edit/buildTimeValue.d.ts +0 -0
  62. package/dist/utils/edit/calculateNewValue.d.ts +14 -0
  63. package/dist/utils/edit/calculateNewValue.js +19 -21
  64. package/dist/utils/edit/editInputElement.d.ts +16 -0
  65. package/dist/utils/edit/editInputElement.js +53 -0
  66. package/dist/{src/utils → utils}/edit/getValue.d.ts +0 -0
  67. package/dist/utils/edit/getValue.js +1 -3
  68. package/dist/utils/edit/isContentEditable.d.ts +7 -0
  69. package/dist/utils/edit/isContentEditable.js +14 -0
  70. package/dist/{src/utils → utils}/edit/isEditable.d.ts +1 -0
  71. package/dist/{src/utils → utils}/edit/isValidDateValue.d.ts +0 -0
  72. package/dist/{src/utils → utils}/edit/isValidInputTimeValue.d.ts +0 -0
  73. package/dist/{src/utils → utils}/edit/maxLength.d.ts +0 -0
  74. package/dist/utils/edit/prepareInput.d.ts +7 -0
  75. package/dist/utils/edit/prepareInput.js +77 -0
  76. package/dist/{src/utils → utils}/focus/blur.d.ts +0 -0
  77. package/dist/{src/utils → utils}/focus/focus.d.ts +0 -0
  78. package/dist/utils/focus/focus.js +3 -0
  79. package/dist/{src/utils → utils}/focus/getActiveElement.d.ts +0 -0
  80. package/dist/utils/focus/getTabDestination.d.ts +1 -0
  81. package/dist/utils/focus/getTabDestination.js +71 -0
  82. package/dist/{src/utils → utils}/focus/isFocusable.d.ts +0 -0
  83. package/dist/utils/focus/selectAll.d.ts +5 -0
  84. package/dist/utils/focus/selectAll.js +51 -0
  85. package/dist/utils/focus/selection.d.ts +42 -0
  86. package/dist/utils/focus/selection.js +190 -0
  87. package/dist/{src/utils → utils}/focus/selector.d.ts +0 -0
  88. package/dist/{src/utils → utils}/index.d.ts +5 -5
  89. package/dist/utils/index.js +47 -47
  90. package/dist/{src/utils → utils}/keyDef/readNextDescriptor.d.ts +0 -0
  91. package/dist/{src/utils → utils}/misc/eventWrapper.d.ts +0 -0
  92. package/dist/{src/utils → utils}/misc/findClosest.d.ts +0 -0
  93. package/dist/{src/utils → utils}/misc/hasFormSubmit.d.ts +0 -0
  94. package/dist/{src/utils → utils}/misc/hasPointerEvents.d.ts +0 -0
  95. package/dist/{src/utils → utils}/misc/isDescendantOrSelf.d.ts +0 -0
  96. package/dist/{src/utils → utils}/misc/isDisabled.d.ts +0 -0
  97. package/dist/{src/utils → utils}/misc/isElementType.d.ts +0 -0
  98. package/dist/{src/utils → utils}/misc/isVisible.d.ts +0 -0
  99. package/dist/{src/utils → utils}/misc/wait.d.ts +0 -0
  100. package/dist/{src/utils → utils}/pointer/firePointerEvents.d.ts +13 -11
  101. package/dist/utils/pointer/firePointerEvents.js +62 -5
  102. package/dist/{src/utils → utils}/pointer/mouseButtons.d.ts +0 -0
  103. package/package.json +2 -2
  104. package/dist/document/applyNative.js +0 -28
  105. package/dist/src/document/applyNative.d.ts +0 -8
  106. package/dist/src/document/index.d.ts +0 -10
  107. package/dist/src/document/interceptor.d.ts +0 -5
  108. package/dist/src/document/selection.d.ts +0 -18
  109. package/dist/src/pointer/pointerMove.d.ts +0 -5
  110. package/dist/src/pointer/pointerPress.d.ts +0 -7
  111. package/dist/src/tab.d.ts +0 -6
  112. package/dist/src/utils/edit/calculateNewValue.d.ts +0 -7
  113. package/dist/src/utils/edit/cursorPosition.d.ts +0 -2
  114. package/dist/src/utils/edit/fireInputEvent.d.ts +0 -8
  115. package/dist/src/utils/edit/isContentEditable.d.ts +0 -3
  116. package/dist/src/utils/edit/selectionRange.d.ts +0 -5
  117. package/dist/src/utils/misc/isDocument.d.ts +0 -1
  118. package/dist/src/utils/pointer/fakeEvent.d.ts +0 -16
  119. package/dist/tests/_helpers/custom-element.d.ts +0 -8
  120. package/dist/tests/_helpers/utils.d.ts +0 -73
  121. package/dist/tests/_setup-env.d.ts +0 -1
  122. package/dist/tests/clear.d.ts +0 -1
  123. package/dist/tests/click/click.d.ts +0 -1
  124. package/dist/tests/click/dblclick.d.ts +0 -1
  125. package/dist/tests/document/index.d.ts +0 -1
  126. package/dist/tests/document/selectionRange.d.ts +0 -1
  127. package/dist/tests/hover/hover.d.ts +0 -1
  128. package/dist/tests/hover/unhover.d.ts +0 -1
  129. package/dist/tests/keyboard/getNextKeyDef.d.ts +0 -1
  130. package/dist/tests/keyboard/index.d.ts +0 -1
  131. package/dist/tests/keyboard/keyboardImplementation.d.ts +0 -1
  132. package/dist/tests/keyboard/plugin/arrow.d.ts +0 -1
  133. package/dist/tests/keyboard/plugin/character.d.ts +0 -1
  134. package/dist/tests/keyboard/plugin/control.d.ts +0 -1
  135. package/dist/tests/keyboard/plugin/functional.d.ts +0 -1
  136. package/dist/tests/keyboard/shared/fireInputEvent.d.ts +0 -1
  137. package/dist/tests/paste.d.ts +0 -1
  138. package/dist/tests/pointer/index.d.ts +0 -1
  139. package/dist/tests/react/keyboard.d.ts +0 -1
  140. package/dist/tests/react/type.d.ts +0 -1
  141. package/dist/tests/selectOptions/deselect.d.ts +0 -1
  142. package/dist/tests/selectOptions/select.d.ts +0 -1
  143. package/dist/tests/setup.d.ts +0 -10
  144. package/dist/tests/tab.d.ts +0 -1
  145. package/dist/tests/type/index.d.ts +0 -1
  146. package/dist/tests/type/modifiers.d.ts +0 -1
  147. package/dist/tests/upload.d.ts +0 -1
  148. package/dist/tests/utils/edit/calculateNewValue.d.ts +0 -1
  149. package/dist/tests/utils/edit/isContentEditable.d.ts +0 -1
  150. package/dist/tests/utils/focus/blur.d.ts +0 -1
  151. package/dist/tests/utils/focus/focus.d.ts +0 -1
  152. package/dist/tests/utils/misc/hasPointerEvents.d.ts +0 -1
  153. package/dist/tests/utils/misc/isDescendantOrSelf.d.ts +0 -1
  154. package/dist/tests/utils/misc/isElementType.d.ts +0 -1
  155. package/dist/tests/utils/misc/isVisible.d.ts +0 -1
  156. package/dist/utils/edit/cursorPosition.js +0 -35
  157. package/dist/utils/edit/fireInputEvent.js +0 -55
  158. package/dist/utils/edit/selectionRange.js +0 -64
  159. package/dist/utils/misc/dom-helpers.d.ts +0 -3
  160. package/dist/utils/misc/isDocument.js +0 -10
  161. package/dist/utils/pointer/fakeEvent.js +0 -82
@@ -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;
@@ -1,5 +1,5 @@
1
1
  import { clear } from './clear';
2
- import { click, clickOptions, dblClick } from './click';
2
+ import { click, clickOptions, dblClick, tripleClick } from './click';
3
3
  import { hover, unhover } from './hover';
4
4
  import { keyboard, keyboardOptions } from './keyboard';
5
5
  import type { keyboardState } from './keyboard/types';
@@ -22,6 +22,7 @@ export declare const userEventApis: {
22
22
  pointer: typeof pointer;
23
23
  selectOptions: typeof selectOptions;
24
24
  tab: typeof tab;
25
+ tripleClick: typeof tripleClick;
25
26
  type: typeof type;
26
27
  unhover: typeof unhover;
27
28
  upload: typeof upload;
@@ -54,6 +55,7 @@ export declare function setup(options?: SetupOptions): {
54
55
  pointer: typeof pointer;
55
56
  selectOptions: typeof selectOptions;
56
57
  tab: typeof tab;
58
+ tripleClick: typeof tripleClick;
57
59
  type: typeof type;
58
60
  unhover: typeof unhover;
59
61
  upload: typeof upload;
@@ -72,12 +74,13 @@ export declare function setup(options?: SetupOptions): {
72
74
  pointer: typeof pointer;
73
75
  selectOptions: typeof selectOptions;
74
76
  tab: typeof tab;
77
+ tripleClick: typeof tripleClick;
75
78
  type: typeof type;
76
79
  unhover: typeof unhover;
77
80
  upload: typeof upload;
78
81
  } & any;
79
82
  };
80
- declare function _setup({ applyAccept, autoModify, delay, document, focusTrap, keyboardMap, pointerMap, skipAutoClose, skipClick, skipHover, skipPointerEventsCheck, }: SetupOptions, { keyboardState, pointerState, }: {
83
+ declare function _setup({ applyAccept, autoModify, delay, document, keyboardMap, pointerMap, skipAutoClose, skipClick, skipHover, skipPointerEventsCheck, }: SetupOptions, { keyboardState, pointerState, }: {
81
84
  keyboardState: keyboardState;
82
85
  pointerState: pointerState;
83
86
  }): UserEventApis & {
package/dist/setup.js CHANGED
@@ -39,6 +39,7 @@ const userEventApis = {
39
39
  pointer: _pointer.pointer,
40
40
  selectOptions: _selectOptions.selectOptions,
41
41
  tab: _tab.tab,
42
+ tripleClick: _click.tripleClick,
42
43
  type: _type.type,
43
44
  unhover: _hover.unhover,
44
45
  upload: _upload.upload
@@ -64,7 +65,6 @@ function _setup({
64
65
  autoModify,
65
66
  delay = 0,
66
67
  document,
67
- focusTrap,
68
68
  keyboardMap,
69
69
  pointerMap,
70
70
  skipAutoClose,
@@ -91,9 +91,6 @@ function _setup({
91
91
  const clickDefaults = {
92
92
  skipHover
93
93
  };
94
- const tabDefaults = {
95
- focusTrap
96
- };
97
94
  const typeDefaults = {
98
95
  delay,
99
96
  skipAutoClose,
@@ -177,11 +174,11 @@ function _setup({
177
174
  });
178
175
  },
179
176
  tab: (...args) => {
180
- args[0] = { ...tabDefaults,
181
- ...args[0]
182
- };
183
177
  return _tab.tab.call(userEvent, ...args);
184
178
  },
179
+ tripleClick: (...args) => {
180
+ return _click.tripleClick.call(userEvent, ...args);
181
+ },
185
182
  // type needs typecasting because of the overloading
186
183
  type: (...args) => {
187
184
  args[2] = { ...typeDefaults,
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
@@ -0,0 +1,14 @@
1
+ import { EditableInputType } from './isEditable';
2
+ /**
3
+ * Calculate a new text value.
4
+ */
5
+ export declare function calculateNewValue(inputData: string, node: (HTMLInputElement & {
6
+ type: EditableInputType;
7
+ }) | HTMLTextAreaElement, { startOffset, endOffset, }: {
8
+ startOffset: number;
9
+ endOffset: number;
10
+ }, inputType?: string): {
11
+ oldValue: string;
12
+ newValue: string;
13
+ newOffset: number;
14
+ };
@@ -5,44 +5,42 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.calculateNewValue = calculateNewValue;
7
7
 
8
- var _selectionRange = require("./selectionRange");
9
-
10
- var _getValue2 = require("./getValue");
8
+ var _document = require("../../document");
11
9
 
12
10
  var _isValidDateValue = require("./isValidDateValue");
13
11
 
14
12
  var _isValidInputTimeValue = require("./isValidInputTimeValue");
15
13
 
16
- function calculateNewValue(newEntry, element, value = (() => {
17
- var _getValue;
18
-
19
- return (_getValue = (0, _getValue2.getValue)(element)) != null ? _getValue :
20
- /* istanbul ignore next */
21
- '';
22
- })(), selectionRange = (0, _selectionRange.getSelectionRange)(element), deleteContent) {
23
- const selectionStart = selectionRange.selectionStart === null ? value.length : selectionRange.selectionStart;
24
- const selectionEnd = selectionRange.selectionEnd === null ? value.length : selectionRange.selectionEnd;
25
- const prologEnd = Math.max(0, selectionStart === selectionEnd && deleteContent === 'backward' ? selectionStart - 1 : selectionStart);
14
+ /**
15
+ * Calculate a new text value.
16
+ */
17
+ function calculateNewValue(inputData, node, {
18
+ startOffset,
19
+ endOffset
20
+ }, inputType) {
21
+ const value = (0, _document.getUIValue)(node);
22
+ const prologEnd = Math.max(0, startOffset === endOffset && inputType === 'deleteContentBackward' ? startOffset - 1 : startOffset);
26
23
  const prolog = value.substring(0, prologEnd);
27
- const epilogStart = Math.min(value.length, selectionStart === selectionEnd && deleteContent === 'forward' ? selectionEnd + 1 : selectionEnd);
24
+ const epilogStart = Math.min(value.length, startOffset === endOffset && inputType === 'deleteContentForward' ? startOffset + 1 : endOffset);
28
25
  const epilog = value.substring(epilogStart, value.length);
29
- let newValue = `${prolog}${newEntry}${epilog}`;
30
- const newSelectionStart = prologEnd + newEntry.length;
26
+ let newValue = `${prolog}${inputData}${epilog}`;
27
+ const newOffset = prologEnd + inputData.length;
31
28
 
32
- if (element.type === 'date' && !(0, _isValidDateValue.isValidDateValue)(element, newValue)) {
29
+ if (node.type === 'date' && !(0, _isValidDateValue.isValidDateValue)(node, newValue)) {
33
30
  newValue = value;
34
31
  }
35
32
 
36
- if (element.type === 'time' && !(0, _isValidInputTimeValue.isValidInputTimeValue)(element, newValue)) {
37
- if ((0, _isValidInputTimeValue.isValidInputTimeValue)(element, newEntry)) {
38
- newValue = newEntry;
33
+ if (node.type === 'time' && !(0, _isValidInputTimeValue.isValidInputTimeValue)(node, newValue)) {
34
+ if ((0, _isValidInputTimeValue.isValidInputTimeValue)(node, inputData)) {
35
+ newValue = inputData;
39
36
  } else {
40
37
  newValue = value;
41
38
  }
42
39
  }
43
40
 
44
41
  return {
42
+ oldValue: value,
45
43
  newValue,
46
- newSelectionStart
44
+ newOffset
47
45
  };
48
46
  }
@@ -0,0 +1,16 @@
1
+ import { fireEvent } from '@testing-library/dom';
2
+ /**
3
+ * Change the value of an element as if it was changed as a result of a user input.
4
+ *
5
+ * Fires the input event.
6
+ */
7
+ export declare function editInputElement(element: HTMLInputElement | HTMLTextAreaElement, { newValue, newSelection, eventOverrides, }: {
8
+ newValue: string;
9
+ newSelection: {
10
+ node: Node;
11
+ offset: number;
12
+ };
13
+ eventOverrides: Partial<Parameters<typeof fireEvent>[1]> & {
14
+ [k: string]: unknown;
15
+ };
16
+ }): void;
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.editInputElement = editInputElement;
7
+
8
+ var _dom = require("@testing-library/dom");
9
+
10
+ var _document = require("../../document");
11
+
12
+ var _selection = require("../focus/selection");
13
+
14
+ /**
15
+ * Change the value of an element as if it was changed as a result of a user input.
16
+ *
17
+ * Fires the input event.
18
+ */
19
+ function editInputElement(element, {
20
+ newValue,
21
+ newSelection,
22
+ eventOverrides
23
+ }) {
24
+ const oldValue = element.value; // apply the changes before firing the input event, so that input handlers can access the altered dom and selection
25
+
26
+ (0, _document.setUIValue)(element, newValue);
27
+ (0, _selection.setSelection)({
28
+ focusNode: newSelection.node,
29
+ anchorOffset: newSelection.offset,
30
+ focusOffset: newSelection.offset
31
+ }); // When the input event happens in the browser, React executes all event handlers
32
+ // and if they change state of a controlled value, nothing happens.
33
+ // But when we trigger the event handlers in test environment,
34
+ // the changes are rolled back by React before the state update is applied.
35
+ // Then the updated state is applied which results in a resetted cursor.
36
+ // There is probably a better way to work around if we figure out
37
+ // why the batched update is executed differently in our test environment.
38
+
39
+ (0, _document.startTrackValue)(element);
40
+
41
+ _dom.fireEvent.input(element, { ...eventOverrides
42
+ });
43
+
44
+ const tracked = (0, _document.endTrackValue)(element);
45
+
46
+ if ((tracked == null ? void 0 : tracked.length) === 2 && tracked[0] === oldValue && tracked[1] === newValue) {
47
+ (0, _selection.setSelection)({
48
+ focusNode: newSelection.node,
49
+ anchorOffset: newSelection.offset,
50
+ focusOffset: newSelection.offset
51
+ });
52
+ }
53
+ }
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
  }
@@ -0,0 +1,7 @@
1
+ export declare function isContentEditable(element: Element): element is HTMLElement & {
2
+ contenteditable: 'true';
3
+ };
4
+ /**
5
+ * If a node is a contenteditable or inside one, return that element.
6
+ */
7
+ export declare function getContentEditable(node: Node): Element | null;
@@ -3,9 +3,23 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ exports.getContentEditable = getContentEditable;
6
7
  exports.isContentEditable = isContentEditable;
7
8
 
8
9
  //jsdom is not supporting isContentEditable
9
10
  function isContentEditable(element) {
10
11
  return element.hasAttribute('contenteditable') && (element.getAttribute('contenteditable') == 'true' || element.getAttribute('contenteditable') == '');
12
+ }
13
+ /**
14
+ * If a node is a contenteditable or inside one, return that element.
15
+ */
16
+
17
+
18
+ function getContentEditable(node) {
19
+ const element = getElement(node);
20
+ return element && (element.closest('[contenteditable=""]') || element.closest('[contenteditable="true"]'));
21
+ }
22
+
23
+ function getElement(node) {
24
+ return node.nodeType === 1 ? node : node.parentElement;
11
25
  }
@@ -17,6 +17,7 @@ export declare enum editableInputTypes {
17
17
  'url' = "url",
18
18
  'week' = "week"
19
19
  }
20
+ export declare type EditableInputType = keyof typeof editableInputTypes;
20
21
  export declare function isEditableInput(element: Element): element is HTMLInputElement & {
21
22
  readOnly: false;
22
23
  type: editableInputTypes;
File without changes
@@ -0,0 +1,7 @@
1
+ export declare function prepareInput(data: string, element: Element, inputType?: string): {
2
+ commit: () => void;
3
+ getNewValue?: undefined;
4
+ } | {
5
+ getNewValue: () => string;
6
+ commit: () => void;
7
+ } | undefined;
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.prepareInput = prepareInput;
7
+
8
+ var _dom = require("@testing-library/dom");
9
+
10
+ var _utils = require("../../utils");
11
+
12
+ function prepareInput(data, element, inputType = 'insertText') {
13
+ const inputRange = (0, _utils.getInputRange)(element);
14
+ /* istanbul ignore if */
15
+
16
+ if (!inputRange) {
17
+ return;
18
+ }
19
+
20
+ if ('startContainer' in inputRange) {
21
+ return {
22
+ commit: () => {
23
+ const del = !inputRange.collapsed;
24
+
25
+ if (del) {
26
+ inputRange.deleteContents();
27
+ }
28
+
29
+ if (data) {
30
+ if (inputRange.endContainer.nodeType === 3) {
31
+ const offset = inputRange.endOffset;
32
+ inputRange.endContainer.insertData(offset, data);
33
+ inputRange.setStart(inputRange.endContainer, offset + data.length);
34
+ inputRange.setEnd(inputRange.endContainer, offset + data.length);
35
+ } else {
36
+ const text = element.ownerDocument.createTextNode(data);
37
+ inputRange.insertNode(text);
38
+ inputRange.setStart(text, data.length);
39
+ inputRange.setEnd(text, data.length);
40
+ }
41
+ }
42
+
43
+ if (del || data) {
44
+ _dom.fireEvent.input(element, {
45
+ inputType
46
+ });
47
+ }
48
+ }
49
+ };
50
+ } else {
51
+ return {
52
+ getNewValue: () => (0, _utils.calculateNewValue)(data, element, inputRange, inputType).newValue,
53
+ commit: () => {
54
+ const {
55
+ newValue,
56
+ newOffset,
57
+ oldValue
58
+ } = (0, _utils.calculateNewValue)(data, element, inputRange, inputType);
59
+
60
+ if (newValue === oldValue && newOffset === inputRange.startOffset && newOffset === inputRange.endOffset) {
61
+ return;
62
+ }
63
+
64
+ (0, _utils.editInputElement)(element, {
65
+ newValue,
66
+ newSelection: {
67
+ node: element,
68
+ offset: newOffset
69
+ },
70
+ eventOverrides: {
71
+ inputType
72
+ }
73
+ });
74
+ }
75
+ };
76
+ }
77
+ }
File without changes
File without changes
@@ -11,9 +11,12 @@ var _getActiveElement = require("./getActiveElement");
11
11
 
12
12
  var _isFocusable = require("./isFocusable");
13
13
 
14
+ var _selection = require("./selection");
15
+
14
16
  function focus(element) {
15
17
  if (!(0, _isFocusable.isFocusable)(element)) return;
16
18
  const isAlreadyActive = (0, _getActiveElement.getActiveElement)(element.ownerDocument) === element;
17
19
  if (isAlreadyActive) return;
18
20
  (0, _eventWrapper.eventWrapper)(() => element.focus());
21
+ (0, _selection.updateSelectionOnFocus)(element);
19
22
  }
@@ -0,0 +1 @@
1
+ export declare function getTabDestination(activeElement: Element, shift: boolean): HTMLElement;