@@ -212,6 +212,58 @@ describe(
212
212
expect ( loaderData . value ) . toBe ( '1' )
213
213
} )
214
214
215
+ it ( 'hydrates without calling the query on the initial navigation' , async ( ) => {
216
+ // setups the loader
217
+ const query = vi . fn ( ) . mockResolvedValue ( 'data' )
218
+ const useData = defineColadaLoader ( {
219
+ query,
220
+ key : ( ) => [ 'id' ] ,
221
+ } )
222
+
223
+ // sets up the page
224
+ let useDataResult : ReturnType < typeof useData > | undefined
225
+ const component = defineComponent ( {
226
+ setup ( ) {
227
+ useDataResult = useData ( )
228
+
229
+ const { data, error, isLoading } = useDataResult
230
+ return { data, error, isLoading }
231
+ } ,
232
+ template : `<p/>` ,
233
+ } )
234
+
235
+ // add the page to the router
236
+ const router = getRouter ( )
237
+ router . addRoute ( {
238
+ name : '_test' ,
239
+ path : '/fetch' ,
240
+ meta : {
241
+ loaders : [ useData ] ,
242
+ } ,
243
+ component,
244
+ } )
245
+
246
+ // sets up the cache
247
+ const pinia = createPinia ( )
248
+ const treeMap = reviveTreeMap ( [
249
+ // entry with successful data for id
250
+ [ 'id' , [ 'data' , null , Date . now ( ) ] , undefined ] ,
251
+ ] )
252
+ pinia . state . value [ useQueryCache . $id ] = { caches : markRaw ( treeMap ) }
253
+
254
+ mount ( RouterViewMock , {
255
+ global : {
256
+ plugins : [ [ DataLoaderPlugin , { router } ] , pinia , PiniaColada ] ,
257
+ } ,
258
+ } )
259
+
260
+ await router . push ( '/fetch' )
261
+ expect ( query ) . toHaveBeenCalledTimes ( 0 )
262
+
263
+ await expect ( async ( ) => useDataResult ! . reload ( ) ) . not . toThrow ( )
264
+ expect ( query ) . toHaveBeenCalledTimes ( 1 )
265
+ } )
266
+
215
267
// NOTE: this test should fail if the `setCurrentContext(currentContext)` is not called in the `if (isInitial)` branch
216
268
it . todo ( 'restores the context after using a loader' , async ( ) => {
217
269
const query = vi . fn ( ) . mockResolvedValue ( 'data' )
@@ -250,7 +302,7 @@ describe(
250
302
] )
251
303
pinia . state . value [ useQueryCache . $id ] = { caches : markRaw ( treeMap ) }
252
304
253
- const wrapper = mount ( RouterViewMock , {
305
+ mount ( RouterViewMock , {
254
306
global : {
255
307
plugins : [ [ DataLoaderPlugin , { router } ] , pinia , PiniaColada ] ,
256
308
} ,
0 commit comments