-
Notifications
You must be signed in to change notification settings - Fork 470
getByRole
does not support exact
option
#1157
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
Comments
Hi, AFAIK this is intended. |
Okay. It would look like the following : // A tuple : [name: string, config: TextMatch]
// PRO it avoids name collision with the TextMatch config object
// CON it is not very popular
getByRole('option', { name: ['idea 1', { exact: false }], selected: true });
// An object : TextMatch & { text: string }
// PRO devs are more familiar with this format
// CON is exposed to collision
getByRole('option', { name: { text: 'idea 1', exact: false }}, selected: true });
// An object : { text: string, config: TextMatch }
// PRO devs are more familiar with this format & it avoids collisions
// CON 3 nesting level starts to make a lot
getByRole('option', { name: { text: 'idea 1', config: { exact: false }}}, selected: true }); |
Would it make more sense to use a RegExp for the |
A RegExp is already supported. I think what @JesusTheHun is asking, is that |
Yes, |
But shouldn't a RegExp allow for the same behavior? Adding another API for the same thing would be problematic. |
Actually it's the same API but for something else. |
What cases could not be solved with a RegExp? |
const MyVG = () => (
<svg width="24" height="24" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M7.14645 7.14645C7.34171 6.95118 7.65829 6.95118 7.85355 7.14645L11.9978 11.2907L16.1421 7.14645C16.3373 6.95118 16.6539 6.95118 16.8492 7.14645C17.0445 7.34171 17.0445 7.65829 16.8492 7.85355L12.7049 11.9978L16.8492 16.1421C17.0445 16.3373 17.0445 16.6539 16.8492 16.8492C16.6539 17.0445 16.3373 17.0445 16.1421 16.8492L11.9978 12.7049L7.85355 16.8492C7.65829 17.0445 7.34171 17.0445 7.14645 16.8492C6.95118 16.6539 6.95118 16.3373 7.14645 16.1421L11.2907 11.9978L7.14645 7.85355C6.95118 7.65829 6.95118 7.34171 7.14645 7.14645Z" />
</svg>
)
const MyComponent = () => {
return <select>
<option><MyVG />A</option> {/* [object Object]A */}
<option><MyVG />B</option> {/* [object Object]B */}
<option><MyVG />C</option> {/* [object Object]C */}
</select>;
}
it("should match the option", async () => {
const { renderBare } = rtlKit();
renderBare(<MyComponent />);
expect(screen.getAllByRole('option', { name: /b/i })).toHaveLength(1);
}); |
Added a note about using `exact` option with *byRole queries to enable fuzzy matching. Please see details of the issue here: testing-library/dom-testing-library#1157 Users may expect to use `exact: false` for substring matching whereas it doesn't affect the way of filtering out DOM nodes by accessible names.
^^ I created a PR to add some clarification regarding the |
I'm not sure I'm following. Do you want to match the element in this case or not? Because IMO there's a bug in the code here and this element shouldn't be matched because |
@MatanBobi sorry, it is not very clear. |
Versions
@testing-library/jest-dom
v5.16.5
@testing-library/react
v13.3.0
The problem
Reproduction:
https://codesandbox.io/s/react-testing-library-repro-getbyrole-exact-false-5zzq2v
Problem description:
In the documentation we only see the use of regex when using
getByRole
and the use ofexact
when usinggetByText
. However the Typescript definition says theexact
option is available forgetByRole
.So it is either a type error or a runtime code error.
Suggested solution:
The text was updated successfully, but these errors were encountered: