-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: use testing-library/user-event v14
- Loading branch information
Showing
4 changed files
with
29 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,21 @@ | ||
import { render, screen, userEvent, act } from '@utils/test/app-test-utils' | ||
import { render, screen } from '@utils/test/app-test-utils' | ||
import App from '@src/App' | ||
|
||
test('should render App with Homepage first and navigate through pages', () => { | ||
// @testing-library/user-event v14 doesn't trigger a rerender. | ||
// eventually fix this issue and remove the act() calls. | ||
test('should render App with Homepage first and navigate through pages', async () => { | ||
const { user } = render(<App />) | ||
|
||
render(<App />) | ||
// start with Homepage | ||
expect(screen.getByRole('heading', { name: /Homepage/i })).toBeInTheDocument() | ||
|
||
// navigate to About page | ||
act(() => userEvent.click(screen.getByRole('link', { name: /go to about/i }))) | ||
expect(screen.getByRole('heading', { name: /About/i })).toBeInTheDocument() | ||
user.click(screen.getByRole('link', { name: /go to about/i })) | ||
expect(await screen.findByRole('heading', { name: /About/i })).toBeInTheDocument() | ||
|
||
// navigate to Fetch page | ||
act(() => userEvent.click(screen.getByRole('link', { name: /go to fetch/i }))) | ||
expect(screen.getByRole('heading', { name: /fetch/i })).toBeInTheDocument() | ||
user.click(screen.getByRole('link', { name: /go to fetch/i })) | ||
expect(await screen.findByRole('heading', { name: /fetch/i })).toBeInTheDocument() | ||
|
||
// navigate to Homepage page | ||
act(() => userEvent.click(screen.getByRole('link', { name: /go to homepage/i }))) | ||
expect(screen.getByRole('heading', { name: /Homepage/i })).toBeInTheDocument() | ||
user.click(screen.getByRole('link', { name: /go to homepage/i })) | ||
expect(await screen.findByRole('heading', { name: /Homepage/i })).toBeInTheDocument() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,22 @@ | ||
import { render as rtlRender } from '@testing-library/react' | ||
import userEvent from '@testing-library/user-event' | ||
import type { RenderOptions, RenderResult } from '@testing-library/react' | ||
import userEvent from '@testing-library/user-event' | ||
import type { UserEvent } from '@testing-library/user-event/dist/types/setup' | ||
import AppProviders from '@src/context/app-providers' | ||
|
||
function render(ui: React.ReactElement, options?: Omit<RenderOptions, 'queries'>): RenderResult { | ||
return rtlRender(ui, { wrapper: AppProviders, ...options }) | ||
type CustomRenderResult = { | ||
user: UserEvent | ||
} & RenderResult | ||
|
||
function customRender( | ||
ui: React.ReactElement, | ||
options?: Omit<RenderOptions, 'queries'> | ||
): CustomRenderResult { | ||
return { | ||
user: userEvent.setup(), | ||
...rtlRender(ui, { wrapper: AppProviders, ...options }), | ||
} | ||
} | ||
|
||
export * from '@testing-library/react' | ||
export { render, userEvent } | ||
export { customRender as render } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters