Skip to content

Commit 3155890

Browse files
ben-dyernickserv
authored andcommitted
Exit early from selectOption if select is disabled
1 parent 52efb3b commit 3155890

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,19 @@ test('throws an error if multiple are passed but not a multiple select', async (
107107
const error = await userEvent.selectOptions(select, ['2', '3']).catch(e => e)
108108
expect(error.message).toMatch(/cannot select multiple/i)
109109
})
110+
111+
test('does not select anything if select is disabled', () => {
112+
const {
113+
container: {firstChild: select},
114+
} = render(
115+
<select disabled>
116+
<option>No value selected</option>
117+
<option value="1">1</option>
118+
<option value="2">2</option>
119+
<option value="3">3</option>
120+
</select>,
121+
)
122+
123+
userEvent.selectOptions(select, '1')
124+
expect(select.selectedIndex).toBe(0)
125+
})

src/user-event/select-options.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ async function selectOptionsBase(newValue, select, values, init) {
3030
}
3131
})
3232

33+
if (select.disabled) return
34+
3335
if (select.multiple) {
3436
for (const option of selectedOptions) {
3537
// events fired for multiple select are weird. Can't use hover...

0 commit comments

Comments
 (0)