Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/allow selecting options in optgroups #196

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
node_modules
coverage/
dist/
.idea/
30 changes: 30 additions & 0 deletions __tests__/react/selectoptions.js
Expand Up @@ -249,4 +249,34 @@ describe("userEvent.selectOptions", () => {
expect(getByTestId("val2").selected).toBe(true);
expect(getByTestId("val3").selected).toBe(false);
});

it("sets the selected prop on the selected OPTION using OPTGROUPS", () => {
const { getByTestId } = render(
<form>
<select multiple data-testid="element">
<optgroup label="test optgroup 1">
<option data-testid="val1" value="1">
1
</option>
</optgroup>
<optgroup label="test optgroup 2">
<option data-testid="val2" value="2">
2
</option>
</optgroup>
<optgroup label="test optgroup 1">
<option data-testid="val3" value="3">
3
</option>
</optgroup>
</select>
</form>
);

userEvent.selectOptions(getByTestId("element"), ["1", "3"]);

expect(getByTestId("val1").selected).toBe(true);
expect(getByTestId("val2").selected).toBe(false);
expect(getByTestId("val3").selected).toBe(true);
});
});
25 changes: 25 additions & 0 deletions __tests__/vue/selectoptions.js
Expand Up @@ -226,4 +226,29 @@ describe("userEvent.selectOptions", () => {
expect(getByTestId("val2").selected).toBe(true);
expect(getByTestId("val3").selected).toBe(false);
});

it("sets the selected prop on the selected OPTION using OPTGROUPS", () => {
const { getByTestId } = render({
template: `
<form>
<select data-testid="element" multiple>
<optgroup label="test optgroup 1">
<option value="1" data-testid="val1">1</option>
</optgroup>
<optgroup label="test optgroup 2">
<option value="2" data-testid="val2">2</option>
</optgroup>
<optgroup label="test optgroup 3">
<option value="3" data-testid="val3">3</option>
</optgroup>
</select>
</form>`
});

userEvent.selectOptions(getByTestId("element"), ["1", "3"]);

expect(getByTestId("val1").selected).toBe(true);
expect(getByTestId("val2").selected).toBe(false);
expect(getByTestId("val3").selected).toBe(true);
});
});
4 changes: 2 additions & 2 deletions src/index.js
Expand Up @@ -160,8 +160,8 @@ const userEvent = {
clickElement(element);

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

if (selectedOptions.length > 0) {
Expand Down