/
type.js
514 lines (463 loc) Β· 14.9 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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
// TODO: wrap in asyncWrapper
import {
fireEvent,
getConfig as getDOMTestingLibraryConfig,
} from '@testing-library/dom'
import {
getActiveElement,
calculateNewValue,
setSelectionRangeIfNecessary,
} from './utils'
import {click} from './click'
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
// but only if it's actually going to be async.
async function type(element, text, {delay = 0, ...options} = {}) {
// we do not want to wrap in the asyncWrapper if we're not
// going to actually be doing anything async, so we only wrap
// if the delay is greater than 0
if (delay > 0) {
let result
await getDOMTestingLibraryConfig().asyncWrapper(async () => {
result = await typeImpl(element, text, {delay, ...options})
})
return result
} else {
return typeImpl(element, text, {delay, ...options})
}
}
async function typeImpl(
element,
text,
{
delay,
skipClick = false,
skipAutoClose = false,
initialSelectionStart,
initialSelectionEnd,
},
) {
if (element.disabled) return
if (!skipClick) click(element)
// The focused element could change between each event, so get the currently active element each time
// This is why most of the utilities are within the type function itself. If
// they weren't, then we'd have to pass the "currentElement" function to them
// as an argument, which would be fine, but make sure that you pass the function
// and not just the element if the active element could change while the function
// is being run (for example, functions that are and/or fire events).
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
const value = currentValue()
if (value === newValue) {
setSelectionRangeIfNecessary(
currentElement(),
newSelectionStart,
newSelectionStart,
)
} else {
// If the currentValue is different than the expected newValue and we *can*
// change the selection range, than we should set it to the length of the
// currentValue to ensure that the browser behavior is mimicked.
setSelectionRangeIfNecessary(currentElement(), value.length, value.length)
}
}
// 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
) {
setSelectionRangeIfNecessary(
currentElement(),
initialSelectionStart ?? currentValue().length,
initialSelectionEnd ?? currentValue().length,
)
}
const eventCallbackMap = getEventCallbackMap({
currentElement,
fireInputEventIfNeeded,
setSelectionRange,
})
const eventCallbacks = queueCallbacks()
await runCallbacks(eventCallbacks)
function queueCallbacks() {
const callbacks = []
const modifierClosers = []
let remainingString = text
while (remainingString) {
const eventKey = Object.keys(eventCallbackMap).find(key =>
remainingString.startsWith(key),
)
if (eventKey) {
const modifierCallback = eventCallbackMap[eventKey]
callbacks.push(modifierCallback)
// if this modifier has an associated "close" callback and the developer
// doesn't close it themselves, then we close it for them automatically
// Effectively if they send in: '{alt}a' then we type: '{alt}a{/alt}'
if (
!skipAutoClose &&
modifierCallback.close &&
!remainingString.includes(modifierCallback.close.name)
) {
modifierClosers.push(modifierCallback.close.fn)
}
remainingString = remainingString.slice(eventKey.length)
} else {
const character = remainingString[0]
callbacks.push((...args) => typeCharacter(character, ...args))
remainingString = remainingString.slice(1)
}
}
return [...callbacks, ...modifierClosers]
}
async function runCallbacks(callbacks) {
const eventOverrides = {}
let prevWasMinus, prevWasPeriod
for (const callback of callbacks) {
if (delay > 0) await wait(delay)
if (!currentElement().disabled) {
const returnValue = callback({
prevWasMinus,
prevWasPeriod,
eventOverrides,
})
Object.assign(eventOverrides, returnValue?.eventOverrides)
prevWasMinus = returnValue?.prevWasMinus
prevWasPeriod = returnValue?.prevWasPeriod
}
}
}
function fireInputEventIfNeeded({
newValue,
newSelectionStart,
eventOverrides,
}) {
const prevValue = currentValue()
if (!currentElement().readOnly && newValue !== prevValue) {
fireEvent.input(currentElement(), {
target: {value: newValue},
...eventOverrides,
})
setSelectionRange({newValue, newSelectionStart})
}
return {prevValue}
}
function typeCharacter(
char,
{prevWasMinus = false, prevWasPeriod = 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, nextPrevWasPeriod
const keyDownDefaultNotPrevented = fireEvent.keyDown(currentElement(), {
key,
keyCode,
which: keyCode,
...eventOverrides,
})
if (keyDownDefaultNotPrevented) {
const keyPressDefaultNotPrevented = fireEvent.keyPress(currentElement(), {
key,
keyCode,
charCode: keyCode,
...eventOverrides,
})
if (keyPressDefaultNotPrevented) {
let newEntry = char
if (prevWasMinus) {
newEntry = `-${char}`
} else if (prevWasPeriod) {
newEntry = `.${char}`
}
const {prevValue} = fireInputEventIfNeeded({
...calculateNewValue(newEntry, currentElement()),
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")
// same applies for the decimal character.
if (currentElement().type === 'number') {
const newValue = currentValue()
if (newValue === prevValue && newEntry !== '-') {
nextPrevWasMinus = prevWasMinus
} else {
nextPrevWasMinus = newEntry === '-'
}
if (newValue === prevValue && newEntry !== '.') {
nextPrevWasPeriod = prevWasPeriod
} else {
nextPrevWasPeriod = newEntry === '.'
}
}
}
}
fireEvent.keyUp(currentElement(), {
key,
keyCode,
which: keyCode,
...eventOverrides,
})
return {prevWasMinus: nextPrevWasMinus, prevWasPeriod: nextPrevWasPeriod}
}
}
// 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(element) {
const {selectionStart, selectionEnd, value} = element
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
newSelectionStart = selectionStart
} 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 calculateNewDeleteValue(element) {
const {selectionStart, selectionEnd, value} = element
let newValue
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
} else if (selectionStart === selectionEnd) {
if (selectionStart === 0) {
// at the beginning of the input
newValue = value.slice(1)
} else if (selectionStart === value.length) {
// at the end of the input
newValue = value
} else {
// in the middle of the input
newValue = value.slice(0, selectionStart) + value.slice(selectionEnd + 1)
}
} else {
// we have something selected
const firstPart = value.slice(0, selectionStart)
newValue = firstPart + value.slice(selectionEnd)
}
return {newValue, newSelectionStart: selectionStart}
}
function getEventCallbackMap({
currentElement,
fireInputEventIfNeeded,
setSelectionRange,
}) {
return {
...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}': ({eventOverrides}) => {
const key = 'Enter'
const keyCode = 13
const keyDownDefaultNotPrevented = fireEvent.keyDown(currentElement(), {
key,
keyCode,
which: keyCode,
...eventOverrides,
})
if (keyDownDefaultNotPrevented) {
fireEvent.keyPress(currentElement(), {
key,
keyCode,
charCode: keyCode,
...eventOverrides,
})
}
if (currentElement().tagName === 'BUTTON') {
fireEvent.click(currentElement(), {
...eventOverrides,
})
}
if (currentElement().tagName === 'TEXTAREA') {
const {newValue, newSelectionStart} = calculateNewValue(
'\n',
currentElement(),
)
fireEvent.input(currentElement(), {
target: {value: newValue},
inputType: 'insertLineBreak',
...eventOverrides,
})
setSelectionRange({newValue, newSelectionStart})
}
fireEvent.keyUp(currentElement(), {
key,
keyCode,
which: keyCode,
...eventOverrides,
})
},
'{esc}': ({eventOverrides}) => {
const key = 'Escape'
const keyCode = 27
fireEvent.keyDown(currentElement(), {
key,
keyCode,
which: keyCode,
...eventOverrides,
})
// NOTE: Browsers do not fire a keypress on meta key presses
fireEvent.keyUp(currentElement(), {
key,
keyCode,
which: keyCode,
...eventOverrides,
})
},
'{del}': ({eventOverrides}) => {
const key = 'Delete'
const keyCode = 46
const keyPressDefaultNotPrevented = fireEvent.keyDown(currentElement(), {
key,
keyCode,
which: keyCode,
...eventOverrides,
})
if (keyPressDefaultNotPrevented) {
fireInputEventIfNeeded({
...calculateNewDeleteValue(currentElement()),
eventOverrides: {
inputType: 'deleteContentForward',
...eventOverrides,
},
})
}
fireEvent.keyUp(currentElement(), {
key,
keyCode,
which: keyCode,
...eventOverrides,
})
},
'{backspace}': ({eventOverrides}) => {
const key = 'Backspace'
const keyCode = 8
const keyPressDefaultNotPrevented = fireEvent.keyDown(currentElement(), {
key,
keyCode,
which: keyCode,
...eventOverrides,
})
if (keyPressDefaultNotPrevented) {
fireInputEventIfNeeded({
...calculateNewBackspaceValue(currentElement()),
eventOverrides: {
inputType: 'deleteContentBackward',
...eventOverrides,
},
})
}
fireEvent.keyUp(currentElement(), {
key,
keyCode,
which: keyCode,
...eventOverrides,
})
},
// the user can actually select in several different ways
// we're not going to choose, so we'll *only* set the selection range
'{selectall}': () => {
currentElement().setSelectionRange(0, currentElement().value.length)
},
}
function modifier({name, key, keyCode, modifierProperty}) {
function open({eventOverrides}) {
const newEventOverrides = {[modifierProperty]: true}
fireEvent.keyDown(currentElement(), {
key,
keyCode,
which: keyCode,
...eventOverrides,
...newEventOverrides,
})
return {eventOverrides: newEventOverrides}
}
open.close = {name: [`{/${name}}`], fn: close}
function close({eventOverrides}) {
const newEventOverrides = {[modifierProperty]: false}
fireEvent.keyUp(currentElement(), {
key,
keyCode,
which: keyCode,
...eventOverrides,
...newEventOverrides,
})
return {eventOverrides: newEventOverrides}
}
return {
[`{${name}}`]: open,
[`{/${name}}`]: close,
}
}
}
export {type}
/*
eslint
no-loop-func: "off",
max-lines-per-function: "off",
*/