Skip to content

Commit 475c6d6

Browse files
rootroot
authored andcommitted
Add unit test for reproducing #4245
1 parent 790016d commit 475c6d6

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ afterEach(() => {
2727
})
2828

2929
const WAIT_TIME = 100
30+
const LOADER_WAIT_TIME = 1000
3031

3132
describe('loaders are being called', () => {
3233
configure({ reactStrictMode: true })
@@ -325,3 +326,68 @@ test('throw error from beforeLoad when navigating to route', async () => {
325326
const indexElement = await screen.findByText('fooErrorComponent')
326327
expect(indexElement).toBeInTheDocument()
327328
})
329+
330+
test('reproducer #4245', async () => {
331+
const rootRoute = createRootRoute({})
332+
333+
const indexRoute = createRoute({
334+
getParentRoute: () => rootRoute,
335+
path: '/',
336+
loader: async () => {
337+
await sleep(LOADER_WAIT_TIME)
338+
return 'index'
339+
},
340+
component: () => {
341+
const data = indexRoute.useLoaderData()
342+
return (
343+
<div>
344+
<Link to="/foo">foo</Link>
345+
{data}
346+
</div>
347+
)
348+
},
349+
})
350+
351+
const fooRoute = createRoute({
352+
getParentRoute: () => rootRoute,
353+
path: '/foo',
354+
component: () => <Link to="/">index</Link>,
355+
})
356+
357+
const routeTree = rootRoute.addChildren([indexRoute, fooRoute])
358+
const router = createRouter({ routeTree })
359+
360+
render(<RouterProvider router={router} />)
361+
362+
// We wait for the initial loader to complete
363+
const fooLink = await screen.findByRole('link', { name: 'foo' }, { timeout: LOADER_WAIT_TIME + WAIT_TIME })
364+
expect(fooLink).toBeInTheDocument()
365+
366+
// We navigate to the foo route
367+
fireEvent.click(fooLink)
368+
369+
// We immediately see the content of the foo route
370+
const indexLink = await screen.findByRole('link', { name: 'index' }, { timeout: WAIT_TIME })
371+
expect(indexLink).toBeInTheDocument()
372+
373+
// We navigate to the index route
374+
fireEvent.click(indexLink)
375+
376+
// We immediately see the content of the index route because the stale data is still available
377+
const fooLink2 = await screen.findByRole('link', { name: 'foo' }, { timeout: WAIT_TIME })
378+
expect(fooLink2).toBeInTheDocument()
379+
380+
// We navigate to the foo route again
381+
fireEvent.click(fooLink2)
382+
383+
// We immediately see the content of the foo route
384+
const indexLink2 = await screen.findByRole('link', { name: 'index' }, { timeout: WAIT_TIME })
385+
expect(indexLink2).toBeInTheDocument()
386+
387+
// We navigate to the index route again
388+
fireEvent.click(indexLink2)
389+
390+
// We now should see the content of the index route immediately because the stale data is still available
391+
const fooLink3 = await screen.findByRole('link', { name: 'foo' }, { timeout: WAIT_TIME })
392+
expect(fooLink3).toBeInTheDocument()
393+
})

0 commit comments

Comments
 (0)