Skip to content

Commit 8ba1b15

Browse files
fix: πŸ›οΈ select options even if they are nested in optgroups (#196)
* chore: πŸ€–οΈ Add .idea/ to .gitignore * fix: πŸ›οΈ select options even if they are nested in optgroups
1 parent ab40343 commit 8ba1b15

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

β€Ž.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
coverage/
33
dist/
4+
.idea/

β€Ž__tests__/react/selectoptions.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,4 +249,34 @@ describe("userEvent.selectOptions", () => {
249249
expect(getByTestId("val2").selected).toBe(true);
250250
expect(getByTestId("val3").selected).toBe(false);
251251
});
252+
253+
it("sets the selected prop on the selected OPTION using OPTGROUPS", () => {
254+
const { getByTestId } = render(
255+
<form>
256+
<select multiple data-testid="element">
257+
<optgroup label="test optgroup 1">
258+
<option data-testid="val1" value="1">
259+
1
260+
</option>
261+
</optgroup>
262+
<optgroup label="test optgroup 2">
263+
<option data-testid="val2" value="2">
264+
2
265+
</option>
266+
</optgroup>
267+
<optgroup label="test optgroup 1">
268+
<option data-testid="val3" value="3">
269+
3
270+
</option>
271+
</optgroup>
272+
</select>
273+
</form>
274+
);
275+
276+
userEvent.selectOptions(getByTestId("element"), ["1", "3"]);
277+
278+
expect(getByTestId("val1").selected).toBe(true);
279+
expect(getByTestId("val2").selected).toBe(false);
280+
expect(getByTestId("val3").selected).toBe(true);
281+
});
252282
});

β€Ž__tests__/vue/selectoptions.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,4 +226,29 @@ describe("userEvent.selectOptions", () => {
226226
expect(getByTestId("val2").selected).toBe(true);
227227
expect(getByTestId("val3").selected).toBe(false);
228228
});
229+
230+
it("sets the selected prop on the selected OPTION using OPTGROUPS", () => {
231+
const { getByTestId } = render({
232+
template: `
233+
<form>
234+
<select data-testid="element" multiple>
235+
<optgroup label="test optgroup 1">
236+
<option value="1" data-testid="val1">1</option>
237+
</optgroup>
238+
<optgroup label="test optgroup 2">
239+
<option value="2" data-testid="val2">2</option>
240+
</optgroup>
241+
<optgroup label="test optgroup 3">
242+
<option value="3" data-testid="val3">3</option>
243+
</optgroup>
244+
</select>
245+
</form>`
246+
});
247+
248+
userEvent.selectOptions(getByTestId("element"), ["1", "3"]);
249+
250+
expect(getByTestId("val1").selected).toBe(true);
251+
expect(getByTestId("val2").selected).toBe(false);
252+
expect(getByTestId("val3").selected).toBe(true);
253+
});
229254
});

β€Žsrc/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ const userEvent = {
169169
clickElement(element);
170170

171171
const valArray = Array.isArray(values) ? values : [values];
172-
const selectedOptions = Array.from(element.children).filter(
173-
opt => opt.tagName === "OPTION" && valArray.includes(opt.value)
172+
const selectedOptions = Array.from(element.querySelectorAll('option')).filter(
173+
opt => valArray.includes(opt.value)
174174
);
175175

176176
if (selectedOptions.length > 0) {

0 commit comments

Comments
Β (0)