11/**
22 * @vitest -environment happy-dom
33 */
4- import { App , defineComponent } from 'vue'
4+ import { App , defineComponent , markRaw } from 'vue'
55import { defineColadaLoader } from './defineColadaLoader'
66import {
77 describe ,
@@ -15,6 +15,7 @@ import {
1515import {
1616 DataLoaderPlugin ,
1717 DataLoaderPluginOptions ,
18+ getCurrentContext ,
1819 setCurrentContext ,
1920 UseDataLoader ,
2021} from 'unplugin-vue-router/runtime'
@@ -23,7 +24,12 @@ import { getRouter } from 'vue-router-mock'
2324import { enableAutoUnmount , mount } from '@vue/test-utils'
2425import RouterViewMock from '../../tests/data-loaders/RouterViewMock.vue'
2526import { setActivePinia , createPinia , getActivePinia } from 'pinia'
26- import { useQuery , PiniaColada } from '@pinia/colada'
27+ import {
28+ useQuery ,
29+ PiniaColada ,
30+ useQueryCache ,
31+ reviveTreeMap ,
32+ } from '@pinia/colada'
2733import { RouteLocationNormalizedLoaded } from 'vue-router'
2834
2935describe (
@@ -205,5 +211,56 @@ describe(
205211 expect ( query ) . toHaveBeenCalledTimes ( 2 )
206212 expect ( loaderData . value ) . toBe ( '1' )
207213 } )
214+
215+ // NOTE: this test should fail if the `setCurrentContext(currentContext)` is not called in the `if (isInitial)` branch
216+ it . todo ( 'restores the context after using a loader' , async ( ) => {
217+ const query = vi . fn ( ) . mockResolvedValue ( 'data' )
218+
219+ const useData = defineColadaLoader ( {
220+ query,
221+ key : ( ) => [ 'id' ] ,
222+ } )
223+
224+ let useDataResult : ReturnType < typeof useData > | undefined
225+ const component = defineComponent ( {
226+ setup ( ) {
227+ useDataResult = useData ( )
228+ expect ( getCurrentContext ( ) ) . toEqual ( [ ] )
229+
230+ const { data, error, isLoading } = useDataResult
231+ return { data, error, isLoading }
232+ } ,
233+ template : `<p/>` ,
234+ } )
235+
236+ const router = getRouter ( )
237+ router . addRoute ( {
238+ name : '_test' ,
239+ path : '/fetch' ,
240+ meta : {
241+ loaders : [ useData ] ,
242+ } ,
243+ component,
244+ } )
245+
246+ const pinia = createPinia ( )
247+
248+ const treeMap = reviveTreeMap ( [
249+ [ 'id' , [ 'data' , null , Date . now ( ) ] , undefined ] ,
250+ ] )
251+ pinia . state . value [ useQueryCache . $id ] = { caches : markRaw ( treeMap ) }
252+
253+ const wrapper = mount ( RouterViewMock , {
254+ global : {
255+ plugins : [ [ DataLoaderPlugin , { router } ] , pinia , PiniaColada ] ,
256+ } ,
257+ } )
258+
259+ await router . push ( '/fetch' )
260+
261+ expect ( useDataResult ?. data . value ) . toBe ( 'data' )
262+
263+ expect ( getCurrentContext ( ) ) . toEqual ( [ ] )
264+ } )
208265 }
209266)
0 commit comments