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

[docs] add examples of testing with mui with react-testing-library #44670

Open
Demianeen opened this issue Dec 6, 2024 · 1 comment
Open
Assignees
Labels
docs Improvements or additions to the documentation

Comments

@Demianeen
Copy link

Demianeen commented Dec 6, 2024

Related page

https://mui.com/material-ui/guides/testing/

Kind of issue

Unclear explanations

Issue description

I already spend a day trying to figure out what is a good way to write tests with mui. From docs:

For example, when rendering a TextField your test should not need to query for the specific Material UI instance of the TextField but rather for the input, or [role="textbox"].

Okay, I should query for role=textbox, but I am not sure how I would test some mui components without data-testid. For example, password and confirm password. I can't really get first input without exact specifying exact label. Should I do it? Or should I use data-testid then? I understand that mui is not about testing, but still it can save so much time for those that are developing with mui if there was at least one example for reference.

Context

I have a form with different submit schemas and it relatively big. It consists of 6 steps (sign up, email confirmation, 4 other steps for company creation specific to our business). Just sign-up step with firebase is around 300 lines of code, so I am trying to write tests for it to ensure I wouldn't break anything in the future.

For form itself we use, zod with react-hook-form + firebase for user authentication. We also use next and next-intl for internalization

Search keywords: testing mui jest vitest react-testing-library rtl @testing-library/react

@Demianeen Demianeen added status: waiting for maintainer These issues haven't been looked at yet by a maintainer support: docs-feedback Feedback from documentation page labels Dec 6, 2024
@mj12albert
Copy link
Member

Okay, I should query for role=textbox, but I am not sure how I would test some mui components without data-testid. For example, password and confirm password. I can't really get first input without exact specifying exact label. Should I do it? Or should I use data-testid then?

@Demianeen I agree that maybe our page on testing could have a bit more practical info, in the mean time I think this part of the RTL docs answers your question: https://testing-library.com/docs/queries/about#priority

@mj12albert mj12albert added docs Improvements or additions to the documentation and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer support: docs-feedback Feedback from documentation page labels Dec 6, 2024
@mj12albert mj12albert self-assigned this Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Improvements or additions to the documentation
Projects
None yet
Development

No branches or pull requests

2 participants