Skip to content

Commit

Permalink
test(react-router): reducing the React.act warnings during unit tes…
Browse files Browse the repository at this point in the history
…ts (#1975)

* test(react-router): loaders

* chore(react-router): act does not need to be awaited here

* test(react-router): createLazyRoute

* test(react-router): useMatch

* test(react-router): add a setupTests file

* test(react-router): router

* test(react-router): renable these tests 😅

* refactor(react-router): move to the waitFor flow

* refactor(react-router): link
  • Loading branch information
SeanCassiere authored Jul 17, 2024
1 parent da9d52a commit 7452f23
Show file tree
Hide file tree
Showing 8 changed files with 452 additions and 332 deletions.
16 changes: 12 additions & 4 deletions packages/react-router/tests/createLazyRoute.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import React, { act } from 'react'
import { afterEach, describe, expect, it, vi } from 'vitest'
import '@testing-library/jest-dom/vitest'
import { cleanup, configure, render, screen } from '@testing-library/react'
import {
cleanup,
configure,
render,
screen,
waitFor,
} from '@testing-library/react'
import {
Link,
RouterProvider,
Expand Down Expand Up @@ -79,9 +85,9 @@ describe('preload: matched routes', { timeout: 20000 }, () => {
it('should render the heavy/lazy component', async () => {
const { router } = createTestRouter(createBrowserHistory())

await act(() => render(<RouterProvider router={router} />))
render(<RouterProvider router={router} />)

const linkToHeavy = await screen.findByText('Link to heavy')
const linkToHeavy = await waitFor(() => screen.findByText('Link to heavy'))
expect(linkToHeavy).toBeInTheDocument()

expect(router.state.location.pathname).toBe('/')
Expand All @@ -90,7 +96,9 @@ describe('preload: matched routes', { timeout: 20000 }, () => {
// click the link to navigate to the heavy route
act(() => linkToHeavy.click())

const heavyElement = await screen.findByText('I am sooo heavy')
const heavyElement = await waitFor(() =>
screen.findByText('I am sooo heavy'),
)
expect(heavyElement).toBeInTheDocument()

expect(router.state.location.pathname).toBe('/heavy')
Expand Down
Loading

0 comments on commit 7452f23

Please sign in to comment.