Skip to content

Commit 03d08c8

Browse files
committed
test: add all event types as listeners
1 parent a9c6702 commit 03d08c8

File tree

5 files changed

+75
-29
lines changed

5 files changed

+75
-29
lines changed

src/user-event/__tests__/clear.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ test('clears text', async () => {
1818
mouseup: Left (0)
1919
click: Left (0)
2020
dblclick: Left (0)
21+
select
2122
keydown: Backspace (8)
2223
keyup: Backspace (8)
2324
input: "{SELECTION}hello{/SELECTION}" -> "hello"
@@ -51,6 +52,7 @@ test('does not clear text on readonly inputs', async () => {
5152
mouseup: Left (0)
5253
click: Left (0)
5354
dblclick: Left (0)
55+
select
5456
keydown: Backspace (8)
5557
keyup: Backspace (8)
5658
`)

src/user-event/__tests__/helpers/utils.js

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import {eventMap} from '../../../event-map'
12
// this is pretty helpful:
2-
// https://jsbin.com/nimelileyo/edit?js,output
3+
// https://codesandbox.io/s/quizzical-worker-eo909
34

45
// all of the stuff below is complex magic that makes the simpler tests work
56
// sorrynotsorry...
@@ -105,33 +106,11 @@ function getElementDisplayName(element) {
105106

106107
function addListeners(element, {eventHandlers = {}} = {}) {
107108
const generalListener = jest.fn().mockName('eventListener')
108-
const listeners = [
109-
'submit',
110-
'keydown',
111-
'keyup',
112-
'keypress',
113-
'input',
114-
'change',
115-
'blur',
116-
'focus',
117-
'focusin',
118-
'focusout',
119-
'click',
120-
'dblclick',
121-
'mouseover',
122-
'mousemove',
123-
'mouseenter',
124-
'mouseleave',
125-
'mouseup',
126-
'mousedown',
127-
]
109+
const listeners = Object.keys(eventMap)
128110

129111
for (const name of listeners) {
130-
addEventListener(element, name, (...args) => {
131-
const [, handler] =
132-
Object.entries(eventHandlers).find(
133-
([key]) => key.toLowerCase() === name,
134-
) ?? []
112+
addEventListener(element, name.toLowerCase(), (...args) => {
113+
const handler = eventHandlers[name]
135114
if (handler) {
136115
generalListener(...args)
137116
return handler(...args)
@@ -146,7 +125,7 @@ function addListeners(element, {eventHandlers = {}} = {}) {
146125
function getEventCalls() {
147126
const eventCalls = generalListener.mock.calls
148127
.map(([event]) => {
149-
const window = event.target.ownerDocument.defaultView
128+
const window = event.target?.ownerDocument.defaultView
150129
const modifiers = ['altKey', 'shiftKey', 'metaKey', 'ctrlKey']
151130
.filter(key => event[key])
152131
.map(k => `{${k.replace('Key', '')}}`)
@@ -161,9 +140,9 @@ function addListeners(element, {eventHandlers = {}} = {}) {
161140
log = getCheckboxOrRadioClickedLine(event)
162141
} else if (event.type === 'input' && event.hasOwnProperty('testData')) {
163142
log = getInputLine(element, event)
164-
} else if (event instanceof window.KeyboardEvent) {
143+
} else if (window && event instanceof window.KeyboardEvent) {
165144
log = getKeyboardEventLine(event)
166-
} else if (event instanceof window.MouseEvent) {
145+
} else if (window && event instanceof window.MouseEvent) {
167146
log = getMouseEventLine(event)
168147
}
169148

src/user-event/__tests__/type-modifiers.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ test('{esc} triggers typing the escape character', async () => {
2424
Events fired on: input[value=""]
2525
2626
focus
27+
select
2728
keydown: Escape (27)
2829
keyup: Escape (27)
2930
`)
@@ -36,12 +37,15 @@ test('a{backspace}', async () => {
3637
Events fired on: input[value=""]
3738
3839
focus
40+
select
3941
keydown: a (97)
4042
keypress: a (97)
4143
input: "{CURSOR}" -> "a"
44+
select
4245
keyup: a (97)
4346
keydown: Backspace (8)
4447
input: "a{CURSOR}" -> ""
48+
select
4549
keyup: Backspace (8)
4650
`)
4751
})
@@ -53,11 +57,13 @@ test('{backspace}a', async () => {
5357
Events fired on: input[value="a"]
5458
5559
focus
60+
select
5661
keydown: Backspace (8)
5762
keyup: Backspace (8)
5863
keydown: a (97)
5964
keypress: a (97)
6065
input: "{CURSOR}" -> "a"
66+
select
6167
keyup: a (97)
6268
`)
6369
})
@@ -71,9 +77,11 @@ test('{backspace} triggers typing the backspace character and deletes the charac
7177
expect(getEventCalls()).toMatchInlineSnapshot(`
7278
Events fired on: input[value="o"]
7379
80+
select
7481
focus
7582
keydown: Backspace (8)
7683
input: "y{CURSOR}o" -> "o"
84+
select
7785
keyup: Backspace (8)
7886
`)
7987
})
@@ -87,6 +95,7 @@ test('{backspace} on a readOnly input', async () => {
8795
expect(getEventCalls()).toMatchInlineSnapshot(`
8896
Events fired on: input[value="yo"]
8997
98+
select
9099
focus
91100
keydown: Backspace (8)
92101
keyup: Backspace (8)
@@ -104,6 +113,7 @@ test('{backspace} does not fire input if keydown prevents default', async () =>
104113
expect(getEventCalls()).toMatchInlineSnapshot(`
105114
Events fired on: input[value="yo"]
106115
116+
select
107117
focus
108118
keydown: Backspace (8)
109119
keyup: Backspace (8)
@@ -119,9 +129,11 @@ test('{backspace} deletes the selected range', async () => {
119129
expect(getEventCalls()).toMatchInlineSnapshot(`
120130
Events fired on: input[value="Here"]
121131
132+
select
122133
focus
123134
keydown: Backspace (8)
124135
input: "H{SELECTION}i th{/SELECTION}ere" -> "Here"
136+
select
125137
keyup: Backspace (8)
126138
`)
127139
})
@@ -143,10 +155,12 @@ test('{alt}a{/alt}', async () => {
143155
Events fired on: input[value="a"]
144156
145157
focus
158+
select
146159
keydown: Alt (18) {alt}
147160
keydown: a (97) {alt}
148161
keypress: a (97) {alt}
149162
input: "{CURSOR}" -> "a"
163+
select
150164
keyup: a (97) {alt}
151165
keyup: Alt (18)
152166
`)
@@ -161,10 +175,12 @@ test('{meta}a{/meta}', async () => {
161175
Events fired on: input[value="a"]
162176
163177
focus
178+
select
164179
keydown: Meta (93) {meta}
165180
keydown: a (97) {meta}
166181
keypress: a (97) {meta}
167182
input: "{CURSOR}" -> "a"
183+
select
168184
keyup: a (97) {meta}
169185
keyup: Meta (93)
170186
`)
@@ -179,10 +195,12 @@ test('{ctrl}a{/ctrl}', async () => {
179195
Events fired on: input[value="a"]
180196
181197
focus
198+
select
182199
keydown: Control (17) {ctrl}
183200
keydown: a (97) {ctrl}
184201
keypress: a (97) {ctrl}
185202
input: "{CURSOR}" -> "a"
203+
select
186204
keyup: a (97) {ctrl}
187205
keyup: Control (17)
188206
`)
@@ -197,10 +215,12 @@ test('{shift}a{/shift}', async () => {
197215
Events fired on: input[value="a"]
198216
199217
focus
218+
select
200219
keydown: Shift (16) {shift}
201220
keydown: a (97) {shift}
202221
keypress: a (97) {shift}
203222
input: "{CURSOR}" -> "a"
223+
select
204224
keyup: a (97) {shift}
205225
keyup: Shift (16)
206226
`)
@@ -215,9 +235,11 @@ test('a{enter}', async () => {
215235
Events fired on: input[value="a"]
216236
217237
focus
238+
select
218239
keydown: a (97)
219240
keypress: a (97)
220241
input: "{CURSOR}" -> "a"
242+
select
221243
keyup: a (97)
222244
keydown: Enter (13)
223245
keypress: Enter (13)
@@ -238,6 +260,7 @@ test('{enter} with preventDefault keydown', async () => {
238260
Events fired on: input[value=""]
239261
240262
focus
263+
select
241264
keydown: Enter (13)
242265
keyup: Enter (13)
243266
`)
@@ -268,10 +291,12 @@ test('{enter} on a textarea', async () => {
268291
Events fired on: textarea[value="\\n"]
269292
270293
focus
294+
select
271295
keydown: Enter (13)
272296
keypress: Enter (13)
273297
input: "{CURSOR}" -> "
274298
"
299+
select
275300
keyup: Enter (13)
276301
`)
277302
})
@@ -303,12 +328,14 @@ test('{meta}{alt}{ctrl}a{/ctrl}{/alt}{/meta}', async () => {
303328
Events fired on: input[value="a"]
304329
305330
focus
331+
select
306332
keydown: Meta (93) {meta}
307333
keydown: Alt (18) {alt}{meta}
308334
keydown: Control (17) {alt}{meta}{ctrl}
309335
keydown: a (97) {alt}{meta}{ctrl}
310336
keypress: a (97) {alt}{meta}{ctrl}
311337
input: "{CURSOR}" -> "a"
338+
select
312339
keyup: a (97) {alt}{meta}{ctrl}
313340
keyup: Control (17) {alt}{meta}
314341
keyup: Alt (18) {meta}

0 commit comments

Comments
 (0)