UNPKG

3.84 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.click = click;
7exports.dblClick = dblClick;
8
9var _dom = require("@testing-library/dom");
10
11var _utils = require("./utils");
12
13var _hover = require("./hover");
14
15var _blur = require("./blur");
16
17var _focus = require("./focus");
18
19function getPreviouslyFocusedElement(element) {
20 const focusedElement = element.ownerDocument.activeElement;
21 const wasAnotherElementFocused = focusedElement && focusedElement !== element.ownerDocument.body && focusedElement !== element;
22 return wasAnotherElementFocused ? focusedElement : null;
23}
24
25function clickLabel(label, init, {
26 clickCount
27}) {
28 if ((0, _utils.isLabelWithInternallyDisabledControl)(label)) return;
29
30 _dom.fireEvent.pointerDown(label, init);
31
32 _dom.fireEvent.mouseDown(label, (0, _utils.getMouseEventOptions)('mousedown', init, clickCount));
33
34 _dom.fireEvent.pointerUp(label, init);
35
36 _dom.fireEvent.mouseUp(label, (0, _utils.getMouseEventOptions)('mouseup', init, clickCount));
37
38 _dom.fireEvent.click(label, (0, _utils.getMouseEventOptions)('click', init, clickCount)); // clicking the label will trigger a click of the label.control
39 // however, it will not focus the label.control so we have to do it
40 // ourselves.
41
42
43 if (label.control) (0, _focus.focus)(label.control);
44}
45
46function clickBooleanElement(element, init, clickCount) {
47 _dom.fireEvent.pointerDown(element, init);
48
49 if (!element.disabled) {
50 _dom.fireEvent.mouseDown(element, (0, _utils.getMouseEventOptions)('mousedown', init, clickCount));
51 }
52
53 (0, _focus.focus)(element, init);
54
55 _dom.fireEvent.pointerUp(element, init);
56
57 if (!element.disabled) {
58 _dom.fireEvent.mouseUp(element, (0, _utils.getMouseEventOptions)('mouseup', init, clickCount));
59
60 _dom.fireEvent.click(element, (0, _utils.getMouseEventOptions)('click', init, clickCount));
61 }
62}
63
64function clickElement(element, init, {
65 clickCount
66}) {
67 const previousElement = getPreviouslyFocusedElement(element);
68
69 _dom.fireEvent.pointerDown(element, init);
70
71 if (!element.disabled) {
72 const continueDefaultHandling = _dom.fireEvent.mouseDown(element, (0, _utils.getMouseEventOptions)('mousedown', init, clickCount));
73
74 if (continueDefaultHandling && element !== element.ownerDocument.activeElement) {
75 if (previousElement && !(0, _utils.isFocusable)(element)) {
76 (0, _blur.blur)(previousElement, init);
77 } else {
78 (0, _focus.focus)(element, init);
79 }
80 }
81 }
82
83 _dom.fireEvent.pointerUp(element, init);
84
85 if (!element.disabled) {
86 _dom.fireEvent.mouseUp(element, (0, _utils.getMouseEventOptions)('mouseup', init, clickCount));
87
88 _dom.fireEvent.click(element, (0, _utils.getMouseEventOptions)('click', init, clickCount));
89
90 const parentLabel = element.closest('label');
91 if (parentLabel == null ? void 0 : parentLabel.control) (0, _focus.focus)(parentLabel.control, init);
92 }
93}
94
95function click(element, init, {
96 skipHover = false,
97 clickCount = 0
98} = {}) {
99 if (!skipHover) (0, _hover.hover)(element, init);
100
101 switch (element.tagName) {
102 case 'LABEL':
103 clickLabel(element, init, {
104 clickCount
105 });
106 break;
107
108 case 'INPUT':
109 if (element.type === 'checkbox' || element.type === 'radio') {
110 clickBooleanElement(element, init, {
111 clickCount
112 });
113 } else {
114 clickElement(element, init, {
115 clickCount
116 });
117 }
118
119 break;
120
121 default:
122 clickElement(element, init, {
123 clickCount
124 });
125 }
126}
127
128function dblClick(element, init) {
129 (0, _hover.hover)(element, init);
130 click(element, init, {
131 skipHover: true,
132 clickCount: 0
133 });
134 click(element, init, {
135 skipHover: true,
136 clickCount: 1
137 });
138
139 _dom.fireEvent.dblClick(element, (0, _utils.getMouseEventOptions)('dblclick', init, 2));
140}
\No newline at end of file