-
Notifications
You must be signed in to change notification settings - Fork 240
/
functional.ts
138 lines (126 loc) Β· 4.08 KB
/
functional.ts
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
/**
* This file should contain behavior for functional keys as described here:
* https://w3c.github.io/uievents-code/#key-alphanumeric-functional
*/
import {fireEvent} from '@testing-library/dom'
import {getValue, isClickableInput, isElementType} from '../../utils'
import {getKeyEventProps, getMouseEventProps} from '../getEventProps'
import {fireInputEventIfNeeded} from '../shared'
import {behaviorPlugin} from '../types'
import {calculateNewBackspaceValue} from './functional/calculateBackspaceValue'
const modifierKeys = {
Alt: 'alt',
Control: 'ctrl',
Shift: 'shift',
Meta: 'meta',
} as const
export const preKeydownBehavior: behaviorPlugin[] = [
// modifierKeys switch on the modifier BEFORE the keydown event
...Object.entries(modifierKeys).map(
([key, modKey]): behaviorPlugin => ({
matches: keyDef => keyDef.key === key,
handle: (keyDef, element, options, state) => {
state.modifiers[modKey] = true
},
}),
),
// AltGraph produces an extra keydown for Control
// The modifier does not change
{
matches: keyDef => keyDef.key === 'AltGraph',
handle: (keyDef, element, options, state) => {
const ctrlKeyDef = options.keyboardMap.find(
k => k.key === 'Control',
) ?? /* istanbul ignore next */ {key: 'Control', code: 'Control'}
fireEvent.keyDown(element, getKeyEventProps(ctrlKeyDef, state))
},
},
]
export const keydownBehavior: behaviorPlugin[] = [
{
matches: keyDef => keyDef.key === 'CapsLock',
handle: (keyDef, element, options, state) => {
state.modifiers.caps = !state.modifiers.caps
},
},
{
matches: keyDef => keyDef.key === 'Backspace',
handle: (keyDef, element, options, state) => {
const {newValue, newSelectionStart} = calculateNewBackspaceValue(
element,
state.carryValue,
)
fireInputEventIfNeeded({
newValue,
newSelectionStart,
eventOverrides: {
inputType: 'deleteContentBackward',
},
currentElement: () => element,
})
if (state.carryValue) {
state.carryValue = getValue(element) === newValue ? undefined : newValue
}
},
},
]
export const keypressBehavior: behaviorPlugin[] = [
{
matches: (keyDef, element) =>
keyDef.key === 'Enter' &&
(isClickableInput(element) ||
// Links with href defined should handle Enter the same as a click
(isElementType(element, 'a') && Boolean(element.href))),
handle: (keyDef, element, options, state) => {
fireEvent.click(element, getMouseEventProps(state))
},
},
{
matches: (keyDef, element) =>
keyDef.key === 'Enter' && isElementType(element, 'input'),
handle: (keyDef, element) => {
const form = (element as HTMLInputElement).form
if (
form &&
(form.querySelectorAll('input').length === 1 ||
form.querySelector('input[type="submit"]') ||
form.querySelector('button[type="submit"]'))
) {
fireEvent.submit(form)
}
},
},
]
export const preKeyupBehavior: behaviorPlugin[] = [
// modifierKeys switch off the modifier BEFORE the keyup event
...Object.entries(modifierKeys).map(
([key, modKey]): behaviorPlugin => ({
matches: keyDef => keyDef.key === key,
handle: (keyDef, element, options, state) => {
state.modifiers[modKey] = false
},
}),
),
]
export const keyupBehavior: behaviorPlugin[] = [
{
matches: (keyDef, element) =>
keyDef.key === ' ' && isClickableInput(element),
handle: (keyDef, element, options, state) => {
fireEvent.click(element, getMouseEventProps(state))
},
},
]
export const postKeyupBehavior: behaviorPlugin[] = [
// AltGraph produces an extra keyup for Control
// The modifier does not change
{
matches: keyDef => keyDef.key === 'AltGraph',
handle: (keyDef, element, options, state) => {
const ctrlKeyDef = options.keyboardMap.find(
k => k.key === 'Control',
) ?? /* istanbul ignore next */ {key: 'Control', code: 'Control'}
fireEvent.keyUp(element, getKeyEventProps(ctrlKeyDef, state))
},
},
]