Skip to content

Commit 2708676

Browse files
committed
fix(userEvent): Exit early if options are disabled
1 parent d5aed71 commit 2708676

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

src/user-event/__tests__/helpers/utils.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,21 @@ function setup(ui, {eventHandlers} = {}) {
2424
return {element, ...addListeners(element, {eventHandlers})}
2525
}
2626

27-
function setupSelect({disabled = false, multiple = false} = {}) {
27+
function setupSelect({
28+
disabled = false,
29+
disabledOptions = false,
30+
multiple = false,
31+
} = {}) {
2832
const form = document.createElement('form')
2933
form.innerHTML = `
3034
<select
3135
name="select"
3236
${disabled ? 'disabled' : ''}
3337
${multiple ? 'multiple' : ''}
3438
>
35-
<option value="1">1</option>
36-
<option value="2">2</option>
37-
<option value="3">3</option>
39+
<option value="1" ${disabledOptions ? 'disabled' : ''}>1</option>
40+
<option value="2" ${disabledOptions ? 'disabled' : ''}>2</option>
41+
<option value="3" ${disabledOptions ? 'disabled' : ''}>3</option>
3842
</select>
3943
`
4044
document.body.append(form)

src/user-event/__tests__/select-options.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,17 @@ test('does not select anything if select is disabled', async () => {
119119
expect(o2.selected).toBe(false)
120120
expect(o3.selected).toBe(false)
121121
})
122+
123+
test('does not select anything if options are disabled', async () => {
124+
const {select, options, getEventSnapshot} = setupSelect({
125+
disabledOptions: true,
126+
})
127+
await userEvent.selectOptions(select, '2')
128+
expect(getEventSnapshot()).toMatchInlineSnapshot(
129+
`No events were fired on: select[name="select"][value=""]`,
130+
)
131+
const [o1, o2, o3] = options
132+
expect(o1.selected).toBe(false)
133+
expect(o2.selected).toBe(false)
134+
expect(o3.selected).toBe(false)
135+
})

src/user-event/select-options.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,25 @@ async function selectOptionsBase(newValue, select, values, init) {
1414
}
1515
const valArray = Array.isArray(values) ? values : [values]
1616
const allOptions = Array.from(select.querySelectorAll('option'))
17-
const selectedOptions = valArray.map(val => {
18-
if (allOptions.includes(val)) {
19-
return val
20-
} else {
21-
const matchingOption = allOptions.find(o => o.value === val)
22-
if (matchingOption) {
23-
return matchingOption
17+
const selectedOptions = valArray
18+
.map(val => {
19+
if (allOptions.includes(val)) {
20+
return val
2421
} else {
25-
throw getConfig().getElementError(
26-
`Value "${val}" not found in options`,
27-
select,
28-
)
22+
const matchingOption = allOptions.find(o => o.value === val)
23+
if (matchingOption) {
24+
return matchingOption
25+
} else {
26+
throw getConfig().getElementError(
27+
`Value "${val}" not found in options`,
28+
select,
29+
)
30+
}
2931
}
30-
}
31-
})
32+
})
33+
.filter(option => !option.disabled)
3234

33-
if (select.disabled) return
35+
if (select.disabled || !selectedOptions.length) return
3436

3537
if (select.multiple) {
3638
for (const option of selectedOptions) {

0 commit comments

Comments
 (0)