-
Notifications
You must be signed in to change notification settings - Fork 249
/
hover.ts
80 lines (72 loc) · 2.28 KB
/
hover.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
import {fireEvent} from '@testing-library/dom'
import {
isLabelWithInternallyDisabledControl,
getMouseEventOptions,
isDisabled,
hasPointerEvents,
PointerOptions,
} from './utils'
// includes `element`
function getParentElements(element: Element) {
const parentElements = [element]
let currentElement: Element | null = element
while ((currentElement = currentElement.parentElement) != null) {
parentElements.push(currentElement)
}
return parentElements
}
function hover(
element: Element,
init?: MouseEventInit,
{skipPointerEventsCheck = false}: PointerOptions = {},
) {
if (!skipPointerEventsCheck && !hasPointerEvents(element)) {
throw new Error(
'unable to hover element as it has or inherits pointer-events set to "none".',
)
}
if (isLabelWithInternallyDisabledControl(element)) return
const parentElements = getParentElements(element).reverse()
fireEvent.pointerOver(element, init)
for (const el of parentElements) {
fireEvent.pointerEnter(el, init)
}
if (!isDisabled(element)) {
fireEvent.mouseOver(element, getMouseEventOptions('mouseover', init))
for (const el of parentElements) {
fireEvent.mouseEnter(el, getMouseEventOptions('mouseenter', init))
}
}
fireEvent.pointerMove(element, init)
if (!isDisabled(element)) {
fireEvent.mouseMove(element, getMouseEventOptions('mousemove', init))
}
}
function unhover(
element: Element,
init?: MouseEventInit,
{skipPointerEventsCheck = false}: PointerOptions = {},
) {
if (!skipPointerEventsCheck && !hasPointerEvents(element)) {
throw new Error(
'unable to unhover element as it has or inherits pointer-events set to "none".',
)
}
if (isLabelWithInternallyDisabledControl(element)) return
const parentElements = getParentElements(element)
fireEvent.pointerMove(element, init)
if (!isDisabled(element)) {
fireEvent.mouseMove(element, getMouseEventOptions('mousemove', init))
}
fireEvent.pointerOut(element, init)
for (const el of parentElements) {
fireEvent.pointerLeave(el, init)
}
if (!isDisabled(element)) {
fireEvent.mouseOut(element, getMouseEventOptions('mouseout', init))
for (const el of parentElements) {
fireEvent.mouseLeave(el, getMouseEventOptions('mouseleave', init))
}
}
}
export {hover, unhover}