@testing-library/user-event 12.2.1 → 12.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +54 -16
- package/dist/blur.js +15 -0
- package/dist/clear.js +35 -0
- package/dist/click.js +140 -0
- package/dist/focus.js +15 -0
- package/dist/hover.js +73 -0
- package/dist/index.js +38 -0
- package/dist/keys/navigation-key.js +54 -0
- package/dist/paste.js +55 -0
- package/dist/select-options.js +97 -0
- package/dist/tab.js +137 -0
- package/dist/type.js +837 -0
- package/dist/upload.js +57 -0
- package/dist/utils.js +307 -0
- package/package.json +1 -1
package/dist/upload.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.upload = upload;
|
|
7
|
+
|
|
8
|
+
var _dom = require("@testing-library/dom");
|
|
9
|
+
|
|
10
|
+
var _click = require("./click");
|
|
11
|
+
|
|
12
|
+
var _blur = require("./blur");
|
|
13
|
+
|
|
14
|
+
var _focus = require("./focus");
|
|
15
|
+
|
|
16
|
+
function upload(element, fileOrFiles, init) {
|
|
17
|
+
if (element.disabled) return;
|
|
18
|
+
let files;
|
|
19
|
+
let input = element;
|
|
20
|
+
(0, _click.click)(element, init);
|
|
21
|
+
|
|
22
|
+
if (element.tagName === 'LABEL') {
|
|
23
|
+
files = element.control.multiple ? fileOrFiles : [fileOrFiles];
|
|
24
|
+
input = element.control;
|
|
25
|
+
} else {
|
|
26
|
+
files = element.multiple ? fileOrFiles : [fileOrFiles];
|
|
27
|
+
} // blur fires when the file selector pops up
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
(0, _blur.blur)(element, init); // focus fires when they make their selection
|
|
31
|
+
|
|
32
|
+
(0, _focus.focus)(element, init); // the event fired in the browser isn't actually an "input" or "change" event
|
|
33
|
+
// but a new Event with a type set to "input" and "change"
|
|
34
|
+
// Kinda odd...
|
|
35
|
+
|
|
36
|
+
const inputFiles = {
|
|
37
|
+
length: files.length,
|
|
38
|
+
item: index => files[index],
|
|
39
|
+
...files
|
|
40
|
+
};
|
|
41
|
+
(0, _dom.fireEvent)(input, (0, _dom.createEvent)('input', input, {
|
|
42
|
+
target: {
|
|
43
|
+
files: inputFiles
|
|
44
|
+
},
|
|
45
|
+
bubbles: true,
|
|
46
|
+
cancelable: false,
|
|
47
|
+
composed: true,
|
|
48
|
+
...init
|
|
49
|
+
}));
|
|
50
|
+
|
|
51
|
+
_dom.fireEvent.change(input, {
|
|
52
|
+
target: {
|
|
53
|
+
files: inputFiles
|
|
54
|
+
},
|
|
55
|
+
...init
|
|
56
|
+
});
|
|
57
|
+
}
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.isFocusable = isFocusable;
|
|
7
|
+
exports.isClickable = isClickable;
|
|
8
|
+
exports.getMouseEventOptions = getMouseEventOptions;
|
|
9
|
+
exports.isLabelWithInternallyDisabledControl = isLabelWithInternallyDisabledControl;
|
|
10
|
+
exports.getActiveElement = getActiveElement;
|
|
11
|
+
exports.calculateNewValue = calculateNewValue;
|
|
12
|
+
exports.setSelectionRangeIfNecessary = setSelectionRangeIfNecessary;
|
|
13
|
+
exports.eventWrapper = eventWrapper;
|
|
14
|
+
exports.isValidDateValue = isValidDateValue;
|
|
15
|
+
exports.isValidInputTimeValue = isValidInputTimeValue;
|
|
16
|
+
exports.buildTimeValue = buildTimeValue;
|
|
17
|
+
exports.getValue = getValue;
|
|
18
|
+
exports.getSelectionRange = getSelectionRange;
|
|
19
|
+
exports.isContentEditable = isContentEditable;
|
|
20
|
+
exports.FOCUSABLE_SELECTOR = void 0;
|
|
21
|
+
|
|
22
|
+
var _dom = require("@testing-library/dom");
|
|
23
|
+
|
|
24
|
+
function isMousePressEvent(event) {
|
|
25
|
+
return event === 'mousedown' || event === 'mouseup' || event === 'click' || event === 'dblclick';
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function invert(map) {
|
|
29
|
+
const res = {};
|
|
30
|
+
|
|
31
|
+
for (const key of Object.keys(map)) {
|
|
32
|
+
res[map[key]] = key;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return res;
|
|
36
|
+
} // https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/buttons
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
const BUTTONS_TO_NAMES = {
|
|
40
|
+
0: 'none',
|
|
41
|
+
1: 'primary',
|
|
42
|
+
2: 'secondary',
|
|
43
|
+
4: 'auxiliary'
|
|
44
|
+
};
|
|
45
|
+
const NAMES_TO_BUTTONS = invert(BUTTONS_TO_NAMES); // https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button
|
|
46
|
+
|
|
47
|
+
const BUTTON_TO_NAMES = {
|
|
48
|
+
0: 'primary',
|
|
49
|
+
1: 'auxiliary',
|
|
50
|
+
2: 'secondary'
|
|
51
|
+
};
|
|
52
|
+
const NAMES_TO_BUTTON = invert(BUTTON_TO_NAMES);
|
|
53
|
+
|
|
54
|
+
function convertMouseButtons(event, init, property, mapping) {
|
|
55
|
+
if (!isMousePressEvent(event)) {
|
|
56
|
+
return 0;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (init[property] != null) {
|
|
60
|
+
return init[property];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (init.buttons != null) {
|
|
64
|
+
// not sure how to test this. Feel free to try and add a test if you want.
|
|
65
|
+
// istanbul ignore next
|
|
66
|
+
return mapping[BUTTONS_TO_NAMES[init.buttons]] || 0;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (init.button != null) {
|
|
70
|
+
// not sure how to test this. Feel free to try and add a test if you want.
|
|
71
|
+
// istanbul ignore next
|
|
72
|
+
return mapping[BUTTON_TO_NAMES[init.button]] || 0;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return property != 'button' && isMousePressEvent(event) ? 1 : 0;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function getMouseEventOptions(event, init, clickCount = 0) {
|
|
79
|
+
init = init || {};
|
|
80
|
+
return { ...init,
|
|
81
|
+
// https://developer.mozilla.org/en-US/docs/Web/API/UIEvent/detail
|
|
82
|
+
detail: event === 'mousedown' || event === 'mouseup' || event === 'click' ? 1 + clickCount : clickCount,
|
|
83
|
+
buttons: convertMouseButtons(event, init, 'buttons', NAMES_TO_BUTTONS),
|
|
84
|
+
button: convertMouseButtons(event, init, 'button', NAMES_TO_BUTTON)
|
|
85
|
+
};
|
|
86
|
+
} // Absolutely NO events fire on label elements that contain their control
|
|
87
|
+
// if that control is disabled. NUTS!
|
|
88
|
+
// no joke. There are NO events for: <label><input disabled /><label>
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
function isLabelWithInternallyDisabledControl(element) {
|
|
92
|
+
var _element$control;
|
|
93
|
+
|
|
94
|
+
return element.tagName === 'LABEL' && ((_element$control = element.control) == null ? void 0 : _element$control.disabled) && element.contains(element.control);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function getActiveElement(document) {
|
|
98
|
+
const activeElement = document.activeElement;
|
|
99
|
+
|
|
100
|
+
if (activeElement != null && activeElement.shadowRoot) {
|
|
101
|
+
return getActiveElement(activeElement.shadowRoot);
|
|
102
|
+
} else {
|
|
103
|
+
return activeElement;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function supportsMaxLength(element) {
|
|
108
|
+
if (element.tagName === 'TEXTAREA') return true;
|
|
109
|
+
|
|
110
|
+
if (element.tagName === 'INPUT') {
|
|
111
|
+
const type = element.getAttribute('type'); // Missing value default is "text"
|
|
112
|
+
|
|
113
|
+
if (!type) return true; // https://html.spec.whatwg.org/multipage/input.html#concept-input-apply
|
|
114
|
+
|
|
115
|
+
if (type.match(/email|password|search|telephone|text|url/)) return true;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function getSelectionRange(element) {
|
|
122
|
+
if (isContentEditable(element)) {
|
|
123
|
+
const range = element.ownerDocument.getSelection().getRangeAt(0);
|
|
124
|
+
return {
|
|
125
|
+
selectionStart: range.startOffset,
|
|
126
|
+
selectionEnd: range.endOffset
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return {
|
|
131
|
+
selectionStart: element.selectionStart,
|
|
132
|
+
selectionEnd: element.selectionEnd
|
|
133
|
+
};
|
|
134
|
+
} //jsdom is not supporting isContentEditable
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
function isContentEditable(element) {
|
|
138
|
+
return element.hasAttribute('contenteditable') && (element.getAttribute('contenteditable') == 'true' || element.getAttribute('contenteditable') == '');
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function getValue(element) {
|
|
142
|
+
if (isContentEditable(element)) {
|
|
143
|
+
return element.textContent;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return element.value;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function calculateNewValue(newEntry, element) {
|
|
150
|
+
var _element$getAttribute;
|
|
151
|
+
|
|
152
|
+
const {
|
|
153
|
+
selectionStart,
|
|
154
|
+
selectionEnd
|
|
155
|
+
} = getSelectionRange(element);
|
|
156
|
+
const value = getValue(element); // can't use .maxLength property because of a jsdom bug:
|
|
157
|
+
// https://github.com/jsdom/jsdom/issues/2927
|
|
158
|
+
|
|
159
|
+
const maxLength = Number((_element$getAttribute = element.getAttribute('maxlength')) != null ? _element$getAttribute : -1);
|
|
160
|
+
let newValue, newSelectionStart;
|
|
161
|
+
|
|
162
|
+
if (selectionStart === null) {
|
|
163
|
+
// at the end of an input type that does not support selection ranges
|
|
164
|
+
// https://github.com/testing-library/user-event/issues/316#issuecomment-639744793
|
|
165
|
+
newValue = value + newEntry;
|
|
166
|
+
} else if (selectionStart === selectionEnd) {
|
|
167
|
+
if (selectionStart === 0) {
|
|
168
|
+
// at the beginning of the input
|
|
169
|
+
newValue = newEntry + value;
|
|
170
|
+
} else if (selectionStart === value.length) {
|
|
171
|
+
// at the end of the input
|
|
172
|
+
newValue = value + newEntry;
|
|
173
|
+
} else {
|
|
174
|
+
// in the middle of the input
|
|
175
|
+
newValue = value.slice(0, selectionStart) + newEntry + value.slice(selectionEnd);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
newSelectionStart = selectionStart + newEntry.length;
|
|
179
|
+
} else {
|
|
180
|
+
// we have something selected
|
|
181
|
+
const firstPart = value.slice(0, selectionStart) + newEntry;
|
|
182
|
+
newValue = firstPart + value.slice(selectionEnd);
|
|
183
|
+
newSelectionStart = firstPart.length;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (element.type === 'date' && !isValidDateValue(element, newValue)) {
|
|
187
|
+
newValue = value;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (element.type === 'time' && !isValidInputTimeValue(element, newValue)) {
|
|
191
|
+
if (isValidInputTimeValue(element, newEntry)) {
|
|
192
|
+
newValue = newEntry;
|
|
193
|
+
} else {
|
|
194
|
+
newValue = value;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (!supportsMaxLength(element) || maxLength < 0) {
|
|
199
|
+
return {
|
|
200
|
+
newValue,
|
|
201
|
+
newSelectionStart
|
|
202
|
+
};
|
|
203
|
+
} else {
|
|
204
|
+
return {
|
|
205
|
+
newValue: newValue.slice(0, maxLength),
|
|
206
|
+
newSelectionStart: newSelectionStart > maxLength ? maxLength : newSelectionStart
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function setSelectionRangeIfNecessary(element, newSelectionStart, newSelectionEnd) {
|
|
212
|
+
const {
|
|
213
|
+
selectionStart,
|
|
214
|
+
selectionEnd
|
|
215
|
+
} = getSelectionRange(element);
|
|
216
|
+
|
|
217
|
+
if (!isContentEditable(element) && (!element.setSelectionRange || selectionStart === null)) {
|
|
218
|
+
// cannot set selection
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
if (selectionStart !== newSelectionStart || selectionEnd !== newSelectionStart) {
|
|
223
|
+
if (isContentEditable(element)) {
|
|
224
|
+
const range = element.ownerDocument.createRange();
|
|
225
|
+
range.selectNodeContents(element);
|
|
226
|
+
range.setStart(element.firstChild, newSelectionStart);
|
|
227
|
+
range.setEnd(element.firstChild, newSelectionEnd);
|
|
228
|
+
element.ownerDocument.getSelection().removeAllRanges();
|
|
229
|
+
element.ownerDocument.getSelection().addRange(range);
|
|
230
|
+
} else {
|
|
231
|
+
element.setSelectionRange(newSelectionStart, newSelectionEnd);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const FOCUSABLE_SELECTOR = ['input:not([type=hidden]):not([disabled])', 'button:not([disabled])', 'select:not([disabled])', 'textarea:not([disabled])', '[contenteditable=""]', '[contenteditable="true"]', 'a[href]', '[tabindex]:not([disabled])'].join(', ');
|
|
237
|
+
exports.FOCUSABLE_SELECTOR = FOCUSABLE_SELECTOR;
|
|
238
|
+
|
|
239
|
+
function isFocusable(element) {
|
|
240
|
+
return !isLabelWithInternallyDisabledControl(element) && (element == null ? void 0 : element.matches(FOCUSABLE_SELECTOR));
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const CLICKABLE_INPUT_TYPES = ['button', 'color', 'file', 'image', 'reset', 'submit'];
|
|
244
|
+
|
|
245
|
+
function isClickable(element) {
|
|
246
|
+
return element.tagName === 'BUTTON' || element instanceof element.ownerDocument.defaultView.HTMLInputElement && CLICKABLE_INPUT_TYPES.includes(element.type);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function eventWrapper(cb) {
|
|
250
|
+
let result;
|
|
251
|
+
(0, _dom.getConfig)().eventWrapper(() => {
|
|
252
|
+
result = cb();
|
|
253
|
+
});
|
|
254
|
+
return result;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function isValidDateValue(element, value) {
|
|
258
|
+
if (element.type !== 'date') return false;
|
|
259
|
+
const clone = element.cloneNode();
|
|
260
|
+
clone.value = value;
|
|
261
|
+
return clone.value === value;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function buildTimeValue(value) {
|
|
265
|
+
function build(onlyDigitsValue, index) {
|
|
266
|
+
const hours = onlyDigitsValue.slice(0, index);
|
|
267
|
+
const validHours = Math.min(parseInt(hours, 10), 23);
|
|
268
|
+
const minuteCharacters = onlyDigitsValue.slice(index);
|
|
269
|
+
const parsedMinutes = parseInt(minuteCharacters, 10);
|
|
270
|
+
const validMinutes = Math.min(parsedMinutes, 59);
|
|
271
|
+
return `${validHours.toString().padStart(2, '0')}:${validMinutes.toString().padStart(2, '0')}`;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
const onlyDigitsValue = value.replace(/\D/g, '');
|
|
275
|
+
|
|
276
|
+
if (onlyDigitsValue.length < 2) {
|
|
277
|
+
return value;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const firstDigit = parseInt(onlyDigitsValue[0], 10);
|
|
281
|
+
const secondDigit = parseInt(onlyDigitsValue[1], 10);
|
|
282
|
+
|
|
283
|
+
if (firstDigit >= 3 || firstDigit === 2 && secondDigit >= 4) {
|
|
284
|
+
let index;
|
|
285
|
+
|
|
286
|
+
if (firstDigit >= 3) {
|
|
287
|
+
index = 1;
|
|
288
|
+
} else {
|
|
289
|
+
index = 2;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
return build(onlyDigitsValue, index);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
if (value.length === 2) {
|
|
296
|
+
return value;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
return build(onlyDigitsValue, 2);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function isValidInputTimeValue(element, timeValue) {
|
|
303
|
+
if (element.type !== 'time') return false;
|
|
304
|
+
const clone = element.cloneNode();
|
|
305
|
+
clone.value = timeValue;
|
|
306
|
+
return clone.value === timeValue;
|
|
307
|
+
}
|