/
type.js
443 lines (393 loc) · 12.8 KB
/
type.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
import {
getConfig as getDOMTestingLibraryConfig,
fireEvent,
} from '@testing-library/dom'
import {tick} from './tick'
function wait(time) {
return new Promise(resolve => setTimeout(() => resolve(), time))
}
// this needs to be wrapped in the asyncWrapper for React's act and angular's change detection
async function type(...args) {
let result
await getDOMTestingLibraryConfig().asyncWrapper(async () => {
result = await typeImpl(...args)
})
return result
}
const getActiveElement = document => {
const activeElement = document.activeElement
if (activeElement.shadowRoot) {
return getActiveElement(activeElement.shadowRoot) || activeElement
} else {
return activeElement
}
}
// eslint-disable-next-line complexity
async function typeImpl(
element,
text,
{allAtOnce = false, delay, initialSelectionStart, initialSelectionEnd} = {},
) {
if (element.disabled) return
element.focus()
// The focused element could change between each event, so get the currently active element each time
const currentElement = () => getActiveElement(element.ownerDocument)
const currentValue = () => currentElement().value
const setSelectionRange = ({newValue, newSelectionStart}) => {
// if we *can* change the selection start, then we will if the new value
// is the same as the current value (so it wasn't programatically changed
// when the fireEvent.input was triggered).
// The reason we have to do this at all is because it actually *is*
// programmatically changed by fireEvent.input, so we have to simulate the
// browser's default behavior
if (
currentElement().selectionStart !== null &&
currentValue() === newValue
) {
currentElement().setSelectionRange?.(newSelectionStart, newSelectionStart)
}
}
// by default, a new element has it's selection start and end at 0
// but most of the time when people call "type", they expect it to type
// at the end of the current input value. So, if the selection start
// and end are both the default of 0, then we'll go ahead and change
// them to the length of the current value.
// the only time it would make sense to pass the initialSelectionStart or
// initialSelectionEnd is if you have an input with a value and want to
// explicitely start typing with the cursor at 0. Not super common.
if (
currentElement().selectionStart === 0 &&
currentElement().selectionEnd === 0
) {
currentElement().setSelectionRange(
initialSelectionStart ?? currentValue()?.length ?? 0,
initialSelectionEnd ?? currentValue()?.length ?? 0,
)
}
if (allAtOnce) {
if (!element.readOnly) {
const {newValue, newSelectionStart} = calculateNewValue(text)
fireEvent.input(element, {
target: {value: newValue},
})
setSelectionRange({newValue, newSelectionStart})
}
} else {
const eventCallbackMap = {
...modifier({
name: 'shift',
key: 'Shift',
keyCode: 16,
modifierProperty: 'shiftKey',
}),
...modifier({
name: 'ctrl',
key: 'Control',
keyCode: 17,
modifierProperty: 'ctrlKey',
}),
...modifier({
name: 'alt',
key: 'Alt',
keyCode: 18,
modifierProperty: 'altKey',
}),
...modifier({
name: 'meta',
key: 'Meta',
keyCode: 93,
modifierProperty: 'metaKey',
}),
'{enter}': async ({eventOverrides}) => {
const key = 'Enter'
const keyCode = 13
const keyDownDefaultNotPrevented = fireEvent.keyDown(currentElement(), {
key,
keyCode,
which: keyCode,
...eventOverrides,
})
if (keyDownDefaultNotPrevented) {
await tick()
fireEvent.keyPress(currentElement(), {
key,
keyCode,
charCode: keyCode,
...eventOverrides,
})
}
if (currentElement().tagName === 'BUTTON') {
await tick()
fireEvent.click(currentElement(), {
...eventOverrides,
})
}
if (currentElement().tagName === 'TEXTAREA') {
await tick()
const {newValue, newSelectionStart} = calculateNewValue('\n')
fireEvent.input(currentElement(), {
target: {value: newValue},
inputType: 'insertLineBreak',
...eventOverrides,
})
setSelectionRange({newValue, newSelectionStart})
}
await tick()
fireEvent.keyUp(currentElement(), {
key,
keyCode,
which: keyCode,
...eventOverrides,
})
},
'{esc}': async ({eventOverrides}) => {
const key = 'Escape'
const keyCode = 27
fireEvent.keyDown(currentElement(), {
key,
keyCode,
which: keyCode,
...eventOverrides,
})
await tick()
// NOTE: Browsers do not fire a keypress on meta key presses
fireEvent.keyUp(currentElement(), {
key,
keyCode,
which: keyCode,
...eventOverrides,
})
},
'{backspace}': async ({eventOverrides}) => {
const key = 'Backspace'
const keyCode = 8
const keyPressDefaultNotPrevented = fireEvent.keyDown(
currentElement(),
{
key,
keyCode,
which: keyCode,
...eventOverrides,
},
)
if (keyPressDefaultNotPrevented) {
await fireInputEventIfNeeded({
...calculateNewBackspaceValue(),
eventOverrides: {
inputType: 'deleteContentBackward',
...eventOverrides,
},
})
}
await tick()
fireEvent.keyUp(currentElement(), {
key,
keyCode,
which: keyCode,
...eventOverrides,
})
},
}
const eventCallbacks = []
let remainingString = text
while (remainingString) {
const eventKey = Object.keys(eventCallbackMap).find(key =>
remainingString.startsWith(key),
)
if (eventKey) {
eventCallbacks.push(eventCallbackMap[eventKey])
remainingString = remainingString.slice(eventKey.length)
} else {
const character = remainingString[0]
eventCallbacks.push((...args) => typeCharacter(character, ...args))
remainingString = remainingString.slice(1)
}
}
const eventOverrides = {}
let prevWasMinus
for (const callback of eventCallbacks) {
if (delay > 0) await wait(delay)
if (!currentElement().disabled) {
const returnValue = await callback({prevWasMinus, eventOverrides})
Object.assign(eventOverrides, returnValue?.eventOverrides)
prevWasMinus = returnValue?.prevWasMinus
}
}
}
async function fireInputEventIfNeeded({
newValue,
newSelectionStart,
eventOverrides,
}) {
const prevValue = currentValue()
if (!currentElement().readOnly && newValue !== prevValue) {
await tick()
fireEvent.input(currentElement(), {
target: {value: newValue},
...eventOverrides,
})
setSelectionRange({newValue, newSelectionStart})
}
return {prevValue}
}
// yes, calculateNewBackspaceValue and calculateNewValue look extremely similar
// and you may be tempted to create a shared abstraction.
// If you, brave soul, decide to so endevor, please increment this count
// when you inevitably fail: 1
function calculateNewBackspaceValue() {
const {selectionStart, selectionEnd} = currentElement()
const value = currentValue()
let newValue, newSelectionStart
if (selectionStart === null) {
// at the end of an input type that does not support selection ranges
// https://github.com/testing-library/user-event/issues/316#issuecomment-639744793
newValue = value.slice(0, value.length - 1)
newSelectionStart = selectionStart - 1
} else if (selectionStart === selectionEnd) {
if (selectionStart === 0) {
// at the beginning of the input
newValue = value
} else if (selectionStart === value.length) {
// at the end of the input
newValue = value.slice(0, value.length - 1)
newSelectionStart = selectionStart - 1
} else {
// in the middle of the input
newValue =
value.slice(0, selectionStart - 1) + value.slice(selectionEnd)
newSelectionStart = selectionStart - 1
}
} else {
// we have something selected
const firstPart = value.slice(0, selectionStart)
newValue = firstPart + value.slice(selectionEnd)
newSelectionStart = firstPart.length
}
return {newValue, newSelectionStart}
}
function calculateNewValue(newEntry) {
const {selectionStart, selectionEnd} = currentElement()
// can't use .maxLength property because of a jsdom bug:
// https://github.com/jsdom/jsdom/issues/2927
const maxLength = Number(currentElement().getAttribute('maxlength') ?? -1)
const value = currentValue()
let newValue, newSelectionStart
if (selectionStart === null) {
// at the end of an input type that does not support selection ranges
// https://github.com/testing-library/user-event/issues/316#issuecomment-639744793
newValue = value + newEntry
} else if (selectionStart === selectionEnd) {
if (selectionStart === 0) {
// at the beginning of the input
newValue = newEntry + value
} else if (selectionStart === value.length) {
// at the end of the input
newValue = value + newEntry
} else {
// in the middle of the input
newValue =
value.slice(0, selectionStart) + newEntry + value.slice(selectionEnd)
}
newSelectionStart = selectionStart + newEntry.length
} else {
// we have something selected
const firstPart = value.slice(0, selectionStart) + newEntry
newValue = firstPart + value.slice(selectionEnd)
newSelectionStart = firstPart.length
}
if (maxLength < 0) {
return {newValue, newSelectionStart}
} else {
return {
newValue: newValue.slice(0, maxLength),
newSelectionStart:
newSelectionStart > maxLength ? maxLength : newSelectionStart,
}
}
}
async function typeCharacter(char, {prevWasMinus = false, eventOverrides}) {
const key = char // TODO: check if this also valid for characters with diacritic markers e.g. úé etc
const keyCode = char.charCodeAt(0)
let nextPrevWasMinus
const keyDownDefaultNotPrevented = fireEvent.keyDown(currentElement(), {
key,
keyCode,
which: keyCode,
...eventOverrides,
})
if (keyDownDefaultNotPrevented) {
await tick()
const keyPressDefaultNotPrevented = fireEvent.keyPress(currentElement(), {
key,
keyCode,
charCode: keyCode,
...eventOverrides,
})
if (keyPressDefaultNotPrevented) {
const newEntry = prevWasMinus ? `-${char}` : char
const {prevValue} = await fireInputEventIfNeeded({
...calculateNewValue(newEntry),
eventOverrides: {
data: key,
inputType: 'insertText',
...eventOverrides,
},
})
// typing "-" into a number input will not actually update the value
// so for the next character we type, the value should be set to
// `-${newEntry}`
// we also preserve the prevWasMinus when the value is unchanged due
// to typing an invalid character (typing "-a3" results in "-3")
if (currentElement().type === 'number') {
const newValue = currentValue()
if (newValue === prevValue && newEntry !== '-') {
nextPrevWasMinus = prevWasMinus
} else {
nextPrevWasMinus = newEntry === '-'
}
}
}
}
await tick()
fireEvent.keyUp(currentElement(), {
key,
keyCode,
which: keyCode,
...eventOverrides,
})
return {prevWasMinus: nextPrevWasMinus}
}
function modifier({name, key, keyCode, modifierProperty}) {
return {
[`{${name}}`]: ({eventOverrides}) => {
const newEventOverrides = {[modifierProperty]: true}
fireEvent.keyDown(currentElement(), {
key,
keyCode,
which: keyCode,
...eventOverrides,
...newEventOverrides,
})
return {eventOverrides: newEventOverrides}
},
[`{/${name}}`]: ({eventOverrides}) => {
const newEventOverrides = {[modifierProperty]: false}
fireEvent.keyUp(currentElement(), {
key,
keyCode,
which: keyCode,
...eventOverrides,
...newEventOverrides,
})
return {eventOverrides: newEventOverrides}
},
}
}
}
export {type}
/*
eslint
no-await-in-loop: "off",
no-loop-func: "off",
max-lines-per-function: "off",
*/