Skip to content

tests: reproducer for #4245 #4246

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions packages/react-router/tests/loaders.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ afterEach(() => {
})

const WAIT_TIME = 100
const LOADER_WAIT_TIME = 1000

describe('loaders are being called', () => {
configure({ reactStrictMode: true })
Expand Down Expand Up @@ -325,3 +326,90 @@ test('throw error from beforeLoad when navigating to route', async () => {
const indexElement = await screen.findByText('fooErrorComponent')
expect(indexElement).toBeInTheDocument()
})

test('reproducer #4245', async () => {
const rootRoute = createRootRoute({})

const indexRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/',
loader: async () => {
await sleep(LOADER_WAIT_TIME)
return 'index'
},
component: () => {
const data = indexRoute.useLoaderData()
return (
<div>
<Link to="/foo">foo</Link>
{data}
</div>
)
},
})

const fooRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/foo',
component: () => <Link to="/">index</Link>,
})

const routeTree = rootRoute.addChildren([indexRoute, fooRoute])
const router = createRouter({ routeTree })

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add

await router.load()

to ensure the test progresses only after the loading is done

await router.load()

// We wait for the initial loader to complete
const fooLink = await screen.findByRole(
'link',
{ name: 'foo' },
{ timeout: LOADER_WAIT_TIME + WAIT_TIME },
)
expect(fooLink).toBeInTheDocument()

// We navigate to the foo route
fireEvent.click(fooLink)

// We immediately see the content of the foo route
const indexLink = await screen.findByRole(
'link',
{ name: 'index' },
{ timeout: WAIT_TIME },
)
expect(indexLink).toBeInTheDocument()

// We navigate to the index route
fireEvent.click(indexLink)

// We immediately see the content of the index route because the stale data is still available
const fooLink2 = await screen.findByRole(
'link',
{ name: 'foo' },
{ timeout: WAIT_TIME },
)
expect(fooLink2).toBeInTheDocument()

// We navigate to the foo route again
fireEvent.click(fooLink2)

// We immediately see the content of the foo route
const indexLink2 = await screen.findByRole(
'link',
{ name: 'index' },
{ timeout: WAIT_TIME },
)
expect(indexLink2).toBeInTheDocument()

// We navigate to the index route again
fireEvent.click(indexLink2)

// We now should see the content of the index route immediately because the stale data is still available
const fooLink3 = await screen.findByRole(
'link',
{ name: 'foo' },
{ timeout: WAIT_TIME },
)
expect(fooLink3).toBeInTheDocument()
})
Loading