|
1 | 1 | import {createEvent, getConfig, fireEvent} from '@testing-library/dom' |
2 | | -import {isDisabled, isElementType} from './utils' |
| 2 | +import {hasPointerEvents, isDisabled, isElementType} from './utils' |
3 | 3 | import {click} from './click' |
4 | 4 | import {focus} from './focus' |
5 | 5 | import {hover, unhover} from './hover' |
@@ -47,36 +47,55 @@ function selectOptionsBase( |
47 | 47 | if (isElementType(select, 'select')) { |
48 | 48 | if (select.multiple) { |
49 | 49 | for (const option of selectedOptions) { |
| 50 | + const withPointerEvents = hasPointerEvents(option) |
| 51 | + |
50 | 52 | // events fired for multiple select are weird. Can't use hover... |
51 | | - fireEvent.pointerOver(option, init) |
52 | | - fireEvent.pointerEnter(select, init) |
53 | | - fireEvent.mouseOver(option) |
54 | | - fireEvent.mouseEnter(select) |
55 | | - fireEvent.pointerMove(option, init) |
56 | | - fireEvent.mouseMove(option, init) |
57 | | - fireEvent.pointerDown(option, init) |
58 | | - fireEvent.mouseDown(option, init) |
| 53 | + if (withPointerEvents) { |
| 54 | + fireEvent.pointerOver(option, init) |
| 55 | + fireEvent.pointerEnter(select, init) |
| 56 | + fireEvent.mouseOver(option) |
| 57 | + fireEvent.mouseEnter(select) |
| 58 | + fireEvent.pointerMove(option, init) |
| 59 | + fireEvent.mouseMove(option, init) |
| 60 | + fireEvent.pointerDown(option, init) |
| 61 | + fireEvent.mouseDown(option, init) |
| 62 | + } |
| 63 | + |
59 | 64 | focus(select) |
60 | | - fireEvent.pointerUp(option, init) |
61 | | - fireEvent.mouseUp(option, init) |
| 65 | + |
| 66 | + if (withPointerEvents) { |
| 67 | + fireEvent.pointerUp(option, init) |
| 68 | + fireEvent.mouseUp(option, init) |
| 69 | + } |
| 70 | + |
62 | 71 | selectOption(option as HTMLOptionElement) |
63 | | - fireEvent.click(option, init) |
| 72 | + |
| 73 | + if (withPointerEvents) { |
| 74 | + fireEvent.click(option, init) |
| 75 | + } |
64 | 76 | } |
65 | 77 | } else if (selectedOptions.length === 1) { |
| 78 | + const withPointerEvents = hasPointerEvents(select) |
66 | 79 | // the click to open the select options |
67 | | - click(select, init) |
| 80 | + if (withPointerEvents) { |
| 81 | + click(select, init) |
| 82 | + } else { |
| 83 | + focus(select) |
| 84 | + } |
68 | 85 |
|
69 | 86 | selectOption(selectedOptions[0] as HTMLOptionElement) |
70 | 87 |
|
71 | | - // the browser triggers another click event on the select for the click on the option |
72 | | - // this second click has no 'down' phase |
73 | | - fireEvent.pointerOver(select, init) |
74 | | - fireEvent.pointerEnter(select, init) |
75 | | - fireEvent.mouseOver(select) |
76 | | - fireEvent.mouseEnter(select) |
77 | | - fireEvent.pointerUp(select, init) |
78 | | - fireEvent.mouseUp(select, init) |
79 | | - fireEvent.click(select, init) |
| 88 | + if (withPointerEvents) { |
| 89 | + // the browser triggers another click event on the select for the click on the option |
| 90 | + // this second click has no 'down' phase |
| 91 | + fireEvent.pointerOver(select, init) |
| 92 | + fireEvent.pointerEnter(select, init) |
| 93 | + fireEvent.mouseOver(select) |
| 94 | + fireEvent.mouseEnter(select) |
| 95 | + fireEvent.pointerUp(select, init) |
| 96 | + fireEvent.mouseUp(select, init) |
| 97 | + fireEvent.click(select, init) |
| 98 | + } |
80 | 99 | } else { |
81 | 100 | throw getConfig().getElementError( |
82 | 101 | `Cannot select multiple options on a non-multiple select`, |
|
0 commit comments