@testing-library/dom version: "^7.2.2"@testing-library/react version: "^10.0.4"Node version: 13.7.0
test("fieldset accessible name", () => {
const { getByRole } = render(
<>
<fieldset>
<legend>My Legend</legend>
<label>
My Input
<input type="text" />
</label>
</fieldset>
</>
);
const fieldset = getByRole("group", { name: /my legend/i });
expect(within(fieldset).getByLabelText(/my input/i)).not.toBeNull();
});
Following the suggested guidance from Which query should I use FAQ
I am trying to get a fieldset element by its role and name.
Unable to find an accessible element with the role "group" and name `/fieldset/i`
Here are the accessible roles:
document:
Name "":
<body />
--------------------------------------------------
group:
Name "":
<fieldset />
--------------------------------------------------
textbox:
Name "My Input":
<input
type="text"
/>
--------------------------------------------------
<body>
<div>
<fieldset>
<legend>
Legend
</legend>
<label>
My Input
<input
type="text"
/>
</label>
</fieldset>
</div>
</body>
https://codesandbox.io/s/fieldset-accessible-name-69lhg?file=/src/__tests__/App.test.js
--------------------------------------------------
group:
Name "":
<fieldset />
--------------------------------------------------
Consider assigning legend element as accessible name for a fieldset.
The fix is likely similar to #570. Instead of the <caption> for a <table> we should use the <legend> for a <fieldset>.
:tada: This issue has been resolved in version 7.5.7 :tada:
The release is available on:
npm package (@latest dist-tag)Your semantic-release bot :package::rocket:
For future viewers the <legend> needs to be the first child of the <fieldset> per MDN.
A聽fieldset聽whose first child is this聽legend聽element