@@ -339,3 +339,77 @@ test('throw error from beforeLoad when navigating to route', async () => {
339
339
const indexElement = await screen . findByText ( 'fooErrorComponent' )
340
340
expect ( indexElement ) . toBeInTheDocument ( )
341
341
} )
342
+
343
+ test ( 'reproducer #4245' , async ( ) => {
344
+ const LOADER_WAIT_TIME = 500
345
+ const rootRoute = createRootRoute ( { } )
346
+
347
+ const indexRoute = createRoute ( {
348
+ getParentRoute : ( ) => rootRoute ,
349
+ path : '/' ,
350
+ loader : async ( ) => {
351
+ await sleep ( LOADER_WAIT_TIME )
352
+ return 'index'
353
+ } ,
354
+
355
+ component : ( ) => {
356
+ const data = indexRoute . useLoaderData ( )
357
+ return (
358
+ < div >
359
+ < Link to = "/foo" data-testid = "link-to-foo" >
360
+ foo
361
+ </ Link >
362
+ { data }
363
+ </ div >
364
+ )
365
+ } ,
366
+ } )
367
+
368
+ const fooRoute = createRoute ( {
369
+ getParentRoute : ( ) => rootRoute ,
370
+ path : '/foo' ,
371
+ component : ( ) => (
372
+ < Link to = "/" data-testid = "link-to-index" >
373
+ index
374
+ </ Link >
375
+ ) ,
376
+ } )
377
+
378
+ const routeTree = rootRoute . addChildren ( [ indexRoute , fooRoute ] )
379
+ const router = createRouter ( { routeTree } )
380
+
381
+ render ( < RouterProvider router = { router } /> )
382
+ // We wait for the initial loader to complete
383
+ await router . load ( )
384
+ const fooLink = await screen . findByTestId ( 'link-to-foo' )
385
+
386
+ expect ( fooLink ) . toBeInTheDocument ( )
387
+
388
+ // We navigate to the foo route
389
+ fireEvent . click ( fooLink )
390
+
391
+ // We immediately see the content of the foo route
392
+ const indexLink = await screen . findByTestId ( 'link-to-index' )
393
+ expect ( indexLink ) . toBeInTheDocument ( )
394
+
395
+ // We navigate to the index route
396
+ fireEvent . click ( indexLink )
397
+
398
+ // We immediately see the content of the index route because the stale data is still available
399
+ const fooLink2 = await screen . findByTestId ( 'link-to-foo' )
400
+ expect ( fooLink2 ) . toBeInTheDocument ( )
401
+
402
+ // We navigate to the foo route again
403
+ fireEvent . click ( fooLink2 )
404
+
405
+ // We immediately see the content of the foo route
406
+ const indexLink2 = await screen . findByTestId ( 'link-to-index' )
407
+ expect ( indexLink2 ) . toBeInTheDocument ( )
408
+
409
+ // We navigate to the index route again
410
+ fireEvent . click ( indexLink2 )
411
+
412
+ // We now should see the content of the index route immediately because the stale data is still available
413
+ const fooLink3 = await screen . findByTestId ( 'link-to-foo' )
414
+ expect ( fooLink3 ) . toBeInTheDocument ( )
415
+ } )
0 commit comments