-
Notifications
You must be signed in to change notification settings - Fork 10
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
Add section for Testing User Actions #199
Conversation
|
||
Here's a simple example using Jest and React Testing Library to test a button click: | ||
|
||
```jsx |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we cover some more tricky examples. For example, try out the 'should render toggle show more/less button' (yes, the name is weird) test in CountryFilterGroup.test.tsx in the Messe repository.
`
it.only("should render toggle show more/less button", async () => {
let showLessButton = screen.queryByRole("button", { name: /showless/i });
expect(showLessButton).toBeNull();
let showMoreButton = screen.queryByRole("button", { name: /showmore/i });
await userEvent.click(showMoreButton as HTMLButtonElement);
showMoreButton = screen.queryByRole('button', { name: /showmore/i });
expect(showMoreButton).toBeNull();
showLessButton = screen.queryByRole('button', { name: /showless/i });
expect(showLessButton).toBeInstanceOf(HTMLButtonElement);
});
`
- It must await the user action to succeed. Why, because of something specific in that component?
- Some other tests use fireEvent which we should probably avoid in favour of userEvent?
- Our existing chapter suggests we use waitFor but the test I linked doesn't use it. When do we need to use waitFor if ever?
Also how does this fit in in the existing chapter. Maybe I should have told you upfront that the chapter already exist, and maybe you should have known :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated test cases.
- Added a chapter explaining usage of await,
- Definitely.
- Updated the Existing chapter.
Done in: |
Help from ChatGPT :)