Closed shadaxv closed 1 year ago
Hi @shadaxv, thanks for opening this one.
The reason for this is that the name
attribute isn't considered the component's accessible name (which is what we're querying for).. In the case of an input, the accessible name is defined by an aria-label
or aria-labelledyby
.
To solve that issue, I'd replace the name
attribute with an aria-label
and then the query will work.
Since this isn't an issue with testing-library, I'm closing this one :) Thanks again.
Hi @MatanBobi, thank you for your quick reply, I was just about to close the issue because I already found this thread - https://github.com/testing-library/dom-testing-library/issues/567
I think it is simply caused by the fact that the input with the password type, does not have its role, aria-label also will not work in this case, but thank you very much for the suggestion
For anyone who stumbles across this thread: instead of getByRole
, you can use getByLabelText
, which will be a good alternative
Wasn't familiar with that one.. Thanks!
Hi @MatanBobi, thank you for your quick reply, I was just about to close the issue because I already found this thread - #567
I think it is simply caused by the fact that the input with the password type, does not have its role, aria-label also will not work in this case, but thank you very much for the suggestion
For anyone who stumbles across this thread: instead of
getByRole
, you can usegetByLabelText
, which will be a good alternative
hello i used getByLabelText but i had to remove 'toBeInTheDocument()' for it to work can you help me to understand why?
here is the code:
describe('add new sub:', () => {
test('Should render without crash', async () => {
render(<ItineraryDialog /> );
const inputField = screen.getByLabelText('destinatio');
expect(inputField);
});
});
@urbain32 - We'll need more data to try and help you with that issue.
Hello guys! I want to test a fail scenario.
I know the cardHeadingElement element is not found, but I want to show result with expect function, But the result is:
Unable to find an accessible element with the role "heading" and name /shazam/i
` render(
const cardHeadingElement = screen.getByRole('heading', {
name: /shazam/i
})
expect(cardHeadingElement).not.toBeInTheDocument()`
Please help :)
@PeymanOruji You can follow the guides here: https://testing-library.com/docs/guide-disappearance#asserting-elements-are-not-present
Thank you sir. Can i ask another question? How we can test valid date ? :)
On Sun, Aug 6, 2023, 11:21 Matan Borenkraout @.***> wrote:
@PeymanOruji https://github.com/PeymanOruji You can follow the guides here:
https://testing-library.com/docs/guide-disappearance#asserting-elements-are-not-present
— Reply to this email directly, view it on GitHub https://github.com/testing-library/dom-testing-library/issues/1173#issuecomment-1666745750, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMN35H5OTCGTQZEMNSF6WALXT45HVANCNFSM6AAAAAAQVRP4OA . You are receiving this because you were mentioned.Message ID: @.***>
I can't really understand what you mean with this question and this issue is aimed to provide answer for something else. If you have any questions about RTL, please feel free to open a stackoverflow question or reach out on our discord server and we'd be happy to help :)
@testing-library/dom
version: 8.17.1@testing-library/react@13.3.0
+@testing-library/jest-dom@5.16.5
+jest@27.5.1
jest-environment-jsdom@27.5.1
Relevant code or config:
Tested component
Test
What you did:
I want to test a component by using getByRole query, which uses another component underneath
What happened:
Query getByRole does not find an accessible element, however, any other query can find the element (e.g. getByTestId, getByText)
Error output
Reproduction:
https://codesandbox.io/s/react-testing-library-demo-forked-v3dz08
Problem description:
Testing library is unable to find an accessible element in nested react components
Suggested solution:
Testing library should find an accessible element in a nested structure