1
1
/**
2
2
* @vitest -environment happy-dom
3
3
*/
4
- import { App , defineComponent , markRaw } from 'vue'
4
+ import { App , defineComponent , markRaw , nextTick } from 'vue'
5
5
import { defineColadaLoader } from './defineColadaLoader'
6
6
import {
7
7
describe ,
@@ -25,7 +25,6 @@ import { enableAutoUnmount, mount } from '@vue/test-utils'
25
25
import RouterViewMock from '../../tests/data-loaders/RouterViewMock.vue'
26
26
import { setActivePinia , createPinia , getActivePinia } from 'pinia'
27
27
import {
28
- useQuery ,
29
28
PiniaColada ,
30
29
useQueryCache ,
31
30
reviveTreeMap ,
@@ -68,7 +67,13 @@ describe(
68
67
function singleLoaderOneRoute < Loader extends UseDataLoader > (
69
68
useData : Loader ,
70
69
pluginOptions ?: Omit < DataLoaderPluginOptions , 'router' >
71
- ) {
70
+ ) : {
71
+ wrapper : ReturnType < typeof mount >
72
+ router : ReturnType < typeof getRouter >
73
+ // technically it should be () => ReturnType<Loader> but it doesn't infer all the types
74
+ useData : Loader ,
75
+ app : App
76
+ } {
72
77
let useDataResult : ReturnType < Loader >
73
78
const component = defineComponent ( {
74
79
setup ( ) {
@@ -111,6 +116,7 @@ describe(
111
116
return {
112
117
wrapper,
113
118
router,
119
+ // @ts -expect-error: not exactly Loader
114
120
useData : ( ) => {
115
121
if ( useDataResult ) {
116
122
return useDataResult
@@ -148,7 +154,7 @@ describe(
148
154
} )
149
155
150
156
it ( 'updates data loader data if internal data changes' , async ( ) => {
151
- const query = vi . fn ( ) . mockResolvedValue ( 'data' )
157
+ const query = vi . fn ( async ( ) => 'data' )
152
158
153
159
const { router, useData } = singleLoaderOneRoute (
154
160
defineColadaLoader ( {
@@ -165,24 +171,19 @@ describe(
165
171
const wrapper = mount (
166
172
defineComponent ( {
167
173
setup ( ) {
168
- return useQuery ( {
169
- query,
170
- key : [ 'id' ] ,
171
- } )
174
+ const caches = useQueryCache ( )
175
+ return { caches }
172
176
} ,
173
- template : `<div>{{ data }} </div>` ,
177
+ template : `<div></div>` ,
174
178
} ) ,
175
179
{
176
180
global : {
177
181
plugins : [ getActivePinia ( ) ! , PiniaColada ] ,
178
182
} ,
179
183
}
180
184
)
181
- query . mockResolvedValue ( 'new' )
182
- await wrapper . vm . refetch ( )
183
- await vi . runAllTimersAsync ( )
184
- expect ( query ) . toHaveBeenCalledTimes ( 2 )
185
- expect ( wrapper . vm . data ) . toBe ( 'new' )
185
+ wrapper . vm . caches . setQueryData ( [ 'id' ] , 'new' )
186
+ await nextTick ( )
186
187
expect ( loaderData . value ) . toBe ( 'new' )
187
188
} )
188
189
0 commit comments