Skip to content

Commit 41f71b5

Browse files
author
Doug Bacelar
authored
feat: allow selecting options by nodes (testing-library#297)
1 parent 6239c3a commit 41f71b5

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,15 @@ test('selectOptions', () => {
272272
The `values` parameter can be either an array of values or a singular scalar
273273
value.
274274

275+
It also accepts option nodes:
276+
277+
```js
278+
userEvent.selectOptions(screen.getByTestId('select-multiple'), [
279+
screen.getByText('A'),
280+
screen.getByText('B'),
281+
])
282+
```
283+
275284
### `tab({shift, focusTrap})`
276285

277286
Fires a tab event changing the document.activeElement in the same way the

src/__tests__/selectoptions.js

+27
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,33 @@ test('sets the selected prop on the selected OPTION', () => {
168168
expect(screen.getByTestId('val3').selected).toBe(true)
169169
})
170170

171+
test('sets the selected prop on the selected OPTION using nodes', () => {
172+
render(
173+
<form onSubmit={() => {}}>
174+
<select multiple data-testid="element">
175+
<option data-testid="val1" value="1">
176+
first option
177+
</option>
178+
<option data-testid="val2" value="2">
179+
second option
180+
</option>
181+
<option data-testid="val3" value="3">
182+
third option
183+
</option>
184+
</select>
185+
</form>,
186+
)
187+
188+
userEvent.selectOptions(screen.getByTestId('element'), [
189+
screen.getByText('second option'),
190+
screen.getByText('third option'),
191+
])
192+
193+
expect(screen.getByTestId('val1').selected).toBe(false)
194+
expect(screen.getByTestId('val2').selected).toBe(true)
195+
expect(screen.getByTestId('val3').selected).toBe(true)
196+
})
197+
171198
test('sets the selected prop on the selected OPTION using htmlFor', () => {
172199
const onSubmit = jest.fn()
173200

src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ function selectOptions(element, values, init) {
270270
const valArray = Array.isArray(values) ? values : [values]
271271
const selectedOptions = Array.from(
272272
element.querySelectorAll('option'),
273-
).filter(opt => valArray.includes(opt.value))
273+
).filter(opt => valArray.includes(opt.value) || valArray.includes(opt))
274274

275275
if (selectedOptions.length > 0) {
276276
if (element.multiple) {

typings/index.d.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ declare const userEvent: {
2222
clear: (element: TargetElement) => void
2323
click: (element: TargetElement, init?: MouseEventInit) => void
2424
dblClick: (element: TargetElement, init?: MouseEventInit) => void
25-
selectOptions: (element: TargetElement, values: string | string[], init?: MouseEventInit) => void
25+
selectOptions: (
26+
element: TargetElement,
27+
values: string | string[] | HTMLElement | HTMLElement[],
28+
init?: MouseEventInit,
29+
) => void
2630
upload: (
2731
element: TargetElement,
2832
files: FilesArgument,

0 commit comments

Comments
 (0)