@testing-library/user-event 13.5.0 → 14.0.0-alpha.4

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 (100) hide show
  1. package/README.md +3 -0
  2. package/dist/clear.d.ts +2 -2
  3. package/dist/clear.js +1 -3
  4. package/dist/click.d.ts +3 -3
  5. package/dist/click.js +13 -144
  6. package/dist/document/index.d.ts +9 -0
  7. package/dist/document/index.js +100 -0
  8. package/dist/document/interceptor.d.ts +15 -0
  9. package/dist/document/interceptor.js +37 -0
  10. package/dist/document/selection.d.ts +17 -0
  11. package/dist/document/selection.js +64 -0
  12. package/dist/document/value.d.ts +18 -0
  13. package/dist/document/value.js +77 -0
  14. package/dist/hover.d.ts +3 -3
  15. package/dist/hover.js +15 -57
  16. package/dist/index.d.ts +4 -21
  17. package/dist/index.js +4 -29
  18. package/dist/keyboard/getNextKeyDef.js +3 -92
  19. package/dist/keyboard/index.d.ts +1 -0
  20. package/dist/keyboard/index.js +4 -0
  21. package/dist/keyboard/keyMap.js +4 -0
  22. package/dist/keyboard/plugins/character.js +8 -18
  23. package/dist/keyboard/plugins/control.js +3 -6
  24. package/dist/keyboard/plugins/functional.js +20 -6
  25. package/dist/keyboard/plugins/index.js +2 -4
  26. package/dist/keyboard/shared/index.d.ts +0 -2
  27. package/dist/keyboard/shared/index.js +0 -26
  28. package/dist/keyboard/types.d.ts +3 -0
  29. package/dist/paste.d.ts +3 -2
  30. package/dist/pointer/index.d.ts +18 -0
  31. package/dist/pointer/index.js +90 -0
  32. package/dist/pointer/keyMap.d.ts +2 -0
  33. package/dist/pointer/keyMap.js +29 -0
  34. package/dist/pointer/parseKeyDef.d.ts +6 -0
  35. package/dist/pointer/parseKeyDef.js +36 -0
  36. package/dist/pointer/pointerAction.d.ts +6 -0
  37. package/dist/pointer/pointerAction.js +52 -0
  38. package/dist/pointer/pointerMove.d.ts +5 -0
  39. package/dist/pointer/pointerMove.js +125 -0
  40. package/dist/pointer/pointerPress.d.ts +7 -0
  41. package/dist/pointer/pointerPress.js +328 -0
  42. package/dist/pointer/resolveSelectionTarget.d.ts +5 -0
  43. package/dist/pointer/resolveSelectionTarget.js +74 -0
  44. package/dist/pointer/types.d.ts +72 -0
  45. package/dist/pointer/types.js +1 -0
  46. package/dist/selectOptions.d.ts +4 -0
  47. package/dist/{select-options.js → selectOptions.js} +18 -20
  48. package/dist/setup.d.ts +89 -0
  49. package/dist/setup.js +206 -0
  50. package/dist/tab.d.ts +3 -3
  51. package/dist/tab.js +18 -97
  52. package/dist/type.d.ts +14 -0
  53. package/dist/type.js +74 -0
  54. package/dist/upload.d.ts +4 -3
  55. package/dist/upload.js +3 -9
  56. package/dist/{keyboard/shared → utils/edit}/fireInputEvent.d.ts +0 -9
  57. package/dist/utils/edit/fireInputEvent.js +59 -0
  58. package/dist/utils/edit/getValue.d.ts +1 -1
  59. package/dist/utils/edit/getValue.js +3 -1
  60. package/dist/utils/edit/selectionRange.d.ts +0 -11
  61. package/dist/utils/edit/selectionRange.js +9 -49
  62. package/dist/{blur.d.ts → utils/focus/blur.d.ts} +0 -0
  63. package/dist/utils/focus/blur.js +19 -0
  64. package/dist/{focus.d.ts → utils/focus/focus.d.ts} +0 -0
  65. package/dist/utils/focus/focus.js +19 -0
  66. package/dist/utils/focus/getTabDestination.d.ts +1 -0
  67. package/dist/utils/focus/getTabDestination.js +72 -0
  68. package/dist/utils/focus/isFocusable.js +1 -3
  69. package/dist/utils/index.d.ts +10 -3
  70. package/dist/utils/index.js +120 -29
  71. package/dist/utils/keyDef/readNextDescriptor.d.ts +19 -0
  72. package/dist/utils/keyDef/readNextDescriptor.js +102 -0
  73. package/dist/utils/misc/findClosest.d.ts +1 -0
  74. package/dist/utils/misc/findClosest.js +20 -0
  75. package/dist/utils/misc/isDescendantOrSelf.d.ts +1 -0
  76. package/dist/utils/misc/isDescendantOrSelf.js +20 -0
  77. package/dist/utils/pointer/fakeEvent.d.ts +20 -0
  78. package/dist/utils/pointer/fakeEvent.js +91 -0
  79. package/dist/utils/pointer/firePointerEvents.d.ts +14 -0
  80. package/dist/utils/pointer/firePointerEvents.js +55 -0
  81. package/dist/utils/pointer/mouseButtons.d.ts +12 -0
  82. package/dist/utils/pointer/mouseButtons.js +45 -0
  83. package/package.json +4 -3
  84. package/dist/blur.js +0 -15
  85. package/dist/dom-helpers.d.ts +0 -3
  86. package/dist/focus.js +0 -15
  87. package/dist/keyboard/shared/carryValue.d.ts +0 -2
  88. package/dist/keyboard/shared/carryValue.js +0 -13
  89. package/dist/keyboard/shared/fireInputEvent.js +0 -110
  90. package/dist/select-options.d.ts +0 -4
  91. package/dist/type/index.d.ts +0 -7
  92. package/dist/type/index.js +0 -33
  93. package/dist/type/typeImplementation.d.ts +0 -10
  94. package/dist/type/typeImplementation.js +0 -64
  95. package/dist/utils/click/getMouseEventOptions.d.ts +0 -31
  96. package/dist/utils/click/getMouseEventOptions.js +0 -61
  97. package/dist/utils/edit/hasUnreliableEmptyValue.d.ts +0 -10
  98. package/dist/utils/edit/hasUnreliableEmptyValue.js +0 -21
  99. package/dist/utils/misc/isLabelWithInternallyDisabledControl.d.ts +0 -1
  100. package/dist/utils/misc/isLabelWithInternallyDisabledControl.js +0 -22
@@ -3,21 +3,24 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.selectOptions = exports.deselectOptions = void 0;
6
+ exports.deselectOptions = deselectOptions;
7
+ exports.selectOptions = selectOptions;
7
8
 
8
9
  var _dom = require("@testing-library/dom");
9
10
 
10
11
  var _utils = require("./utils");
11
12
 
12
- var _click = require("./click");
13
-
14
- var _focus = require("./focus");
13
+ function selectOptions(select, values, init, options = {}) {
14
+ return selectOptionsBase.call(this, true, select, values, init, options);
15
+ }
15
16
 
16
- var _hover = require("./hover");
17
+ function deselectOptions(select, values, init, options = {}) {
18
+ return selectOptionsBase.call(this, false, select, values, init, options);
19
+ }
17
20
 
18
21
  function selectOptionsBase(newValue, select, values, init, {
19
- skipPointerEventsCheck = false
20
- } = {}) {
22
+ skipPointerEventsCheck
23
+ }) {
21
24
  if (!newValue && !select.multiple) {
22
25
  throw (0, _dom.getConfig)().getElementError(`Unable to deselect an option in a non-multiple select. Use selectOptions to change the selection instead.`, select);
23
26
  }
@@ -62,7 +65,7 @@ function selectOptionsBase(newValue, select, values, init, {
62
65
  _dom.fireEvent.mouseDown(option, init);
63
66
  }
64
67
 
65
- (0, _focus.focus)(select);
68
+ (0, _utils.focus)(select);
66
69
 
67
70
  if (withPointerEvents) {
68
71
  _dom.fireEvent.pointerUp(option, init);
@@ -80,11 +83,11 @@ function selectOptionsBase(newValue, select, values, init, {
80
83
  const withPointerEvents = skipPointerEventsCheck ? true : (0, _utils.hasPointerEvents)(select); // the click to open the select options
81
84
 
82
85
  if (withPointerEvents) {
83
- (0, _click.click)(select, init, {
84
- skipPointerEventsCheck
86
+ this.click(select, init, {
87
+ skipPointerEventsCheck: true
85
88
  });
86
89
  } else {
87
- (0, _focus.focus)(select);
90
+ (0, _utils.focus)(select);
88
91
  }
89
92
 
90
93
  selectOption(selectedOptions[0]);
@@ -111,13 +114,13 @@ function selectOptionsBase(newValue, select, values, init, {
111
114
  }
112
115
  } else if (select.getAttribute('role') === 'listbox') {
113
116
  selectedOptions.forEach(option => {
114
- (0, _hover.hover)(option, init, {
117
+ this.hover(option, init, {
115
118
  skipPointerEventsCheck
116
119
  });
117
- (0, _click.click)(option, init, {
120
+ this.click(option, init, {
118
121
  skipPointerEventsCheck
119
122
  });
120
- (0, _hover.unhover)(option, init, {
123
+ this.unhover(option, init, {
121
124
  skipPointerEventsCheck
122
125
  });
123
126
  });
@@ -136,9 +139,4 @@ function selectOptionsBase(newValue, select, values, init, {
136
139
 
137
140
  _dom.fireEvent.change(select, init);
138
141
  }
139
- }
140
-
141
- const selectOptions = selectOptionsBase.bind(null, true);
142
- exports.selectOptions = selectOptions;
143
- const deselectOptions = selectOptionsBase.bind(null, false);
144
- exports.deselectOptions = deselectOptions;
142
+ }
@@ -0,0 +1,89 @@
1
+ import { clear } from './clear';
2
+ import { click, clickOptions, dblClick } from './click';
3
+ import { hover, unhover } from './hover';
4
+ import { keyboard, keyboardOptions } from './keyboard';
5
+ import type { keyboardState } from './keyboard/types';
6
+ import { paste } from './paste';
7
+ import { pointer } from './pointer';
8
+ import type { pointerOptions, pointerState } from './pointer/types';
9
+ import { deselectOptions, selectOptions } from './selectOptions';
10
+ import { tab, tabOptions } from './tab';
11
+ import { type, typeOptions } from './type';
12
+ import { upload, uploadOptions } from './upload';
13
+ import { PointerOptions } from './utils';
14
+ export declare const userEventApis: {
15
+ clear: typeof clear;
16
+ click: typeof click;
17
+ dblClick: typeof dblClick;
18
+ deselectOptions: typeof deselectOptions;
19
+ hover: typeof hover;
20
+ keyboard: typeof keyboard;
21
+ paste: typeof paste;
22
+ pointer: typeof pointer;
23
+ selectOptions: typeof selectOptions;
24
+ tab: typeof tab;
25
+ type: typeof type;
26
+ unhover: typeof unhover;
27
+ upload: typeof upload;
28
+ };
29
+ export declare type UserEventApis = typeof userEventApis;
30
+ declare type setup = ReturnType<typeof _setup>['setup'];
31
+ export declare type UserEvent = UserEventApis & {
32
+ setup: setup;
33
+ };
34
+ declare type ClickOptions = Omit<clickOptions, 'clickCount'>;
35
+ declare type KeyboardOptions = Partial<keyboardOptions>;
36
+ declare type PointerApiOptions = Partial<pointerOptions>;
37
+ declare type TabOptions = Omit<tabOptions, 'shift'>;
38
+ declare type TypeOptions = Omit<typeOptions, 'initialSelectionStart' | 'initialSelectionEnd'>;
39
+ declare type UploadOptions = uploadOptions;
40
+ interface SetupOptions extends ClickOptions, KeyboardOptions, PointerOptions, PointerApiOptions, TabOptions, TypeOptions, UploadOptions {
41
+ }
42
+ /**
43
+ * Start a "session" with userEvent.
44
+ * All APIs returned by this function share an input device state and a default configuration.
45
+ */
46
+ export declare function setup(options?: SetupOptions): {
47
+ clear: typeof clear;
48
+ click: typeof click;
49
+ dblClick: typeof dblClick;
50
+ deselectOptions: typeof deselectOptions;
51
+ hover: typeof hover;
52
+ keyboard: typeof keyboard;
53
+ paste: typeof paste;
54
+ pointer: typeof pointer;
55
+ selectOptions: typeof selectOptions;
56
+ tab: typeof tab;
57
+ type: typeof type;
58
+ unhover: typeof unhover;
59
+ upload: typeof upload;
60
+ } & {
61
+ /**
62
+ * Create a set of callbacks with different default settings but the same state.
63
+ */
64
+ setup(options: SetupOptions): {
65
+ clear: typeof clear;
66
+ click: typeof click;
67
+ dblClick: typeof dblClick;
68
+ deselectOptions: typeof deselectOptions;
69
+ hover: typeof hover;
70
+ keyboard: typeof keyboard;
71
+ paste: typeof paste;
72
+ pointer: typeof pointer;
73
+ selectOptions: typeof selectOptions;
74
+ tab: typeof tab;
75
+ type: typeof type;
76
+ unhover: typeof unhover;
77
+ upload: typeof upload;
78
+ } & any;
79
+ };
80
+ declare function _setup({ applyAccept, autoModify, delay, document, focusTrap, keyboardMap, pointerMap, skipAutoClose, skipClick, skipHover, skipPointerEventsCheck, }: SetupOptions, { keyboardState, pointerState, }: {
81
+ keyboardState: keyboardState;
82
+ pointerState: pointerState;
83
+ }): UserEventApis & {
84
+ /**
85
+ * Create a set of callbacks with different default settings but the same state.
86
+ */
87
+ setup(options: SetupOptions): ReturnType<typeof _setup>;
88
+ };
89
+ export {};
package/dist/setup.js ADDED
@@ -0,0 +1,206 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.setup = setup;
7
+ exports.userEventApis = void 0;
8
+
9
+ var _clear = require("./clear");
10
+
11
+ var _click = require("./click");
12
+
13
+ var _document = require("./document");
14
+
15
+ var _hover = require("./hover");
16
+
17
+ var _keyboard = require("./keyboard");
18
+
19
+ var _paste = require("./paste");
20
+
21
+ var _pointer = require("./pointer");
22
+
23
+ var _selectOptions = require("./selectOptions");
24
+
25
+ var _tab = require("./tab");
26
+
27
+ var _type = require("./type");
28
+
29
+ var _upload = require("./upload");
30
+
31
+ const userEventApis = {
32
+ clear: _clear.clear,
33
+ click: _click.click,
34
+ dblClick: _click.dblClick,
35
+ deselectOptions: _selectOptions.deselectOptions,
36
+ hover: _hover.hover,
37
+ keyboard: _keyboard.keyboard,
38
+ paste: _paste.paste,
39
+ pointer: _pointer.pointer,
40
+ selectOptions: _selectOptions.selectOptions,
41
+ tab: _tab.tab,
42
+ type: _type.type,
43
+ unhover: _hover.unhover,
44
+ upload: _upload.upload
45
+ };
46
+ exports.userEventApis = userEventApis;
47
+
48
+ /**
49
+ * Start a "session" with userEvent.
50
+ * All APIs returned by this function share an input device state and a default configuration.
51
+ */
52
+ function setup(options = {}) {
53
+ var _options$document;
54
+
55
+ (0, _document.prepareDocument)((_options$document = options.document) != null ? _options$document : document);
56
+ return _setup(options, {
57
+ keyboardState: (0, _keyboard.createKeyboardState)(),
58
+ pointerState: (0, _pointer.createPointerState)()
59
+ });
60
+ }
61
+
62
+ function _setup({
63
+ applyAccept,
64
+ autoModify,
65
+ delay = 0,
66
+ document,
67
+ focusTrap,
68
+ keyboardMap,
69
+ pointerMap,
70
+ skipAutoClose,
71
+ skipClick,
72
+ skipHover,
73
+ skipPointerEventsCheck = false
74
+ }, {
75
+ keyboardState,
76
+ pointerState
77
+ }) {
78
+ const keyboardDefaults = {
79
+ autoModify,
80
+ delay,
81
+ document,
82
+ keyboardMap
83
+ };
84
+ const pointerDefaults = {
85
+ skipPointerEventsCheck
86
+ };
87
+ const pointerApiDefaults = {
88
+ delay,
89
+ pointerMap
90
+ };
91
+ const clickDefaults = {
92
+ skipHover
93
+ };
94
+ const tabDefaults = {
95
+ focusTrap
96
+ };
97
+ const typeDefaults = {
98
+ delay,
99
+ skipAutoClose,
100
+ skipClick
101
+ };
102
+ const uploadDefaults = {
103
+ applyAccept
104
+ };
105
+ const userEvent = {
106
+ clear: (...args) => {
107
+ return _clear.clear.call(userEvent, ...args);
108
+ },
109
+ click: (...args) => {
110
+ args[2] = { ...pointerDefaults,
111
+ ...clickDefaults,
112
+ ...args[2]
113
+ };
114
+ return _click.click.call(userEvent, ...args);
115
+ },
116
+ dblClick: (...args) => {
117
+ args[2] = { ...pointerDefaults,
118
+ ...clickDefaults,
119
+ ...args[2]
120
+ };
121
+ return _click.dblClick.call(userEvent, ...args);
122
+ },
123
+ deselectOptions: (...args) => {
124
+ args[3] = { ...pointerDefaults,
125
+ ...args[3]
126
+ };
127
+ return _selectOptions.deselectOptions.call(userEvent, ...args);
128
+ },
129
+ hover: (...args) => {
130
+ args[2] = { ...pointerDefaults,
131
+ ...args[2]
132
+ };
133
+ return _hover.hover.call(userEvent, ...args);
134
+ },
135
+ // keyboard needs typecasting because of the overloading
136
+ keyboard: (...args) => {
137
+ args[1] = { ...keyboardDefaults,
138
+ ...args[1],
139
+ keyboardState
140
+ };
141
+ const ret = (0, _keyboard.keyboard)(...args);
142
+
143
+ if (ret instanceof Promise) {
144
+ return ret.then(() => undefined);
145
+ }
146
+ },
147
+ paste: (...args) => {
148
+ return _paste.paste.call(userEvent, ...args);
149
+ },
150
+ // pointer needs typecasting because of the overloading
151
+ pointer: (...args) => {
152
+ args[1] = { ...pointerApiDefaults,
153
+ ...args[1],
154
+ pointerState,
155
+ keyboardState
156
+ };
157
+ const ret = (0, _pointer.pointer)(...args);
158
+
159
+ if (ret instanceof Promise) {
160
+ return ret.then(() => undefined);
161
+ }
162
+ },
163
+ selectOptions: (...args) => {
164
+ args[3] = { ...pointerDefaults,
165
+ ...args[3]
166
+ };
167
+ return _selectOptions.selectOptions.call(userEvent, ...args);
168
+ },
169
+ setup: options => {
170
+ return _setup({ ...keyboardDefaults,
171
+ ...pointerDefaults,
172
+ ...clickDefaults,
173
+ ...options
174
+ }, {
175
+ keyboardState,
176
+ pointerState
177
+ });
178
+ },
179
+ tab: (...args) => {
180
+ args[0] = { ...tabDefaults,
181
+ ...args[0]
182
+ };
183
+ return _tab.tab.call(userEvent, ...args);
184
+ },
185
+ // type needs typecasting because of the overloading
186
+ type: (...args) => {
187
+ args[2] = { ...typeDefaults,
188
+ ...args[2]
189
+ };
190
+ return _type.type.call(userEvent, ...args);
191
+ },
192
+ unhover: (...args) => {
193
+ args[2] = { ...pointerDefaults,
194
+ ...args[2]
195
+ };
196
+ return _hover.unhover.call(userEvent, ...args);
197
+ },
198
+ upload: (...args) => {
199
+ args[3] = { ...uploadDefaults,
200
+ ...args[3]
201
+ };
202
+ return _upload.upload.call(userEvent, ...args);
203
+ }
204
+ };
205
+ return userEvent;
206
+ }
package/dist/tab.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- interface tabOptions {
1
+ import type { UserEvent } from './setup';
2
+ export interface tabOptions {
2
3
  shift?: boolean;
3
4
  focusTrap?: Document | Element;
4
5
  }
5
- declare function tab({ shift, focusTrap }?: tabOptions): void;
6
- export { tab };
6
+ export declare function tab(this: UserEvent, { shift, focusTrap }?: tabOptions): void;
package/dist/tab.js CHANGED
@@ -9,20 +9,6 @@ var _dom = require("@testing-library/dom");
9
9
 
10
10
  var _utils = require("./utils");
11
11
 
12
- var _focus = require("./focus");
13
-
14
- var _blur = require("./blur");
15
-
16
- function getNextElement(currentIndex, shift, elements, focusTrap) {
17
- if ((0, _utils.isDocument)(focusTrap) && (currentIndex === 0 && shift || currentIndex === elements.length - 1 && !shift)) {
18
- return focusTrap.body;
19
- }
20
-
21
- const nextIndex = shift ? currentIndex - 1 : currentIndex + 1;
22
- const defaultIndex = shift ? elements.length - 1 : 0;
23
- return elements[nextIndex] || elements[defaultIndex];
24
- }
25
-
26
12
  function tab({
27
13
  shift = false,
28
14
  focusTrap
@@ -30,71 +16,20 @@ function tab({
30
16
  var _focusTrap$ownerDocum, _focusTrap;
31
17
 
32
18
  const doc = (_focusTrap$ownerDocum = (_focusTrap = focusTrap) == null ? void 0 : _focusTrap.ownerDocument) != null ? _focusTrap$ownerDocum : document;
33
- const previousElement = (0, _utils.getActiveElement)(doc);
19
+ const previousElement = (0, _utils.getActiveElement)(doc); // Never the case in our test environment
20
+ // Only happens if the activeElement is inside a shadowRoot that is not part of `doc`.
21
+
22
+ /* istanbul ignore next */
23
+
24
+ if (!previousElement) {
25
+ return;
26
+ }
34
27
 
35
28
  if (!focusTrap) {
36
29
  focusTrap = doc;
37
30
  }
38
31
 
39
- const focusableElements = focusTrap.querySelectorAll(_utils.FOCUSABLE_SELECTOR);
40
- const enabledElements = Array.from(focusableElements).filter(el => el === previousElement || el.getAttribute('tabindex') !== '-1' && !(0, _utils.isDisabled)(el) && // Hidden elements are not tabable
41
- (0, _utils.isVisible)(el));
42
- if (enabledElements.length === 0) return;
43
- const orderedElements = enabledElements.map((el, idx) => ({
44
- el,
45
- idx
46
- })).sort((a, b) => {
47
- // tabindex has no effect if the active element has tabindex="-1"
48
- if (previousElement && previousElement.getAttribute('tabindex') === '-1') {
49
- return a.idx - b.idx;
50
- }
51
-
52
- const tabIndexA = Number(a.el.getAttribute('tabindex'));
53
- const tabIndexB = Number(b.el.getAttribute('tabindex'));
54
- const diff = tabIndexA - tabIndexB;
55
- return diff === 0 ? a.idx - b.idx : diff;
56
- }).map(({
57
- el
58
- }) => el); // TODO: verify/remove type casts
59
-
60
- const checkedRadio = {};
61
- let prunedElements = [];
62
- orderedElements.forEach(currentElement => {
63
- // For radio groups keep only the active radio
64
- // If there is no active radio, keep only the checked radio
65
- // If there is no checked radio, treat like everything else
66
- const el = currentElement;
67
-
68
- if (el.type === 'radio' && el.name) {
69
- // If the active element is part of the group, add only that
70
- const prev = previousElement;
71
-
72
- if (prev && prev.type === el.type && prev.name === el.name) {
73
- if (el === prev) {
74
- prunedElements.push(el);
75
- }
76
-
77
- return;
78
- } // If we stumble upon a checked radio, remove the others
79
-
80
-
81
- if (el.checked) {
82
- prunedElements = prunedElements.filter(e => e.type !== el.type || e.name !== el.name);
83
- prunedElements.push(el);
84
- checkedRadio[el.name] = el;
85
- return;
86
- } // If we already found the checked one, skip
87
-
88
-
89
- if (typeof checkedRadio[el.name] !== 'undefined') {
90
- return;
91
- }
92
- }
93
-
94
- prunedElements.push(el);
95
- });
96
- const index = prunedElements.findIndex(el => el === previousElement);
97
- const nextElement = getNextElement(index, shift, prunedElements, focusTrap);
32
+ const nextElement = (0, _utils.getTabDestination)(previousElement, shift, focusTrap);
98
33
  const shiftKeyInit = {
99
34
  key: 'Shift',
100
35
  keyCode: 16,
@@ -105,27 +40,18 @@ function tab({
105
40
  keyCode: 9,
106
41
  shiftKey: shift
107
42
  };
108
- let continueToTab = true; // not sure how to make it so there's no previous element...
109
- // istanbul ignore else
110
-
111
- if (previousElement) {
112
- // preventDefault on the shift key makes no difference
113
- if (shift) _dom.fireEvent.keyDown(previousElement, { ...shiftKeyInit
114
- });
115
- continueToTab = _dom.fireEvent.keyDown(previousElement, { ...tabKeyInit
116
- });
117
- }
118
-
119
- const keyUpTarget = !continueToTab && previousElement ? previousElement : nextElement;
43
+ let continueToTab = true;
44
+ if (shift) _dom.fireEvent.keyDown(previousElement, { ...shiftKeyInit
45
+ });
46
+ continueToTab = _dom.fireEvent.keyDown(previousElement, { ...tabKeyInit
47
+ });
48
+ const keyUpTarget = continueToTab ? nextElement : previousElement;
120
49
 
121
50
  if (continueToTab) {
122
51
  if (nextElement === doc.body) {
123
- /* istanbul ignore else */
124
- if (previousElement) {
125
- (0, _blur.blur)(previousElement);
126
- }
52
+ (0, _utils.blur)(previousElement);
127
53
  } else {
128
- (0, _focus.focus)(nextElement);
54
+ (0, _utils.focus)(nextElement);
129
55
  }
130
56
  }
131
57
 
@@ -137,9 +63,4 @@ function tab({
137
63
  shiftKey: false
138
64
  });
139
65
  }
140
- }
141
- /*
142
- eslint
143
- complexity: "off",
144
- max-statements: "off",
145
- */
66
+ }
package/dist/type.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ import type { UserEvent } from './setup';
2
+ export interface typeOptions {
3
+ delay?: number;
4
+ skipClick?: boolean;
5
+ skipAutoClose?: boolean;
6
+ initialSelectionStart?: number;
7
+ initialSelectionEnd?: number;
8
+ }
9
+ export declare function type(this: UserEvent, element: Element, text: string, options?: typeOptions & {
10
+ delay?: 0;
11
+ }): void;
12
+ export declare function type(this: UserEvent, element: Element, text: string, options: typeOptions & {
13
+ delay: number;
14
+ }): Promise<void>;
package/dist/type.js ADDED
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.type = type;
7
+
8
+ var _dom = require("@testing-library/dom");
9
+
10
+ var _document = require("./document");
11
+
12
+ var _utils = require("./utils");
13
+
14
+ var _keyboard = require("./keyboard");
15
+
16
+ // this needs to be wrapped in the event/asyncWrapper for React's act and angular's change detection
17
+ // depending on whether it will be async.
18
+ function type(element, text, {
19
+ delay = 0,
20
+ ...options
21
+ } = {}) {
22
+ (0, _document.prepareDocument)(element.ownerDocument); // we do not want to wrap in the asyncWrapper if we're not
23
+ // going to actually be doing anything async, so we only wrap
24
+ // if the delay is greater than 0
25
+
26
+ if (delay > 0) {
27
+ return (0, _dom.getConfig)().asyncWrapper(() => typeImplementation(this, element, text, {
28
+ delay,
29
+ ...options
30
+ }));
31
+ } else {
32
+ return void typeImplementation(this, element, text, {
33
+ delay,
34
+ ...options
35
+ }) // prevents users from dealing with UnhandledPromiseRejectionWarning
36
+ .catch(console.error);
37
+ }
38
+ }
39
+
40
+ async function typeImplementation(userEvent, element, text, {
41
+ delay,
42
+ skipClick = false,
43
+ skipAutoClose = false,
44
+ initialSelectionStart = undefined,
45
+ initialSelectionEnd = undefined
46
+ }) {
47
+ // TODO: properly type guard
48
+ // we use this workaround for now to prevent changing behavior
49
+ if (element.disabled) return;
50
+ if (!skipClick) userEvent.click(element);
51
+
52
+ if (initialSelectionStart !== undefined) {
53
+ (0, _utils.setSelectionRange)(element, initialSelectionStart, initialSelectionEnd != null ? initialSelectionEnd : initialSelectionStart);
54
+ }
55
+
56
+ const {
57
+ promise,
58
+ releaseAllKeys
59
+ } = (0, _keyboard.keyboardImplementationWrapper)(text, {
60
+ delay,
61
+ document: element.ownerDocument
62
+ });
63
+
64
+ if (delay > 0) {
65
+ await promise;
66
+ }
67
+
68
+ if (!skipAutoClose) {
69
+ releaseAllKeys();
70
+ } // eslint-disable-next-line consistent-return -- we need to return the internal Promise so that it is catchable if we don't await
71
+
72
+
73
+ return promise;
74
+ }
package/dist/upload.d.ts CHANGED
@@ -1,9 +1,10 @@
1
+ import type { UserEvent } from './setup';
1
2
  interface uploadInit {
2
3
  clickInit?: MouseEventInit;
3
4
  changeInit?: EventInit;
4
5
  }
5
- interface uploadOptions {
6
+ export interface uploadOptions {
6
7
  applyAccept?: boolean;
7
8
  }
8
- declare function upload(element: HTMLElement, fileOrFiles: File | File[], init?: uploadInit, { applyAccept }?: uploadOptions): void;
9
- export { upload };
9
+ export declare function upload(this: UserEvent, element: HTMLElement, fileOrFiles: File | File[], init?: uploadInit, { applyAccept }?: uploadOptions): void;
10
+ export {};
package/dist/upload.js CHANGED
@@ -7,12 +7,6 @@ exports.upload = upload;
7
7
 
8
8
  var _dom = require("@testing-library/dom");
9
9
 
10
- var _click = require("./click");
11
-
12
- var _blur = require("./blur");
13
-
14
- var _focus = require("./focus");
15
-
16
10
  var _utils = require("./utils");
17
11
 
18
12
  function upload(element, fileOrFiles, init, {
@@ -29,12 +23,12 @@ function upload(element, fileOrFiles, init, {
29
23
  }
30
24
 
31
25
  if ((0, _utils.isDisabled)(element)) return;
32
- (0, _click.click)(element, init == null ? void 0 : init.clickInit);
26
+ this.click(element, init == null ? void 0 : init.clickInit);
33
27
  const files = (Array.isArray(fileOrFiles) ? fileOrFiles : [fileOrFiles]).filter(file => !applyAccept || isAcceptableFile(file, input.accept)).slice(0, input.multiple ? undefined : 1); // blur fires when the file selector pops up
34
28
 
35
- (0, _blur.blur)(element); // focus fires when they make their selection
29
+ (0, _utils.blur)(element); // focus fires when they make their selection
36
30
 
37
- (0, _focus.focus)(element); // do not fire an input event if the file selection does not change
31
+ (0, _utils.focus)(element); // do not fire an input event if the file selection does not change
38
32
 
39
33
  if (files.length === ((_input$files = input.files) == null ? void 0 : _input$files.length) && files.every((f, i) => {
40
34
  var _input$files2;
@@ -6,12 +6,3 @@ export declare function fireInputEvent(element: HTMLElement, { newValue, newSele
6
6
  [k: string]: unknown;
7
7
  };
8
8
  }): void;
9
- declare const initial: unique symbol;
10
- declare const onBlur: unique symbol;
11
- declare global {
12
- interface Element {
13
- [initial]?: string;
14
- [onBlur]?: EventListener;
15
- }
16
- }
17
- export {};