-
Notifications
You must be signed in to change notification settings - Fork 240
/
click.js
112 lines (104 loc) 路 3.33 KB
/
click.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
import {fireEvent} from '@testing-library/dom'
import {
getMouseEventOptions,
isLabelWithInternallyDisabledControl,
isFocusable,
} from './utils'
import {hover} from './hover'
import {blur} from './blur'
import {focus} from './focus'
function getPreviouslyFocusedElement(element) {
const focusedElement = element.ownerDocument.activeElement
const wasAnotherElementFocused =
focusedElement &&
focusedElement !== element.ownerDocument.body &&
focusedElement !== element
return wasAnotherElementFocused ? focusedElement : null
}
function clickLabel(label, init, {clickCount}) {
if (isLabelWithInternallyDisabledControl(label)) return
fireEvent.pointerDown(label, init)
fireEvent.mouseDown(
label,
getMouseEventOptions('mousedown', init, clickCount),
)
fireEvent.pointerUp(label, init)
fireEvent.mouseUp(label, getMouseEventOptions('mouseup', init, clickCount))
fireEvent.click(label, getMouseEventOptions('click', init, clickCount))
// clicking the label will trigger a click of the label.control
// however, it will not focus the label.control so we have to do it
// ourselves.
if (label.control) focus(label.control)
}
function clickBooleanElement(element, init, clickCount) {
fireEvent.pointerDown(element, init)
if (!element.disabled) {
fireEvent.mouseDown(
element,
getMouseEventOptions('mousedown', init, clickCount),
)
}
focus(element, init)
fireEvent.pointerUp(element, init)
if (!element.disabled) {
fireEvent.mouseUp(
element,
getMouseEventOptions('mouseup', init, clickCount),
)
fireEvent.click(element, getMouseEventOptions('click', init, clickCount))
}
}
function clickElement(element, init, {clickCount}) {
const previousElement = getPreviouslyFocusedElement(element)
fireEvent.pointerDown(element, init)
if (!element.disabled) {
const continueDefaultHandling = fireEvent.mouseDown(
element,
getMouseEventOptions('mousedown', init, clickCount),
)
if (
continueDefaultHandling &&
element !== element.ownerDocument.activeElement
) {
if (previousElement && !isFocusable(element)) {
blur(previousElement, init)
} else {
focus(element, init)
}
}
}
fireEvent.pointerUp(element, init)
if (!element.disabled) {
fireEvent.mouseUp(
element,
getMouseEventOptions('mouseup', init, clickCount),
)
fireEvent.click(element, getMouseEventOptions('click', init, clickCount))
const parentLabel = element.closest('label')
if (parentLabel?.control) focus(parentLabel.control, init)
}
}
function click(element, init, {skipHover = false, clickCount = 0} = {}) {
if (!skipHover) hover(element, init)
switch (element.tagName) {
case 'LABEL':
clickLabel(element, init, {clickCount})
break
case 'INPUT':
if (element.type === 'checkbox' || element.type === 'radio') {
clickBooleanElement(element, init, {clickCount})
} else {
clickElement(element, init, {clickCount})
}
break
default:
clickElement(element, init, {clickCount})
}
}
function dblClick(element, init) {
hover(element, init)
click(element, init, {skipHover: true, clickCount: 0})
click(element, init, {skipHover: true, clickCount: 1})
fireEvent.dblClick(element, getMouseEventOptions('dblclick', init, 2))
}
export {click, dblClick}