@testing-library/user-event 12.2.1 → 12.5.0

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 CHANGED
@@ -89,11 +89,16 @@ import userEvent from '@testing-library/user-event'
89
89
 
90
90
  // or
91
91
 
92
- var userEvent = require('@testing-library/user-event')
92
+ const {default: userEvent} = require('@testing-library/user-event')
93
93
  ```
94
94
 
95
95
  ## API
96
96
 
97
+ Note: All userEvent methods are synchronous with one exception: when `delay`
98
+ with `userEvent.type` as described below). We also discourage using `userEvent`
99
+ inside `before/after` blocks at all, for important reasons described in
100
+ ["Avoid Nesting When You're Testing"](https://kentcdodds.com/blog/avoid-nesting-when-youre-testing).
101
+
97
102
  ### `click(element, eventInit, options)`
98
103
 
99
104
  Clicks `element`, depending on what `element` is it can have different side
@@ -183,20 +188,23 @@ to `await`!
183
188
 
184
189
  The following special character strings are supported:
185
190
 
186
- | Text string | Key | Modifier | Notes |
187
- | -------------- | ---------- | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
188
- | `{enter}` | Enter | N/A | Will insert a newline character (`<textarea />` only). |
189
- | `{space}` | `' '` | N/A | |
190
- | `{esc}` | Escape | N/A | |
191
- | `{backspace}` | Backspace | N/A | Will delete the previous character (or the characters within the `selectedRange`, see example below). |
192
- | `{del}` | Delete | N/A | Will delete the next character (or the characters within the `selectedRange`, see example below) |
193
- | `{selectall}` | N/A | N/A | Selects all the text of the element. Note that this will only work for elements that support selection ranges (so, not `email`, `password`, `number`, among others) |
194
- | `{arrowleft}` | ArrowLeft | N/A | |
195
- | `{arrowright}` | ArrowRight | N/A | |
196
- | `{shift}` | Shift | `shiftKey` | Does **not** capitalize following characters. |
197
- | `{ctrl}` | Control | `ctrlKey` | |
198
- | `{alt}` | Alt | `altKey` | |
199
- | `{meta}` | OS | `metaKey` | |
191
+ | Text string | Key | Modifier | Notes |
192
+ | -------------- | ---------- | ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
193
+ | `{enter}` | Enter | N/A | Will insert a newline character (`<textarea />` only). |
194
+ | `{space}` | `' '` | N/A | |
195
+ | `{esc}` | Escape | N/A | |
196
+ | `{backspace}` | Backspace | N/A | Will delete the previous character (or the characters within the `selectedRange`, see example below). |
197
+ | `{del}` | Delete | N/A | Will delete the next character (or the characters within the `selectedRange`, see example below) |
198
+ | `{selectall}` | N/A | N/A | Selects all the text of the element. Note that this will only work for elements that support selection ranges (so, not `email`, `password`, `number`, among others) |
199
+ | `{arrowleft}` | ArrowLeft | N/A | |
200
+ | `{arrowright}` | ArrowRight | N/A | |
201
+ | `{arrowup}` | ArrowUp | N/A | |
202
+ | `{arrowdown}` | ArrowDown | N/A | |
203
+ | `{shift}` | Shift | `shiftKey` | Does **not** capitalize following characters. |
204
+ | `{ctrl}` | Control | `ctrlKey` | |
205
+ | `{alt}` | Alt | `altKey` | |
206
+ | `{meta}` | OS | `metaKey` | |
207
+ | `{capslock}` | CapsLock | `modifierCapsLock` | Fires both keydown and keyup when used (simulates a user clicking their "Caps Lock" button to enable caps lock). |
200
208
 
201
209
  > **A note about modifiers:** Modifier keys (`{shift}`, `{ctrl}`, `{alt}`,
202
210
  > `{meta}`) will activate their corresponding event modifiers for the duration
@@ -234,6 +242,32 @@ test('delete characters within the selectedRange', () => {
234
242
  })
235
243
  ```
236
244
 
245
+ #### <input type="time" /> support
246
+
247
+ The following is an example of usage of this library with
248
+ `<input type="time" />`
249
+
250
+ ```jsx
251
+ import React from 'react
252
+ import {render, screen} from '@testing-library/react'
253
+ import userEvent from '@testing-library/user-event'
254
+
255
+ test('types into the input', () => {
256
+ render(
257
+ <>
258
+ <label for="time">Enter a time</label>
259
+ <input
260
+ type="time"
261
+ id="time"
262
+ />
263
+ </>
264
+ )
265
+ const input = screen.getByLabelText(/enter a time/i)
266
+ userEvent.type(input, '13:58')
267
+ expect(input.value).toBe('13:58')
268
+ })
269
+ ```
270
+
237
271
  ### `upload(element, file, [{ clickInit, changeInit }])`
238
272
 
239
273
  Uploads file to an `<input>`. For uploading multiple files use `<input>` with
@@ -596,6 +630,10 @@ Thanks goes to these people ([emoji key][emojis]):
596
630
  <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
631
  <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
632
  <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>
633
+ <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>
634
+ <td align="center"><a href="https://github.com/amandapouget"><img src="https://avatars3.githubusercontent.com/u/12855692?v=4" width="100px;" alt=""/><br /><sub><b>Amanda Pouget</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=amandapouget" title="Documentation">📖</a></td>
635
+ <td align="center"><a href="https://github.com/Sonic12040"><img src="https://avatars3.githubusercontent.com/u/21055893?v=4" width="100px;" alt=""/><br /><sub><b>Sonic12040</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=Sonic12040" title="Code">💻</a> <a href="https://github.com/testing-library/user-event/commits?author=Sonic12040" title="Tests">⚠️</a> <a href="https://github.com/testing-library/user-event/commits?author=Sonic12040" title="Documentation">📖</a></td>
636
+ <td align="center"><a href="https://github.com/gndelia"><img src="https://avatars1.githubusercontent.com/u/352474?v=4" width="100px;" alt=""/><br /><sub><b>Gonzalo D'Elia</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=gndelia" title="Code">💻</a> <a href="https://github.com/testing-library/user-event/commits?author=gndelia" title="Tests">⚠️</a> <a href="https://github.com/testing-library/user-event/commits?author=gndelia" title="Documentation">📖</a></td>
599
637
  </tr>
600
638
  </table>
601
639
 
@@ -614,7 +652,7 @@ MIT
614
652
  <!-- prettier-ignore-start -->
615
653
  [npm]: https://www.npmjs.com
616
654
  [node]: https://nodejs.org
617
- [build-badge]: https://img.shields.io/github/workflow/status/testing-library/user-event/validate/main?logo=github&style=flat-square
655
+ [build-badge]: https://img.shields.io/github/workflow/status/testing-library/user-event/validate/master?logo=github&style=flat-square
618
656
  [build]: https://github.com/testing-library/user-event/actions?query=workflow%3Avalidate
619
657
  [coverage-badge]: https://img.shields.io/codecov/c/github/testing-library/user-event.svg?style=flat-square
620
658
  [coverage]: https://codecov.io/github/testing-library/user-event
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 && 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;