dom-testing-library:
dom-testing-libraryversion: 3.8.2reactversion: N/Anodeversion: v10.11.0 (but it doesn’t matter)npm(oryarn) version:yarn 1.10.1(but it doesn’t matter)
Relevant code or config:
See the reproduction section below.
What you did:
- Created a
<div>containing a<select>with two<option>s. - Fetched the
<select>usinggetBySelectText - Fired a
changeevent on the fetched<select>to select another valid option - Verified that the value was changed
- Attempted to fetch the
<select>from the<div>again using the newly selected option’s label (child element)
What happened:
The <select> cannot be fetched by its new “select text”:
FAIL ./index.test.js
✕ should respond to changes (4520ms)
● should respond to changes
Unable to find a <select> element with the selected option's text: Option 2
<div>
<select>
<option
value="option-1"
>
Option 1
</option>
<option
value="option-2"
>
Option 2
</option>
</select>
</div>
22 | // but this times out because the text hasn't been updated.
23 | // No changes are reflected in the DOM, which is probably why…
> 24 | return waitForElement(() => getBySelectText(container, 'Option 2'));
| ^
25 | });
26 |
at getElementError (node_modules/dom-testing-library/dist/query-helpers.js:29:10)
at getAllBySelectText (node_modules/dom-testing-library/dist/queries.js:375:45)
at firstResultOrNull (node_modules/dom-testing-library/dist/query-helpers.js:37:30)
at getBySelectText (node_modules/dom-testing-library/dist/queries.js:385:42)
at getBySelectText (index.test.js:24:33)
at onMutation (node_modules/dom-testing-library/dist/wait-for-element.js:48:22)
at node_modules/dom-testing-library/dist/wait-for-element.js:66:7
at waitForElement (node_modules/dom-testing-library/dist/wait-for-element.js:26:10)
at Object.waitForElement (index.test.js:24:12)
Reproduction:
const { getBySelectText, fireEvent, wait, waitForElement } = require('dom-testing-library');
it('should respond to changes', () => {
const select = document.createElement('select');
select.innerHTML = `
<option value="option-1">Option 1</option>
<option value="option-2">Option 2</option>
`;
const container = document.createElement('div');
container.appendChild(select);
const foundSelectBeforeEvent = getBySelectText(container, 'Option 1');
expect(foundSelectBeforeEvent.value).toBe('option-1');
fireEvent.change(foundSelectBeforeEvent, { target: { value: 'option-2' }});
// It has been updated, and this passes
expect(foundSelectBeforeEvent.value).toBe('option-2');
// I would then expect to be able to get the <select> again by the new text,
// but this times out because the text hasn't been updated.
// No changes are reflected in the DOM, which is probably why…
return waitForElement(() => getBySelectText(container, 'Option 2'));
});
I’ve tried (among other things):
waiting for a tick to let updates happen- Firing events for
focusandblurbefore and after firing thechangeto force a rerender - Not wrapping the fetching in
waitForElement(it just fails in the same way, but faster)
Problem description:
The <select> element doesn’t seem to update its displayed value, so it behaves differently from what a user experiences in a browser.
Suggested solution:
The <select> should be selectable using the label of the selected option. I don’t know how to fix it, though 😄
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 18 (12 by maintainers)
With help from @SimenB I put together a version of jest-environment-jsdom that ships JSDOM version 12:
GitHub: https://github.com/theneva/jest-environment-jsdom-twelve npm: https://www.npmjs.com/package/jest-environment-jsdom-twelve
Using that solves my issue 😄
Thanks for looking into this!
Hmmmm… What if we get the best of all worlds. If the select hasmultiplethen let’s useselectedOptions(hopefully they don’t need it to be reactive), if it’s notmultiplethen we can useselectedIndex.Is there any way we could iterate through the…Yeah, we can do this:
I can reproduce the same error in a Jest/JSDOM environment
🎉 This issue has been resolved in version 3.10.1 🎉
The release is available on:
npm package (@latest dist-tag)Your semantic-release bot 📦🚀
Though
HTMLSelectElement.selectedOptionsis not reactive in the version of JSDOM bundled with Jest,HTMLSelectElement.selectedIndexis.I wouldn’t mind opening a PR to change it to
selectedIndex, but that would drop compatibility with themultipleattribute.@kentcdodds What would you prefer?