-
Notifications
You must be signed in to change notification settings - Fork 240
/
dblclick.js
292 lines (266 loc) Β· 8.44 KB
/
dblclick.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
import userEvent from '../'
import {setup, addEventListener, addListeners} from './helpers/utils'
test('fires the correct events on buttons', () => {
const {element, getEventSnapshot} = setup('<button />')
userEvent.dblClick(element)
expect(getEventSnapshot()).toMatchInlineSnapshot(`
Events fired on: button
button - pointerover
button - pointerenter
button - mouseover: Left (0)
button - mouseenter: Left (0)
button - pointermove
button - mousemove: Left (0)
button - pointerdown
button - mousedown: Left (0)
button - focus
button - focusin
button - pointerup
button - mouseup: Left (0)
button - click: Left (0)
button - pointerdown
button - mousedown: Left (0)
button - pointerup
button - mouseup: Left (0)
button - click: Left (0)
button - dblclick: Left (0)
`)
})
/*
input[checked=false] - pointerdown
input[checked=false] - mousedown
input[checked=false] - focus
input[checked=false] - focusin
input[checked=false] - pointerup
input[checked=false] - mouseup
input[checked=true] - click
input[checked=true] - input
input[checked=true] - change
input[checked=true] - pointerdown
input[checked=true] - mousedown
input[checked=true] - pointerup
input[checked=true] - mouseup
input[checked=false] - click
input[checked=false] - input
input[checked=false] - change
input[checked=false] - dblclick
*/
test('fires the correct events on checkboxes', () => {
const {element, getEventSnapshot} = setup('<input type="checkbox" />')
userEvent.dblClick(element)
expect(getEventSnapshot()).toMatchInlineSnapshot(`
Events fired on: input[checked=false]
input[checked=false] - pointerover
input[checked=false] - pointerenter
input[checked=false] - mouseover: Left (0)
input[checked=false] - mouseenter: Left (0)
input[checked=false] - pointermove
input[checked=false] - mousemove: Left (0)
input[checked=false] - pointerdown
input[checked=false] - mousedown: Left (0)
input[checked=false] - focus
input[checked=false] - focusin
input[checked=false] - pointerup
input[checked=false] - mouseup: Left (0)
input[checked=true] - click: Left (0)
unchecked -> checked
input[checked=true] - input
input[checked=true] - change
input[checked=true] - pointerdown
input[checked=true] - mousedown: Left (0)
input[checked=true] - pointerup
input[checked=true] - mouseup: Left (0)
input[checked=false] - click: Left (0)
checked -> unchecked
input[checked=false] - input
input[checked=false] - change
input[checked=false] - dblclick: Left (0)
`)
})
test('fires the correct events on regular inputs', () => {
const {element, getEventSnapshot} = setup('<input />')
userEvent.dblClick(element)
expect(getEventSnapshot()).toMatchInlineSnapshot(`
Events fired on: input[value=""]
input[value=""] - pointerover
input[value=""] - pointerenter
input[value=""] - mouseover: Left (0)
input[value=""] - mouseenter: Left (0)
input[value=""] - pointermove
input[value=""] - mousemove: Left (0)
input[value=""] - pointerdown
input[value=""] - mousedown: Left (0)
input[value=""] - focus
input[value=""] - focusin
input[value=""] - pointerup
input[value=""] - mouseup: Left (0)
input[value=""] - click: Left (0)
input[value=""] - pointerdown
input[value=""] - mousedown: Left (0)
input[value=""] - pointerup
input[value=""] - mouseup: Left (0)
input[value=""] - click: Left (0)
input[value=""] - dblclick: Left (0)
`)
})
test('fires the correct events on divs', () => {
const {element, getEventSnapshot} = setup('<div></div>')
userEvent.dblClick(element)
expect(getEventSnapshot()).toMatchInlineSnapshot(`
Events fired on: div
div - pointerover
div - pointerenter
div - mouseover: Left (0)
div - mouseenter: Left (0)
div - pointermove
div - mousemove: Left (0)
div - pointerdown
div - mousedown: Left (0)
div - pointerup
div - mouseup: Left (0)
div - click: Left (0)
div - pointerdown
div - mousedown: Left (0)
div - pointerup
div - mouseup: Left (0)
div - click: Left (0)
div - dblclick: Left (0)
`)
})
test('blurs the previous element', () => {
const {element} = setup(`
<div>
<button id="button-a" />
<button id="button-b" />
</div>
`)
const a = element.children[0]
const b = element.children[1]
const {getEventSnapshot, clearEventCalls} = addListeners(a)
userEvent.dblClick(a)
clearEventCalls()
userEvent.dblClick(b)
expect(getEventSnapshot()).toMatchInlineSnapshot(`
Events fired on: button#button-a
button#button-a - blur
button#button-a - focusout
`)
})
test('does not fire focus event if the element is already focused', () => {
const {element, clearEventCalls, eventWasFired} = setup(`<button />`)
element.focus()
clearEventCalls()
userEvent.dblClick(element)
expect(eventWasFired('focus')).toBe(false)
})
test('clicking an element in a label gives the control focus', () => {
const {element} = setup(`
<div>
<label for="nested-input">
<span>nested</span>
</label>
<input id="nested-input" />
</div>
`)
userEvent.dblClick(element.querySelector('span'))
expect(element.querySelector('input')).toHaveFocus()
})
test('does not blur the previous element when mousedown prevents default', () => {
const {element} = setup(`
<div>
<button id="button-a" />
<button id="button-b" />
</div>
`)
const a = element.children[0]
const b = element.children[1]
addEventListener(b, 'mousedown', e => e.preventDefault())
const {getEventSnapshot, clearEventCalls} = addListeners(a)
userEvent.dblClick(a)
clearEventCalls()
userEvent.dblClick(b)
expect(getEventSnapshot()).toMatchInlineSnapshot(
`No events were fired on: button#button-a`,
)
})
test('fires mouse events with the correct properties', () => {
const {element, getClickEventsSnapshot} = setup('<div></div>')
userEvent.dblClick(element)
expect(getClickEventsSnapshot()).toMatchInlineSnapshot(`
pointerover
pointerenter
mouseover - button=0; buttons=0; detail=0
mouseenter - button=0; buttons=0; detail=0
pointermove
mousemove - button=0; buttons=0; detail=0
pointerdown
mousedown - button=0; buttons=1; detail=1
pointerup
mouseup - button=0; buttons=1; detail=1
click - button=0; buttons=1; detail=1
pointerdown
mousedown - button=0; buttons=1; detail=2
pointerup
mouseup - button=0; buttons=1; detail=2
click - button=0; buttons=1; detail=2
dblclick - button=0; buttons=1; detail=2
`)
})
test('fires mouse events with custom button property', () => {
const {element, getClickEventsSnapshot} = setup('<div></div>')
userEvent.dblClick(element, {
button: 1,
altKey: true,
})
expect(getClickEventsSnapshot()).toMatchInlineSnapshot(`
pointerover
pointerenter
mouseover - button=0; buttons=0; detail=0
mouseenter - button=0; buttons=0; detail=0
pointermove
mousemove - button=0; buttons=0; detail=0
pointerdown
mousedown - button=1; buttons=4; detail=1
pointerup
mouseup - button=1; buttons=4; detail=1
click - button=1; buttons=4; detail=1
pointerdown
mousedown - button=1; buttons=4; detail=2
pointerup
mouseup - button=1; buttons=4; detail=2
click - button=1; buttons=4; detail=2
dblclick - button=1; buttons=4; detail=2
`)
})
test('fires mouse events with custom buttons property', () => {
const {element, getClickEventsSnapshot} = setup('<div></div>')
userEvent.dblClick(element, {buttons: 4})
expect(getClickEventsSnapshot()).toMatchInlineSnapshot(`
pointerover
pointerenter
mouseover - button=0; buttons=0; detail=0
mouseenter - button=0; buttons=0; detail=0
pointermove
mousemove - button=0; buttons=0; detail=0
pointerdown
mousedown - button=1; buttons=4; detail=1
pointerup
mouseup - button=1; buttons=4; detail=1
click - button=1; buttons=4; detail=1
pointerdown
mousedown - button=1; buttons=4; detail=2
pointerup
mouseup - button=1; buttons=4; detail=2
click - button=1; buttons=4; detail=2
dblclick - button=1; buttons=4; detail=2
`)
})
test('fires no events when dblClick element with pointer-events set to none', () => {
const {element, getEventSnapshot} = setup(
`<div style="pointer-events: none"></div>`,
)
userEvent.dblClick(element)
expect(getEventSnapshot()).toMatchInlineSnapshot(
`No events were fired on: div`,
)
})