Skip to content

Commit f42b94e

Browse files
committed
test: ssr hydrate
1 parent a60324b commit f42b94e

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

src/data-loaders/defineColadaLoader.spec.ts

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,58 @@ describe(
212212
expect(loaderData.value).toBe('1')
213213
})
214214

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+
215267
// NOTE: this test should fail if the `setCurrentContext(currentContext)` is not called in the `if (isInitial)` branch
216268
it.todo('restores the context after using a loader', async () => {
217269
const query = vi.fn().mockResolvedValue('data')
@@ -250,7 +302,7 @@ describe(
250302
])
251303
pinia.state.value[useQueryCache.$id] = { caches: markRaw(treeMap) }
252304

253-
const wrapper = mount(RouterViewMock, {
305+
mount(RouterViewMock, {
254306
global: {
255307
plugins: [[DataLoaderPlugin, { router }], pinia, PiniaColada],
256308
},

0 commit comments

Comments
 (0)