/
selectOptions.ts
156 lines (141 loc) Β· 4.59 KB
/
selectOptions.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import {getConfig} from '@testing-library/dom'
import {
focus,
hasPointerEvents,
isDisabled,
isElementType,
wait,
} from '../utils'
import {Config, Instance} from '../setup'
export async function selectOptions(
this: Instance,
select: Element,
values: HTMLElement | HTMLElement[] | string[] | string,
) {
return selectOptionsBase.call(this, true, select, values)
}
export async function deselectOptions(
this: Instance,
select: Element,
values: HTMLElement | HTMLElement[] | string[] | string,
) {
return selectOptionsBase.call(this, false, select, values)
}
async function selectOptionsBase(
this: Instance,
newValue: boolean,
select: Element,
values: HTMLElement | HTMLElement[] | string[] | string,
) {
if (!newValue && !(select as HTMLSelectElement).multiple) {
throw getConfig().getElementError(
`Unable to deselect an option in a non-multiple select. Use selectOptions to change the selection instead.`,
select,
)
}
const valArray = Array.isArray(values) ? values : [values]
const allOptions = Array.from(
select.querySelectorAll('option, [role="option"]'),
)
const selectedOptions = valArray
.map(val => {
if (typeof val !== 'string' && allOptions.includes(val)) {
return val
} else {
const matchingOption = allOptions.find(
o =>
(o as HTMLInputElement | HTMLTextAreaElement).value === val ||
o.innerHTML === val,
)
if (matchingOption) {
return matchingOption
} else {
throw getConfig().getElementError(
`Value "${String(val)}" not found in options`,
select,
)
}
}
})
.filter(option => !isDisabled(option))
if (isDisabled(select) || !selectedOptions.length) return
const selectOption = (option: HTMLOptionElement) => {
option.selected = newValue
this.dispatchUIEvent(select, 'input', {
bubbles: true,
cancelable: false,
composed: true,
})
this.dispatchUIEvent(select, 'change')
}
if (isElementType(select, 'select')) {
if (select.multiple) {
for (const option of selectedOptions) {
const withPointerEvents =
this[Config].pointerEventsCheck === 0
? true
: hasPointerEvents(this[Config], option)
// events fired for multiple select are weird. Can't use hover...
if (withPointerEvents) {
this.dispatchUIEvent(option, 'pointerover')
this.dispatchUIEvent(select, 'pointerenter')
this.dispatchUIEvent(option, 'mouseover')
this.dispatchUIEvent(select, 'mouseenter')
this.dispatchUIEvent(option, 'pointermove')
this.dispatchUIEvent(option, 'mousemove')
this.dispatchUIEvent(option, 'pointerdown')
this.dispatchUIEvent(option, 'mousedown')
}
focus(select)
if (withPointerEvents) {
this.dispatchUIEvent(option, 'pointerup')
this.dispatchUIEvent(option, 'mouseup')
}
selectOption(option as HTMLOptionElement)
if (withPointerEvents) {
this.dispatchUIEvent(option, 'click')
}
await wait(this[Config])
}
} else if (selectedOptions.length === 1) {
const withPointerEvents =
this[Config].pointerEventsCheck === 0
? true
: hasPointerEvents(this[Config], select)
// the click to open the select options
if (withPointerEvents) {
await this.click(select)
} else {
focus(select)
}
selectOption(selectedOptions[0] as HTMLOptionElement)
if (withPointerEvents) {
// the browser triggers another click event on the select for the click on the option
// this second click has no 'down' phase
this.dispatchUIEvent(select, 'pointerover')
this.dispatchUIEvent(select, 'pointerenter')
this.dispatchUIEvent(select, 'mouseover')
this.dispatchUIEvent(select, 'mouseenter')
this.dispatchUIEvent(select, 'pointerup')
this.dispatchUIEvent(select, 'mouseup')
this.dispatchUIEvent(select, 'click')
}
await wait(this[Config])
} else {
throw getConfig().getElementError(
`Cannot select multiple options on a non-multiple select`,
select,
)
}
} else if (select.getAttribute('role') === 'listbox') {
for (const option of selectedOptions) {
await this.click(option)
await this.unhover(option)
}
} else {
throw getConfig().getElementError(
`Cannot select options on elements that are neither select nor listbox elements`,
select,
)
}
}