1
1
/**
2
2
* @vitest -environment happy-dom
3
3
*/
4
- import { App , defineComponent } from 'vue'
4
+ import { App , defineComponent , markRaw } from 'vue'
5
5
import { defineColadaLoader } from './defineColadaLoader'
6
6
import {
7
7
describe ,
@@ -15,6 +15,7 @@ import {
15
15
import {
16
16
DataLoaderPlugin ,
17
17
DataLoaderPluginOptions ,
18
+ getCurrentContext ,
18
19
setCurrentContext ,
19
20
UseDataLoader ,
20
21
} from 'unplugin-vue-router/runtime'
@@ -23,7 +24,12 @@ import { getRouter } from 'vue-router-mock'
23
24
import { enableAutoUnmount , mount } from '@vue/test-utils'
24
25
import RouterViewMock from '../../tests/data-loaders/RouterViewMock.vue'
25
26
import { 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'
27
33
import { RouteLocationNormalizedLoaded } from 'vue-router'
28
34
29
35
describe (
@@ -205,5 +211,56 @@ describe(
205
211
expect ( query ) . toHaveBeenCalledTimes ( 2 )
206
212
expect ( loaderData . value ) . toBe ( '1' )
207
213
} )
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
+ } )
208
265
}
209
266
)
0 commit comments