/
type-modifiers.js
277 lines (227 loc) Β· 7.06 KB
/
type-modifiers.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
import React from 'react'
import userEvent from '..'
import {setup, addEventListener} from './helpers/utils'
// Note, use the setup function at the bottom of the file...
// but don't hurt yourself trying to read it π
// keep in mind that we do not handle modifier interactions. This is primarily
// because modifiers behave differently on different operating systems.
// For example: {alt}{backspace}{/alt} will remove everything from the current
// cursor position to the beginning of the word on Mac, but you need to use
// {ctrl}{backspace}{/ctrl} to do that on Windows. And that doesn't appear to
// be consistent within an OS either π
// So we're not going to even try.
// This also means that '{shift}a' will fire an input event with the shiftKey,
// but will not capitalize "a".
test('{esc} triggers typing the escape character', async () => {
const {element: input, getEventCalls} = setup(<input />)
await userEvent.type(input, '{esc}')
expect(getEventCalls()).toMatchInlineSnapshot(`
focus
keydown: Escape (27)
keyup: Escape (27)
`)
})
test('a{backspace}', async () => {
const {element, getEventCalls} = setup(<input />)
await userEvent.type(element, 'a{backspace}')
expect(getEventCalls()).toMatchInlineSnapshot(`
focus
keydown: a (97)
keypress: a (97)
input: "{CURSOR}" -> "a"
keyup: a (97)
keydown: Backspace (8)
input: "a{CURSOR}" -> ""
keyup: Backspace (8)
`)
})
test('{backspace}a', async () => {
const {element, getEventCalls} = setup(<input />)
await userEvent.type(element, '{backspace}a')
expect(getEventCalls()).toMatchInlineSnapshot(`
focus
keydown: Backspace (8)
keyup: Backspace (8)
keydown: a (97)
keypress: a (97)
input: "{CURSOR}" -> "a"
keyup: a (97)
`)
})
test('{backspace} triggers typing the backspace character and deletes the character behind the cursor', async () => {
const {element: input, getEventCalls} = setup(<input defaultValue="yo" />)
input.setSelectionRange(1, 1)
await userEvent.type(input, '{backspace}')
expect(getEventCalls()).toMatchInlineSnapshot(`
focus
keydown: Backspace (8)
input: "y{CURSOR}o" -> "o"
keyup: Backspace (8)
`)
})
test('{backspace} on a readOnly input', async () => {
const {element: input, getEventCalls} = setup(
<input readOnly defaultValue="yo" />,
)
input.setSelectionRange(1, 1)
await userEvent.type(input, '{backspace}')
expect(getEventCalls()).toMatchInlineSnapshot(`
focus
keydown: Backspace (8)
keyup: Backspace (8)
`)
})
test('{backspace} does not fire input if keydown prevents default', async () => {
const {element: input, getEventCalls} = setup(
<input defaultValue="yo" onKeyDown={e => e.preventDefault()} />,
)
input.setSelectionRange(1, 1)
await userEvent.type(input, '{backspace}')
expect(getEventCalls()).toMatchInlineSnapshot(`
focus
keydown: Backspace (8)
keyup: Backspace (8)
`)
})
test('{backspace} deletes the selected range', async () => {
const {element: input, getEventCalls} = setup(
<input defaultValue="Hi there" />,
)
input.setSelectionRange(1, 5)
await userEvent.type(input, '{backspace}')
expect(getEventCalls()).toMatchInlineSnapshot(`
focus
keydown: Backspace (8)
input: "H{SELECTION}i th{/SELECTION}ere" -> "Here"
keyup: Backspace (8)
`)
})
test('{alt}a{/alt}', async () => {
const {element: input, getEventCalls} = setup(<input />)
await userEvent.type(input, '{alt}a{/alt}')
expect(getEventCalls()).toMatchInlineSnapshot(`
focus
keydown: Alt (18) {alt}
keydown: a (97) {alt}
keypress: a (97) {alt}
input: "{CURSOR}" -> "a"
keyup: a (97) {alt}
keyup: Alt (18)
`)
})
test('{meta}a{/meta}', async () => {
const {element: input, getEventCalls} = setup(<input />)
await userEvent.type(input, '{meta}a{/meta}')
expect(getEventCalls()).toMatchInlineSnapshot(`
focus
keydown: Meta (93) {meta}
keydown: a (97) {meta}
keypress: a (97) {meta}
input: "{CURSOR}" -> "a"
keyup: a (97) {meta}
keyup: Meta (93)
`)
})
test('{ctrl}a{/ctrl}', async () => {
const {element: input, getEventCalls} = setup(<input />)
await userEvent.type(input, '{ctrl}a{/ctrl}')
expect(getEventCalls()).toMatchInlineSnapshot(`
focus
keydown: Control (17) {ctrl}
keydown: a (97) {ctrl}
keypress: a (97) {ctrl}
input: "{CURSOR}" -> "a"
keyup: a (97) {ctrl}
keyup: Control (17)
`)
})
test('{shift}a{/shift}', async () => {
const {element: input, getEventCalls} = setup(<input />)
await userEvent.type(input, '{shift}a{/shift}')
expect(getEventCalls()).toMatchInlineSnapshot(`
focus
keydown: Shift (16) {shift}
keydown: a (97) {shift}
keypress: a (97) {shift}
input: "{CURSOR}" -> "a"
keyup: a (97) {shift}
keyup: Shift (16)
`)
})
test('a{enter}', async () => {
const {element: input, getEventCalls} = setup(<input />)
await userEvent.type(input, 'a{enter}')
expect(getEventCalls()).toMatchInlineSnapshot(`
focus
keydown: a (97)
keypress: a (97)
input: "{CURSOR}" -> "a"
keyup: a (97)
keydown: Enter (13)
keypress: Enter (13)
keyup: Enter (13)
`)
})
test('{enter} with preventDefault keydown', async () => {
const {element: input, getEventCalls} = setup(<input />)
addEventListener(input, 'keydown', e => e.preventDefault())
await userEvent.type(input, '{enter}')
expect(getEventCalls()).toMatchInlineSnapshot(`
focus
keydown: Enter (13)
keyup: Enter (13)
`)
})
test('{enter} on a button', async () => {
const {element: button, getEventCalls} = setup(<button />)
await userEvent.type(button, '{enter}')
expect(getEventCalls()).toMatchInlineSnapshot(`
focus
keydown: Enter (13)
keypress: Enter (13)
click: Left (0)
keyup: Enter (13)
`)
})
test('{enter} on a textarea', async () => {
const {element: textarea, getEventCalls} = setup(<textarea />)
await userEvent.type(textarea, '{enter}')
expect(getEventCalls()).toMatchInlineSnapshot(`
focus
keydown: Enter (13)
keypress: Enter (13)
input: "{CURSOR}" -> "
"
keyup: Enter (13)
`)
})
test('{meta}{enter}{/meta} on a button', async () => {
const {element: button, getEventCalls} = setup(<button />)
await userEvent.type(button, '{meta}{enter}{/meta}')
expect(getEventCalls()).toMatchInlineSnapshot(`
focus
keydown: Meta (93) {meta}
keydown: Enter (13) {meta}
keypress: Enter (13) {meta}
click: Left (0) {meta}
keyup: Enter (13) {meta}
keyup: Meta (93)
`)
})
test('{meta}{alt}{ctrl}a{/ctrl}{/alt}{/meta}', async () => {
const {element: input, getEventCalls} = setup(<input />)
await userEvent.type(input, '{meta}{alt}{ctrl}a{/ctrl}{/alt}{/meta}')
expect(getEventCalls()).toMatchInlineSnapshot(`
focus
keydown: Meta (93) {meta}
keydown: Alt (18) {alt}{meta}
keydown: Control (17) {alt}{meta}{ctrl}
keydown: a (97) {alt}{meta}{ctrl}
keypress: a (97) {alt}{meta}{ctrl}
input: "{CURSOR}" -> "a"
keyup: a (97) {alt}{meta}{ctrl}
keyup: Control (17) {alt}{meta}
keyup: Alt (18) {meta}
keyup: Meta (93)
`)
})