@@ -31,6 +31,7 @@ function setup({
31
31
scripts,
32
32
defaultPendingMs,
33
33
defaultPendingMinMs,
34
+ staleTime,
34
35
} : {
35
36
beforeLoad ?: ( ) => any
36
37
loader ?: ( ) => any
@@ -39,24 +40,26 @@ function setup({
39
40
scripts ?: ( ) => any
40
41
defaultPendingMs ?: number
41
42
defaultPendingMinMs ?: number
43
+ staleTime ?: number
42
44
} ) {
43
45
const select = vi . fn ( )
44
46
45
47
const rootRoute = createRootRoute ( {
46
48
component : function RootComponent ( ) {
47
49
useRouterState ( { select } )
48
- return < Outlet />
50
+ return (
51
+ < >
52
+ < Link to = "/" > Back</ Link >
53
+ < Link to = "/posts" > Posts</ Link >
54
+ < Outlet />
55
+ </ >
56
+ )
49
57
} ,
50
58
} )
51
59
const indexRoute = createRoute ( {
52
60
getParentRoute : ( ) => rootRoute ,
53
61
path : '/' ,
54
- component : ( ) => (
55
- < >
56
- < h1 > Index</ h1 >
57
- < Link to = "/posts" > Posts</ Link >
58
- </ >
59
- ) ,
62
+ component : ( ) => < h1 > Index</ h1 > ,
60
63
} )
61
64
62
65
const postsRoute = createRoute ( {
@@ -83,13 +86,24 @@ function setup({
83
86
defaultPendingComponent : ( ) => < p > Loading...</ p > ,
84
87
defaultNotFoundComponent : ( ) => < h1 > Not Found Title</ h1 > ,
85
88
defaultPreload : 'intent' ,
89
+ defaultStaleTime : staleTime ,
90
+ defaultGcTime : staleTime ,
86
91
} )
87
92
88
93
render ( < RouterProvider router = { router } /> )
89
94
90
95
return { select, router }
91
96
}
92
97
98
+ async function back ( ) {
99
+ const link = await waitFor ( ( ) => screen . getByRole ( 'link' , { name : 'Back' } ) )
100
+ fireEvent . click ( link )
101
+ const title = await waitFor ( ( ) =>
102
+ screen . getByRole ( 'heading' , { name : / I n d e x / } ) ,
103
+ )
104
+ expect ( title ) . toBeInTheDocument ( )
105
+ }
106
+
93
107
async function run ( { select } : ReturnType < typeof setup > ) {
94
108
// navigate to /posts
95
109
const link = await waitFor ( ( ) => screen . getByRole ( 'link' , { name : 'Posts' } ) )
@@ -211,4 +225,70 @@ describe("Store doesn't update *too many* times during navigation", () => {
211
225
// Any change that increases this number should be investigated.
212
226
expect ( updates ) . toBe ( 14 )
213
227
} )
228
+
229
+ test ( 'navigate, w/ preloaded & async loaders' , async ( ) => {
230
+ const params = setup ( {
231
+ beforeLoad : ( ) => Promise . resolve ( { foo : 'bar' } ) ,
232
+ loader : ( ) => resolveAfter ( 100 , { hello : 'world' } ) ,
233
+ staleTime : 1000 ,
234
+ } )
235
+
236
+ await params . router . preloadRoute ( { to : '/posts' } )
237
+ const updates = await run ( params )
238
+
239
+ // This number should be as small as possible to minimize the amount of work
240
+ // that needs to be done during a navigation.
241
+ // Any change that increases this number should be investigated.
242
+ expect ( updates ) . toBe ( 7 )
243
+ } )
244
+
245
+ test ( 'navigate, w/ preloaded & sync loaders' , async ( ) => {
246
+ const params = setup ( {
247
+ beforeLoad : ( ) => ( { foo : 'bar' } ) ,
248
+ loader : ( ) => ( { hello : 'world' } ) ,
249
+ staleTime : 1000 ,
250
+ } )
251
+
252
+ await params . router . preloadRoute ( { to : '/posts' } )
253
+ const updates = await run ( params )
254
+
255
+ // This number should be as small as possible to minimize the amount of work
256
+ // that needs to be done during a navigation.
257
+ // Any change that increases this number should be investigated.
258
+ expect ( updates ) . toBe ( 6 )
259
+ } )
260
+
261
+ test ( 'navigate, w/ previous navigation & async loader' , async ( ) => {
262
+ const params = setup ( {
263
+ loader : ( ) => resolveAfter ( 100 , { hello : 'world' } ) ,
264
+ staleTime : 1000 ,
265
+ } )
266
+
267
+ await run ( params )
268
+ await back ( )
269
+ const updates = await run ( params )
270
+
271
+ // This number should be as small as possible to minimize the amount of work
272
+ // that needs to be done during a navigation.
273
+ // Any change that increases this number should be investigated.
274
+ expect ( updates ) . toBe ( 5 )
275
+ } )
276
+
277
+ test ( 'preload a preloaded route w/ async loader' , async ( ) => {
278
+ const params = setup ( {
279
+ loader : ( ) => resolveAfter ( 100 , { hello : 'world' } ) ,
280
+ } )
281
+
282
+ await params . router . preloadRoute ( { to : '/posts' } )
283
+ await new Promise ( ( r ) => setTimeout ( r , 20 ) )
284
+ const before = params . select . mock . calls . length
285
+ await params . router . preloadRoute ( { to : '/posts' } )
286
+ const after = params . select . mock . calls . length
287
+ const updates = after - before
288
+
289
+ // This number should be as small as possible to minimize the amount of work
290
+ // that needs to be done during a navigation.
291
+ // Any change that increases this number should be investigated.
292
+ expect ( updates ) . toBe ( 1 )
293
+ } )
214
294
} )
0 commit comments