@testing-library/user-event 12.2.1 → 12.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/blur.js +15 -0
- package/dist/clear.js +35 -0
- package/dist/click.js +140 -0
- package/dist/focus.js +15 -0
- package/dist/hover.js +73 -0
- package/dist/index.js +38 -0
- package/dist/keys/navigation-key.js +54 -0
- package/dist/paste.js +55 -0
- package/dist/select-options.js +97 -0
- package/dist/tab.js +137 -0
- package/dist/type.js +717 -0
- package/dist/upload.js +57 -0
- package/dist/utils.js +252 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -596,12 +596,12 @@ Thanks goes to these people ([emoji key][emojis]):
|
|
|
596
596
|
<td align="center"><a href="https://www.linkedin.com/in/luis-takahashi/"><img src="https://avatars0.githubusercontent.com/u/19766035?v=4" width="100px;" alt=""/><br /><sub><b>Luís Takahashi</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=luistak" title="Code">💻</a> <a href="https://github.com/testing-library/user-event/commits?author=luistak" title="Tests">⚠️</a></td>
|
|
597
597
|
<td align="center"><a href="https://github.com/jesujcastillom"><img src="https://avatars3.githubusercontent.com/u/7827281?v=4" width="100px;" alt=""/><br /><sub><b>Jesu Castillo</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=jesujcastillom" title="Code">💻</a> <a href="https://github.com/testing-library/user-event/commits?author=jesujcastillom" title="Tests">⚠️</a></td>
|
|
598
598
|
<td align="center"><a href="https://sarahdayan.dev"><img src="https://avatars1.githubusercontent.com/u/5370675?v=4" width="100px;" alt=""/><br /><sub><b>Sarah Dayan</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=sarahdayan" title="Documentation">📖</a></td>
|
|
599
|
+
<td align="center"><a href="http://saul-mirone.github.io/"><img src="https://avatars0.githubusercontent.com/u/10047788?v=4" width="100px;" alt=""/><br /><sub><b>Mirone</b></sub></a><br /><a href="https://github.com/testing-library/user-event/issues?q=author%3ASaul-Mirone" title="Bug reports">🐛</a></td>
|
|
599
600
|
</tr>
|
|
600
601
|
</table>
|
|
601
602
|
|
|
602
603
|
<!-- markdownlint-enable -->
|
|
603
604
|
<!-- prettier-ignore-end -->
|
|
604
|
-
|
|
605
605
|
<!-- ALL-CONTRIBUTORS-LIST:END -->
|
|
606
606
|
|
|
607
607
|
This project follows the [all-contributors][all-contributors] specification.
|
package/dist/blur.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.blur = blur;
|
|
7
|
+
|
|
8
|
+
var _utils = require("./utils");
|
|
9
|
+
|
|
10
|
+
function blur(element) {
|
|
11
|
+
if (!(0, _utils.isFocusable)(element)) return;
|
|
12
|
+
const wasActive = (0, _utils.getActiveElement)(element.ownerDocument) === element;
|
|
13
|
+
if (!wasActive) return;
|
|
14
|
+
(0, _utils.eventWrapper)(() => element.blur());
|
|
15
|
+
}
|
package/dist/clear.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.clear = clear;
|
|
7
|
+
|
|
8
|
+
var _type = require("./type");
|
|
9
|
+
|
|
10
|
+
function clear(element) {
|
|
11
|
+
if (element.tagName !== 'INPUT' && element.tagName !== 'TEXTAREA') {
|
|
12
|
+
// TODO: support contenteditable
|
|
13
|
+
throw new Error('clear currently only supports input and textarea elements.');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (element.disabled) return; // TODO: track the selection range ourselves so we don't have to do this input "type" trickery
|
|
17
|
+
// just like cypress does: https://github.com/cypress-io/cypress/blob/8d7f1a0bedc3c45a2ebf1ff50324b34129fdc683/packages/driver/src/dom/selection.ts#L16-L37
|
|
18
|
+
|
|
19
|
+
const elementType = element.type; // type is a readonly property on textarea, so check if element is an input before trying to modify it
|
|
20
|
+
|
|
21
|
+
if (element.tagName === 'INPUT') {
|
|
22
|
+
// setSelectionRange is not supported on certain types of inputs, e.g. "number" or "email"
|
|
23
|
+
element.type = 'text';
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
(0, _type.type)(element, '{selectall}{del}', {
|
|
27
|
+
delay: 0,
|
|
28
|
+
initialSelectionStart: element.selectionStart,
|
|
29
|
+
initialSelectionEnd: element.selectionEnd
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
if (element.tagName === 'INPUT') {
|
|
33
|
+
element.type = elementType;
|
|
34
|
+
}
|
|
35
|
+
}
|
package/dist/click.js
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.click = click;
|
|
7
|
+
exports.dblClick = dblClick;
|
|
8
|
+
|
|
9
|
+
var _dom = require("@testing-library/dom");
|
|
10
|
+
|
|
11
|
+
var _utils = require("./utils");
|
|
12
|
+
|
|
13
|
+
var _hover = require("./hover");
|
|
14
|
+
|
|
15
|
+
var _blur = require("./blur");
|
|
16
|
+
|
|
17
|
+
var _focus = require("./focus");
|
|
18
|
+
|
|
19
|
+
function 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
|
+
|
|
25
|
+
function 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
|
+
|
|
46
|
+
function 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
|
+
|
|
64
|
+
function 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
|
+
|
|
95
|
+
function 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
|
+
|
|
128
|
+
function 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
|
+
}
|
package/dist/focus.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.focus = focus;
|
|
7
|
+
|
|
8
|
+
var _utils = require("./utils");
|
|
9
|
+
|
|
10
|
+
function focus(element) {
|
|
11
|
+
if (!(0, _utils.isFocusable)(element)) return;
|
|
12
|
+
const isAlreadyActive = (0, _utils.getActiveElement)(element.ownerDocument) === element;
|
|
13
|
+
if (isAlreadyActive) return;
|
|
14
|
+
(0, _utils.eventWrapper)(() => element.focus());
|
|
15
|
+
}
|
package/dist/hover.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.hover = hover;
|
|
7
|
+
exports.unhover = unhover;
|
|
8
|
+
|
|
9
|
+
var _dom = require("@testing-library/dom");
|
|
10
|
+
|
|
11
|
+
var _utils = require("./utils");
|
|
12
|
+
|
|
13
|
+
// includes `element`
|
|
14
|
+
function getParentElements(element) {
|
|
15
|
+
const parentElements = [element];
|
|
16
|
+
let currentElement = element;
|
|
17
|
+
|
|
18
|
+
while ((currentElement = currentElement.parentElement) != null) {
|
|
19
|
+
parentElements.push(currentElement);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return parentElements;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function hover(element, init) {
|
|
26
|
+
if ((0, _utils.isLabelWithInternallyDisabledControl)(element)) return;
|
|
27
|
+
const parentElements = getParentElements(element).reverse();
|
|
28
|
+
|
|
29
|
+
_dom.fireEvent.pointerOver(element, init);
|
|
30
|
+
|
|
31
|
+
for (const el of parentElements) {
|
|
32
|
+
_dom.fireEvent.pointerEnter(el, init);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (!element.disabled) {
|
|
36
|
+
_dom.fireEvent.mouseOver(element, (0, _utils.getMouseEventOptions)('mouseover', init));
|
|
37
|
+
|
|
38
|
+
for (const el of parentElements) {
|
|
39
|
+
_dom.fireEvent.mouseEnter(el, (0, _utils.getMouseEventOptions)('mouseenter', init));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
_dom.fireEvent.pointerMove(element, init);
|
|
44
|
+
|
|
45
|
+
if (!element.disabled) {
|
|
46
|
+
_dom.fireEvent.mouseMove(element, (0, _utils.getMouseEventOptions)('mousemove', init));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function unhover(element, init) {
|
|
51
|
+
if ((0, _utils.isLabelWithInternallyDisabledControl)(element)) return;
|
|
52
|
+
const parentElements = getParentElements(element);
|
|
53
|
+
|
|
54
|
+
_dom.fireEvent.pointerMove(element, init);
|
|
55
|
+
|
|
56
|
+
if (!element.disabled) {
|
|
57
|
+
_dom.fireEvent.mouseMove(element, (0, _utils.getMouseEventOptions)('mousemove', init));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
_dom.fireEvent.pointerOut(element, init);
|
|
61
|
+
|
|
62
|
+
for (const el of parentElements) {
|
|
63
|
+
_dom.fireEvent.pointerLeave(el, init);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (!element.disabled) {
|
|
67
|
+
_dom.fireEvent.mouseOut(element, (0, _utils.getMouseEventOptions)('mouseout', init));
|
|
68
|
+
|
|
69
|
+
for (const el of parentElements) {
|
|
70
|
+
_dom.fireEvent.mouseLeave(el, (0, _utils.getMouseEventOptions)('mouseleave', init));
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _click = require("./click");
|
|
9
|
+
|
|
10
|
+
var _type = require("./type");
|
|
11
|
+
|
|
12
|
+
var _clear = require("./clear");
|
|
13
|
+
|
|
14
|
+
var _tab = require("./tab");
|
|
15
|
+
|
|
16
|
+
var _hover = require("./hover");
|
|
17
|
+
|
|
18
|
+
var _upload = require("./upload");
|
|
19
|
+
|
|
20
|
+
var _selectOptions = require("./select-options");
|
|
21
|
+
|
|
22
|
+
var _paste = require("./paste");
|
|
23
|
+
|
|
24
|
+
const userEvent = {
|
|
25
|
+
click: _click.click,
|
|
26
|
+
dblClick: _click.dblClick,
|
|
27
|
+
type: _type.type,
|
|
28
|
+
clear: _clear.clear,
|
|
29
|
+
tab: _tab.tab,
|
|
30
|
+
hover: _hover.hover,
|
|
31
|
+
unhover: _hover.unhover,
|
|
32
|
+
upload: _upload.upload,
|
|
33
|
+
selectOptions: _selectOptions.selectOptions,
|
|
34
|
+
deselectOptions: _selectOptions.deselectOptions,
|
|
35
|
+
paste: _paste.paste
|
|
36
|
+
};
|
|
37
|
+
var _default = userEvent;
|
|
38
|
+
exports.default = _default;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.navigationKey = navigationKey;
|
|
7
|
+
|
|
8
|
+
var _dom = require("@testing-library/dom");
|
|
9
|
+
|
|
10
|
+
var _utils = require("../utils");
|
|
11
|
+
|
|
12
|
+
const keys = {
|
|
13
|
+
ArrowLeft: {
|
|
14
|
+
keyCode: 37
|
|
15
|
+
},
|
|
16
|
+
ArrowRight: {
|
|
17
|
+
keyCode: 39
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
function getSelectionRange(currentElement, key) {
|
|
22
|
+
const {
|
|
23
|
+
selectionStart,
|
|
24
|
+
selectionEnd
|
|
25
|
+
} = currentElement();
|
|
26
|
+
const cursorChange = Number(key in keys) * (key === 'ArrowLeft' ? -1 : 1);
|
|
27
|
+
return {
|
|
28
|
+
selectionStart: selectionStart + cursorChange,
|
|
29
|
+
selectionEnd: selectionEnd + cursorChange
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function navigationKey(key) {
|
|
34
|
+
const event = {
|
|
35
|
+
key,
|
|
36
|
+
keyCode: keys[key].keyCode,
|
|
37
|
+
which: keys[key].keyCode
|
|
38
|
+
};
|
|
39
|
+
return ({
|
|
40
|
+
currentElement,
|
|
41
|
+
eventOverrides
|
|
42
|
+
}) => {
|
|
43
|
+
_dom.fireEvent.keyDown(currentElement(), { ...event,
|
|
44
|
+
...eventOverrides
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const range = getSelectionRange(currentElement, key);
|
|
48
|
+
(0, _utils.setSelectionRangeIfNecessary)(currentElement(), range.selectionStart, range.selectionEnd);
|
|
49
|
+
|
|
50
|
+
_dom.fireEvent.keyUp(currentElement(), { ...event,
|
|
51
|
+
...eventOverrides
|
|
52
|
+
});
|
|
53
|
+
};
|
|
54
|
+
}
|
package/dist/paste.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.paste = paste;
|
|
7
|
+
|
|
8
|
+
var _dom = require("@testing-library/dom");
|
|
9
|
+
|
|
10
|
+
var _utils = require("./utils");
|
|
11
|
+
|
|
12
|
+
function paste(element, text, init, {
|
|
13
|
+
initialSelectionStart,
|
|
14
|
+
initialSelectionEnd
|
|
15
|
+
} = {}) {
|
|
16
|
+
if (element.disabled) return;
|
|
17
|
+
|
|
18
|
+
if (typeof element.value === 'undefined') {
|
|
19
|
+
throw new TypeError(`the current element is of type ${element.tagName} and doesn't have a valid value`);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
(0, _utils.eventWrapper)(() => element.focus()); // by default, a new element has it's selection start and end at 0
|
|
23
|
+
// but most of the time when people call "paste", they expect it to paste
|
|
24
|
+
// at the end of the current input value. So, if the selection start
|
|
25
|
+
// and end are both the default of 0, then we'll go ahead and change
|
|
26
|
+
// them to the length of the current value.
|
|
27
|
+
// the only time it would make sense to pass the initialSelectionStart or
|
|
28
|
+
// initialSelectionEnd is if you have an input with a value and want to
|
|
29
|
+
// explicitely start typing with the cursor at 0. Not super common.
|
|
30
|
+
|
|
31
|
+
if (element.selectionStart === 0 && element.selectionEnd === 0) {
|
|
32
|
+
(0, _utils.setSelectionRangeIfNecessary)(element, initialSelectionStart != null ? initialSelectionStart : element.value.length, initialSelectionEnd != null ? initialSelectionEnd : element.value.length);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
_dom.fireEvent.paste(element, init);
|
|
36
|
+
|
|
37
|
+
if (!element.readOnly) {
|
|
38
|
+
const {
|
|
39
|
+
newValue,
|
|
40
|
+
newSelectionStart
|
|
41
|
+
} = (0, _utils.calculateNewValue)(text, element);
|
|
42
|
+
|
|
43
|
+
_dom.fireEvent.input(element, {
|
|
44
|
+
inputType: 'insertFromPaste',
|
|
45
|
+
target: {
|
|
46
|
+
value: newValue
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
(0, _utils.setSelectionRangeIfNecessary)(element, {
|
|
51
|
+
newSelectionStart,
|
|
52
|
+
newSelectionEnd: newSelectionStart
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.deselectOptions = exports.selectOptions = void 0;
|
|
7
|
+
|
|
8
|
+
var _dom = require("@testing-library/dom");
|
|
9
|
+
|
|
10
|
+
var _click = require("./click");
|
|
11
|
+
|
|
12
|
+
var _focus = require("./focus");
|
|
13
|
+
|
|
14
|
+
var _hover = require("./hover");
|
|
15
|
+
|
|
16
|
+
function selectOptionsBase(newValue, select, values, init) {
|
|
17
|
+
if (!newValue && !select.multiple) {
|
|
18
|
+
throw (0, _dom.getConfig)().getElementError(`Unable to deselect an option in a non-multiple select. Use selectOptions to change the selection instead.`, select);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const valArray = Array.isArray(values) ? values : [values];
|
|
22
|
+
const allOptions = Array.from(select.querySelectorAll('option, [role="option"]'));
|
|
23
|
+
const selectedOptions = valArray.map(val => {
|
|
24
|
+
if (allOptions.includes(val)) {
|
|
25
|
+
return val;
|
|
26
|
+
} else {
|
|
27
|
+
const matchingOption = allOptions.find(o => o.value === val || o.innerHTML === val);
|
|
28
|
+
|
|
29
|
+
if (matchingOption) {
|
|
30
|
+
return matchingOption;
|
|
31
|
+
} else {
|
|
32
|
+
throw (0, _dom.getConfig)().getElementError(`Value "${val}" not found in options`, select);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}).filter(option => !option.disabled);
|
|
36
|
+
if (select.disabled || !selectedOptions.length) return;
|
|
37
|
+
|
|
38
|
+
if (select.multiple) {
|
|
39
|
+
for (const option of selectedOptions) {
|
|
40
|
+
// events fired for multiple select are weird. Can't use hover...
|
|
41
|
+
_dom.fireEvent.pointerOver(option, init);
|
|
42
|
+
|
|
43
|
+
_dom.fireEvent.pointerEnter(select, init);
|
|
44
|
+
|
|
45
|
+
_dom.fireEvent.mouseOver(option);
|
|
46
|
+
|
|
47
|
+
_dom.fireEvent.mouseEnter(select);
|
|
48
|
+
|
|
49
|
+
_dom.fireEvent.pointerMove(option, init);
|
|
50
|
+
|
|
51
|
+
_dom.fireEvent.mouseMove(option, init);
|
|
52
|
+
|
|
53
|
+
_dom.fireEvent.pointerDown(option, init);
|
|
54
|
+
|
|
55
|
+
_dom.fireEvent.mouseDown(option, init);
|
|
56
|
+
|
|
57
|
+
(0, _focus.focus)(select, init);
|
|
58
|
+
|
|
59
|
+
_dom.fireEvent.pointerUp(option, init);
|
|
60
|
+
|
|
61
|
+
_dom.fireEvent.mouseUp(option, init);
|
|
62
|
+
|
|
63
|
+
selectOption(option);
|
|
64
|
+
|
|
65
|
+
_dom.fireEvent.click(option, init);
|
|
66
|
+
}
|
|
67
|
+
} else if (selectedOptions.length === 1) {
|
|
68
|
+
(0, _click.click)(select, init);
|
|
69
|
+
selectOption(selectedOptions[0]);
|
|
70
|
+
} else {
|
|
71
|
+
throw (0, _dom.getConfig)().getElementError(`Cannot select multiple options on a non-multiple select`, select);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function selectOption(option) {
|
|
75
|
+
if (option.getAttribute('role') === 'option') {
|
|
76
|
+
option == null ? void 0 : option.setAttribute == null ? void 0 : option.setAttribute('aria-selected', newValue);
|
|
77
|
+
(0, _hover.hover)(option, init);
|
|
78
|
+
(0, _click.click)(option, init);
|
|
79
|
+
(0, _hover.unhover)(option, init);
|
|
80
|
+
} else {
|
|
81
|
+
option.selected = newValue;
|
|
82
|
+
(0, _dom.fireEvent)(select, (0, _dom.createEvent)('input', select, {
|
|
83
|
+
bubbles: true,
|
|
84
|
+
cancelable: false,
|
|
85
|
+
composed: true,
|
|
86
|
+
...init
|
|
87
|
+
}));
|
|
88
|
+
|
|
89
|
+
_dom.fireEvent.change(select, init);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const selectOptions = selectOptionsBase.bind(null, true);
|
|
95
|
+
exports.selectOptions = selectOptions;
|
|
96
|
+
const deselectOptions = selectOptionsBase.bind(null, false);
|
|
97
|
+
exports.deselectOptions = deselectOptions;
|
package/dist/tab.js
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.tab = tab;
|
|
7
|
+
|
|
8
|
+
var _dom = require("@testing-library/dom");
|
|
9
|
+
|
|
10
|
+
var _utils = require("./utils");
|
|
11
|
+
|
|
12
|
+
var _focus = require("./focus");
|
|
13
|
+
|
|
14
|
+
var _blur = require("./blur");
|
|
15
|
+
|
|
16
|
+
function getNextElement(currentIndex, shift, elements, focusTrap) {
|
|
17
|
+
if (focusTrap === document && currentIndex === 0 && shift) {
|
|
18
|
+
return document.body;
|
|
19
|
+
} else if (focusTrap === document && currentIndex === elements.length - 1 && !shift) {
|
|
20
|
+
return document.body;
|
|
21
|
+
} else {
|
|
22
|
+
const nextIndex = shift ? currentIndex - 1 : currentIndex + 1;
|
|
23
|
+
const defaultIndex = shift ? elements.length - 1 : 0;
|
|
24
|
+
return elements[nextIndex] || elements[defaultIndex];
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function tab({
|
|
29
|
+
shift = false,
|
|
30
|
+
focusTrap
|
|
31
|
+
} = {}) {
|
|
32
|
+
var _focusTrap$ownerDocum, _focusTrap;
|
|
33
|
+
|
|
34
|
+
const previousElement = (0, _utils.getActiveElement)((_focusTrap$ownerDocum = (_focusTrap = focusTrap) == null ? void 0 : _focusTrap.ownerDocument) != null ? _focusTrap$ownerDocum : document);
|
|
35
|
+
|
|
36
|
+
if (!focusTrap) {
|
|
37
|
+
focusTrap = document;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const focusableElements = focusTrap.querySelectorAll(_utils.FOCUSABLE_SELECTOR);
|
|
41
|
+
const enabledElements = [...focusableElements].filter(el => el === previousElement || el.getAttribute('tabindex') !== '-1' && !el.disabled);
|
|
42
|
+
if (enabledElements.length === 0) return;
|
|
43
|
+
const orderedElements = enabledElements.map((el, idx) => ({
|
|
44
|
+
el,
|
|
45
|
+
idx
|
|
46
|
+
})).sort((a, b) => {
|
|
47
|
+
// tabindex has no effect if the active element has tabindex="-1"
|
|
48
|
+
if (previousElement && previousElement.getAttribute('tabindex') === '-1') {
|
|
49
|
+
return a.idx - b.idx;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const tabIndexA = a.el.getAttribute('tabindex');
|
|
53
|
+
const tabIndexB = b.el.getAttribute('tabindex');
|
|
54
|
+
const diff = tabIndexA - tabIndexB;
|
|
55
|
+
return diff === 0 ? a.idx - b.idx : diff;
|
|
56
|
+
}).map(({
|
|
57
|
+
el
|
|
58
|
+
}) => el);
|
|
59
|
+
const checkedRadio = {};
|
|
60
|
+
let prunedElements = [];
|
|
61
|
+
orderedElements.forEach(el => {
|
|
62
|
+
// For radio groups keep only the active radio
|
|
63
|
+
// If there is no active radio, keep only the checked radio
|
|
64
|
+
// If there is no checked radio, treat like everything else
|
|
65
|
+
if (el.type === 'radio' && el.name) {
|
|
66
|
+
// If the active element is part of the group, add only that
|
|
67
|
+
if (previousElement && previousElement.type === el.type && previousElement.name === el.name) {
|
|
68
|
+
if (el === previousElement) {
|
|
69
|
+
prunedElements.push(el);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return;
|
|
73
|
+
} // If we stumble upon a checked radio, remove the others
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
if (el.checked) {
|
|
77
|
+
prunedElements = prunedElements.filter(e => e.type !== el.type || e.name !== el.name);
|
|
78
|
+
prunedElements.push(el);
|
|
79
|
+
checkedRadio[el.name] = el;
|
|
80
|
+
return;
|
|
81
|
+
} // If we already found the checked one, skip
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
if (checkedRadio[el.name]) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
prunedElements.push(el);
|
|
90
|
+
});
|
|
91
|
+
const index = prunedElements.findIndex(el => el === previousElement);
|
|
92
|
+
const nextElement = getNextElement(index, shift, prunedElements, focusTrap);
|
|
93
|
+
const shiftKeyInit = {
|
|
94
|
+
key: 'Shift',
|
|
95
|
+
keyCode: 16,
|
|
96
|
+
shiftKey: true
|
|
97
|
+
};
|
|
98
|
+
const tabKeyInit = {
|
|
99
|
+
key: 'Tab',
|
|
100
|
+
keyCode: 9,
|
|
101
|
+
shiftKey: shift
|
|
102
|
+
};
|
|
103
|
+
let continueToTab = true; // not sure how to make it so there's no previous element...
|
|
104
|
+
// istanbul ignore else
|
|
105
|
+
|
|
106
|
+
if (previousElement) {
|
|
107
|
+
// preventDefault on the shift key makes no difference
|
|
108
|
+
if (shift) _dom.fireEvent.keyDown(previousElement, { ...shiftKeyInit
|
|
109
|
+
});
|
|
110
|
+
continueToTab = _dom.fireEvent.keyDown(previousElement, { ...tabKeyInit
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const keyUpTarget = !continueToTab && previousElement ? previousElement : nextElement;
|
|
115
|
+
|
|
116
|
+
if (continueToTab) {
|
|
117
|
+
if (nextElement === document.body) {
|
|
118
|
+
(0, _blur.blur)(previousElement);
|
|
119
|
+
} else {
|
|
120
|
+
(0, _focus.focus)(nextElement);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
_dom.fireEvent.keyUp(keyUpTarget, { ...tabKeyInit
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
if (shift) {
|
|
128
|
+
_dom.fireEvent.keyUp(keyUpTarget, { ...shiftKeyInit,
|
|
129
|
+
shiftKey: false
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
/*
|
|
134
|
+
eslint
|
|
135
|
+
complexity: "off",
|
|
136
|
+
max-statements: "off",
|
|
137
|
+
*/
|