Skip to content

Commit a60324b

Browse files
committed
test: todo
1 parent 492362b commit a60324b

File tree

1 file changed

+59
-2
lines changed

1 file changed

+59
-2
lines changed

src/data-loaders/defineColadaLoader.spec.ts

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @vitest-environment happy-dom
33
*/
4-
import { App, defineComponent } from 'vue'
4+
import { App, defineComponent, markRaw } from 'vue'
55
import { defineColadaLoader } from './defineColadaLoader'
66
import {
77
describe,
@@ -15,6 +15,7 @@ import {
1515
import {
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'
2324
import { enableAutoUnmount, mount } from '@vue/test-utils'
2425
import RouterViewMock from '../../tests/data-loaders/RouterViewMock.vue'
2526
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'
2733
import { RouteLocationNormalizedLoaded } from 'vue-router'
2834

2935
describe(
@@ -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

Comments
 (0)