@@ -1490,7 +1490,11 @@ describe('Link', () => {
14901490 return (
14911491 < >
14921492 < h1 > Index</ h1 >
1493- < Link to = "/posts/$postId" params = { { postId : 'id1' } } >
1493+ < Link
1494+ to = "/posts/$postId"
1495+ params = { { postId : 'id1' } }
1496+ preloadDelay = { 0 }
1497+ >
14941498 To first post
14951499 </ Link >
14961500 </ >
@@ -4481,6 +4485,114 @@ describe('Link', () => {
44814485 expect ( mock ) . toHaveBeenCalledTimes ( 1 )
44824486 } )
44834487
4488+ test . each ( [ undefined , false , 'render' , 'viewport' ] as const ) (
4489+ 'Link.preload="%s" should not preload on focus, hover, or touchstart' ,
4490+ async ( preloadMode ) => {
4491+ const rootRoute = createRootRoute ( )
4492+ const indexRoute = createRoute ( {
4493+ getParentRoute : ( ) => rootRoute ,
4494+ path : '/' ,
4495+ component : ( ) => (
4496+ < >
4497+ < h1 > Index Heading</ h1 >
4498+ < Link
4499+ to = "/about"
4500+ { ...( preloadMode === undefined ? { } : { preload : preloadMode } ) }
4501+ >
4502+ About Link
4503+ </ Link >
4504+ </ >
4505+ ) ,
4506+ } )
4507+ const aboutRoute = createRoute ( {
4508+ getParentRoute : ( ) => rootRoute ,
4509+ path : '/about' ,
4510+ component : ( ) => < h1 > About Heading</ h1 > ,
4511+ } )
4512+
4513+ const router = createRouter ( {
4514+ routeTree : rootRoute . addChildren ( [ aboutRoute , indexRoute ] ) ,
4515+ defaultPreload : false ,
4516+ defaultPreloadDelay : 0 ,
4517+ history,
4518+ } )
4519+
4520+ const preloadRouteSpy = vi . spyOn ( router , 'preloadRoute' )
4521+
4522+ render ( ( ) => < RouterProvider router = { router } /> )
4523+
4524+ const aboutLink = await screen . findByRole ( 'link' , { name : 'About Link' } )
4525+ expect ( aboutLink ) . toBeInTheDocument ( )
4526+
4527+ if ( preloadMode === 'render' ) {
4528+ await waitFor ( ( ) =>
4529+ expect ( preloadRouteSpy . mock . calls . length ) . toBeGreaterThan ( 0 ) ,
4530+ )
4531+ }
4532+
4533+ const baselineCalls = preloadRouteSpy . mock . calls . length
4534+
4535+ fireEvent . focus ( aboutLink )
4536+ fireEvent . mouseOver ( aboutLink )
4537+ fireEvent . touchStart ( aboutLink )
4538+
4539+ await sleep ( 100 )
4540+ expect ( preloadRouteSpy ) . toHaveBeenCalledTimes ( baselineCalls )
4541+ } ,
4542+ )
4543+
4544+ test ( 'Link.preload="intent" should preload on focus, hover, and touchstart' , async ( ) => {
4545+ const rootRoute = createRootRoute ( )
4546+ const indexRoute = createRoute ( {
4547+ getParentRoute : ( ) => rootRoute ,
4548+ path : '/' ,
4549+ component : ( ) => (
4550+ < >
4551+ < h1 > Index Heading</ h1 >
4552+ < Link to = "/about" preload = "intent" >
4553+ About Link
4554+ </ Link >
4555+ </ >
4556+ ) ,
4557+ } )
4558+ const aboutRoute = createRoute ( {
4559+ getParentRoute : ( ) => rootRoute ,
4560+ path : '/about' ,
4561+ component : ( ) => < h1 > About Heading</ h1 > ,
4562+ } )
4563+
4564+ const router = createRouter ( {
4565+ routeTree : rootRoute . addChildren ( [ aboutRoute , indexRoute ] ) ,
4566+ defaultPreload : false ,
4567+ defaultPreloadDelay : 0 ,
4568+ history,
4569+ } )
4570+
4571+ const preloadRouteSpy = vi . spyOn ( router , 'preloadRoute' )
4572+
4573+ render ( ( ) => < RouterProvider router = { router } /> )
4574+
4575+ const aboutLink = await screen . findByRole ( 'link' , { name : 'About Link' } )
4576+ expect ( aboutLink ) . toBeInTheDocument ( )
4577+
4578+ const baselineCalls = preloadRouteSpy . mock . calls . length
4579+
4580+ fireEvent . focus ( aboutLink )
4581+ await waitFor ( ( ) =>
4582+ expect ( preloadRouteSpy ) . toHaveBeenCalledTimes ( baselineCalls + 1 ) ,
4583+ )
4584+
4585+ fireEvent . mouseOver ( aboutLink )
4586+ await waitFor ( ( ) =>
4587+ expect ( preloadRouteSpy ) . toHaveBeenCalledTimes ( baselineCalls + 2 ) ,
4588+ )
4589+
4590+ fireEvent . touchStart ( aboutLink )
4591+ await waitFor ( ( ) =>
4592+ expect ( preloadRouteSpy ) . toHaveBeenCalledTimes ( baselineCalls + 3 ) ,
4593+ )
4594+ } )
4595+
44844596 test ( 'Router.preload="intent", pendingComponent renders during unresolved route loader' , async ( ) => {
44854597 const rootRoute = createRootRoute ( )
44864598 const indexRoute = createRoute ( {
0 commit comments