Skip to content

Commit 470f9b6

Browse files
authored
test(router): add waitFor for page navigation link/optional-path-params (#5741)
1 parent 7fbd3e2 commit 470f9b6

File tree

4 files changed

+44
-19
lines changed

4 files changed

+44
-19
lines changed

packages/react-router/tests/link.test.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,10 @@ describe('Link', () => {
360360
// navigate to /?foo=bar
361361
await act(() => fireEvent.click(indexFooBarLink))
362362

363+
await waitFor(() => {
364+
expect(indexFooBarLink).toHaveClass('active')
365+
})
366+
363367
expect(indexExactLink).toHaveClass('inactive')
364368
expect(indexExactLink).not.toHaveClass('active')
365369
expect(indexExactLink).toHaveAttribute('href', '/')

packages/react-router/tests/optional-path-params.test.tsx

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import { afterEach, describe, expect, it, vi } from 'vitest'
2-
import { act, cleanup, fireEvent, render, screen } from '@testing-library/react'
2+
import {
3+
act,
4+
cleanup,
5+
fireEvent,
6+
render,
7+
screen,
8+
waitFor,
9+
} from '@testing-library/react'
310
import {
411
Link,
512
Outlet,
@@ -639,17 +646,22 @@ describe('React Router - Optional Path Parameters', () => {
639646

640647
// Test navigation scenarios
641648
const navigateAll = await screen.findByTestId('navigate-all')
642-
const navigateTech = await screen.findByTestId('navigate-tech')
643-
const navigateSpecific = await screen.findByTestId('navigate-specific')
644-
645-
fireEvent.click(navigateAll)
646-
expect(router.state.location.pathname).toBe('/posts')
649+
await fireEvent.click(navigateAll)
650+
await waitFor(() => {
651+
expect(router.state.location.pathname).toBe('/posts')
652+
})
647653

648-
fireEvent.click(navigateTech)
649-
expect(router.state.location.pathname).toBe('/posts/tech')
654+
const navigateTech = await screen.findByTestId('navigate-tech')
655+
await fireEvent.click(navigateTech)
656+
await waitFor(() => {
657+
expect(router.state.location.pathname).toBe('/posts/tech')
658+
})
650659

651-
fireEvent.click(navigateSpecific)
652-
expect(router.state.location.pathname).toBe('/posts/tech/hello-world')
660+
const navigateSpecific = await screen.findByTestId('navigate-specific')
661+
await fireEvent.click(navigateSpecific)
662+
await waitFor(() => {
663+
expect(router.state.location.pathname).toBe('/posts/tech/hello-world')
664+
})
653665
})
654666

655667
it('should handle relative navigation with optional parameters', async () => {

packages/solid-router/tests/link.test.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,10 @@ describe('Link', () => {
393393
// navigate to /?foo=bar
394394
fireEvent.click(indexFooBarLink)
395395

396+
await waitFor(() => {
397+
expect(indexFooBarLink).toHaveClass('active')
398+
})
399+
396400
expect(indexExactLink).toHaveClass('inactive')
397401
expect(indexExactLink).not.toHaveClass('active')
398402
expect(indexExactLink).toHaveAttribute('href', '/')

packages/solid-router/tests/optional-path-params.test.tsx

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -647,17 +647,22 @@ describe('Solid Router - Optional Path Parameters', () => {
647647

648648
// Test navigation scenarios
649649
const navigateAll = await screen.findByTestId('navigate-all')
650-
const navigateTech = await screen.findByTestId('navigate-tech')
651-
const navigateSpecific = await screen.findByTestId('navigate-specific')
652-
653-
fireEvent.click(navigateAll)
654-
expect(router.state.location.pathname).toBe('/posts')
650+
await fireEvent.click(navigateAll)
651+
await waitFor(() => {
652+
expect(router.state.location.pathname).toBe('/posts')
653+
})
655654

656-
fireEvent.click(navigateTech)
657-
expect(router.state.location.pathname).toBe('/posts/tech')
655+
const navigateTech = await screen.findByTestId('navigate-tech')
656+
await fireEvent.click(navigateTech)
657+
await waitFor(() => {
658+
expect(router.state.location.pathname).toBe('/posts/tech')
659+
})
658660

659-
fireEvent.click(navigateSpecific)
660-
expect(router.state.location.pathname).toBe('/posts/tech/hello-world')
661+
const navigateSpecific = await screen.findByTestId('navigate-specific')
662+
await fireEvent.click(navigateSpecific)
663+
await waitFor(() => {
664+
expect(router.state.location.pathname).toBe('/posts/tech/hello-world')
665+
})
661666
})
662667

663668
it('should handle relative navigation with optional parameters', async () => {

0 commit comments

Comments
 (0)