1
- import { Application } from '@feathersjs/feathers' ;
1
+ import { Application , Params } from '@feathersjs/feathers' ;
2
2
import { nextTick , ref } from 'vue' ;
3
3
4
4
import useFindOriginal , { UseFind } from '~/useFind' ;
@@ -142,11 +142,16 @@ describe('Find composition', () => {
142
142
} ) ;
143
143
144
144
it ( 'should reload data after changing the params' , async ( ) => {
145
- expect . assertions ( 2 ) ;
145
+ expect . assertions ( 3 ) ;
146
146
147
147
// given
148
148
const findParams = ref ( { query : { zug : 'start' } } ) ;
149
- const serviceFind = jest . fn ( ( ) => Promise . resolve ( [ ] ) ) ;
149
+ const serviceFind = jest . fn ( ( params : Params ) => {
150
+ if ( params . query && params . query . zug === 'start' ) {
151
+ return [ additionalTestModel ] ;
152
+ }
153
+ return [ testModel ] ;
154
+ } ) ;
150
155
const feathersMock = {
151
156
service : ( ) => ( {
152
157
find : serviceFind ,
@@ -157,12 +162,17 @@ describe('Find composition', () => {
157
162
off : jest . fn ( ) ,
158
163
} as unknown as Application ;
159
164
const useFind = useFindOriginal ( feathersMock ) ;
165
+ let findComposition = null as UseFind < TestModel > | null ;
160
166
mountComposition ( ( ) => {
161
- useFind ( 'testModels' , findParams ) ;
167
+ findComposition = useFind ( 'testModels' , findParams ) ;
162
168
} ) ;
163
169
170
+ // before then to ensure that the previous loading procedure is completed
171
+ await nextTick ( ) ;
172
+ expect ( findComposition && findComposition . data . value ) . toStrictEqual ( [ additionalTestModel ] ) ;
173
+
164
174
// when
165
- findParams . value . query = { zug : 'change' } ;
175
+ findParams . value = { query : { zug : 'change' } } ;
166
176
await nextTick ( ) ;
167
177
168
178
// then
@@ -175,6 +185,162 @@ describe('Find composition', () => {
175
185
) ;
176
186
} ) ;
177
187
188
+ it ( 'should un-load data after params changes to ref of undefined' , async ( ) => {
189
+ expect . assertions ( 5 ) ;
190
+
191
+ // given
192
+ const findParams = ref < Params | undefined > ( { query : { zug : 'start' } } ) ;
193
+ const serviceFind = jest . fn ( ( ) => Promise . resolve ( [ testModel ] ) ) ;
194
+ const feathersMock = {
195
+ service : ( ) => ( {
196
+ find : serviceFind ,
197
+ on : jest . fn ( ) ,
198
+ off : jest . fn ( ) ,
199
+ } ) ,
200
+ on : jest . fn ( ) ,
201
+ off : jest . fn ( ) ,
202
+ } as unknown as Application ;
203
+ const useFind = useFindOriginal ( feathersMock ) ;
204
+ let findComposition = null as UseFind < TestModel > | null ;
205
+ mountComposition ( ( ) => {
206
+ findComposition = useFind ( 'testModels' , findParams ) ;
207
+ } ) ;
208
+
209
+ // before then to ensure that the previous loading procedure is completed
210
+ await nextTick ( ) ;
211
+ expect ( findComposition && findComposition . data . value ) . toStrictEqual ( [ testModel ] ) ;
212
+
213
+ // when
214
+ findParams . value = undefined ;
215
+ await nextTick ( ) ;
216
+
217
+ // then
218
+ expect ( serviceFind ) . toHaveBeenCalledTimes ( 1 ) ;
219
+ expect ( findComposition ) . toBeTruthy ( ) ;
220
+ expect ( findComposition && findComposition . isLoading . value ) . toBeFalsy ( ) ;
221
+ expect ( findComposition && findComposition . data . value ) . toStrictEqual ( [ ] ) ;
222
+ } ) ;
223
+
224
+ it ( 'should un-load data after params changes to ref of null' , async ( ) => {
225
+ expect . assertions ( 5 ) ;
226
+
227
+ // given
228
+ const findParams = ref < Params | null > ( { query : { zug : 'start' } } ) ;
229
+ const serviceFind = jest . fn ( ( ) => Promise . resolve ( [ testModel ] ) ) ;
230
+ const feathersMock = {
231
+ service : ( ) => ( {
232
+ find : serviceFind ,
233
+ on : jest . fn ( ) ,
234
+ off : jest . fn ( ) ,
235
+ } ) ,
236
+ on : jest . fn ( ) ,
237
+ off : jest . fn ( ) ,
238
+ } as unknown as Application ;
239
+ const useFind = useFindOriginal ( feathersMock ) ;
240
+ let findComposition = null as UseFind < TestModel > | null ;
241
+ mountComposition ( ( ) => {
242
+ findComposition = useFind ( 'testModels' , findParams ) ;
243
+ } ) ;
244
+
245
+ // before then to ensure that the previous loading procedure is completed
246
+ await nextTick ( ) ;
247
+ expect ( findComposition && findComposition . data . value ) . toStrictEqual ( [ testModel ] ) ;
248
+
249
+ // when
250
+ findParams . value = null ;
251
+ await nextTick ( ) ;
252
+
253
+ // then
254
+ expect ( serviceFind ) . toHaveBeenCalledTimes ( 1 ) ;
255
+ expect ( findComposition ) . toBeTruthy ( ) ;
256
+ expect ( findComposition && findComposition . isLoading . value ) . toBeFalsy ( ) ;
257
+ expect ( findComposition && findComposition . data . value ) . toStrictEqual ( [ ] ) ;
258
+ } ) ;
259
+
260
+ it ( 'should return empty data when params is ref of undefined and load data after changing to valid params' , async ( ) => {
261
+ expect . assertions ( 6 ) ;
262
+
263
+ // given
264
+ const findParams = ref < Params | undefined > ( ) ;
265
+ const serviceFind = jest . fn ( ( params : Params ) => {
266
+ if ( params . query && params . query . zug === 'start' ) {
267
+ return [ additionalTestModel ] ;
268
+ }
269
+ return [ testModel ] ;
270
+ } ) ;
271
+ const feathersMock = {
272
+ service : ( ) => ( {
273
+ find : serviceFind ,
274
+ on : jest . fn ( ) ,
275
+ off : jest . fn ( ) ,
276
+ } ) ,
277
+ on : jest . fn ( ) ,
278
+ off : jest . fn ( ) ,
279
+ } as unknown as Application ;
280
+ const useFind = useFindOriginal ( feathersMock ) ;
281
+ let findComposition = null as UseFind < TestModel > | null ;
282
+ mountComposition ( ( ) => {
283
+ findComposition = useFind ( 'testModels' , findParams ) ;
284
+ } ) ;
285
+
286
+ // before then to ensure that the previous loading procedure is completed
287
+ await nextTick ( ) ;
288
+ expect ( findComposition && findComposition . isLoading . value ) . toBeFalsy ( ) ;
289
+ expect ( findComposition && findComposition . data . value ) . toStrictEqual ( [ ] ) ;
290
+
291
+ // when
292
+ findParams . value = { query : { zug : 'start' } } ;
293
+ await nextTick ( ) ;
294
+
295
+ // then
296
+ expect ( serviceFind ) . toHaveBeenCalledTimes ( 1 ) ;
297
+ expect ( findComposition ) . toBeTruthy ( ) ;
298
+ expect ( findComposition && findComposition . isLoading . value ) . toBeFalsy ( ) ;
299
+ expect ( findComposition && findComposition . data . value ) . toStrictEqual ( [ additionalTestModel ] ) ;
300
+ } ) ;
301
+
302
+ it ( 'should return empty data when params is ref of null and load data after changing to valid params' , async ( ) => {
303
+ expect . assertions ( 6 ) ;
304
+
305
+ // given
306
+ const findParams = ref < Params | null > ( null ) ;
307
+ const serviceFind = jest . fn ( ( params : Params ) => {
308
+ if ( params . query && params . query . zug === 'start' ) {
309
+ return [ additionalTestModel ] ;
310
+ }
311
+ return [ testModel ] ;
312
+ } ) ;
313
+ const feathersMock = {
314
+ service : ( ) => ( {
315
+ find : serviceFind ,
316
+ on : jest . fn ( ) ,
317
+ off : jest . fn ( ) ,
318
+ } ) ,
319
+ on : jest . fn ( ) ,
320
+ off : jest . fn ( ) ,
321
+ } as unknown as Application ;
322
+ const useFind = useFindOriginal ( feathersMock ) ;
323
+ let findComposition = null as UseFind < TestModel > | null ;
324
+ mountComposition ( ( ) => {
325
+ findComposition = useFind ( 'testModels' , findParams ) ;
326
+ } ) ;
327
+
328
+ // before then to ensure that the previous loading procedure is completed
329
+ await nextTick ( ) ;
330
+ expect ( findComposition && findComposition . isLoading . value ) . toBeFalsy ( ) ;
331
+ expect ( findComposition && findComposition . data . value ) . toStrictEqual ( [ ] ) ;
332
+
333
+ // when
334
+ findParams . value = { query : { zug : 'start' } } ;
335
+ await nextTick ( ) ;
336
+
337
+ // then
338
+ expect ( serviceFind ) . toHaveBeenCalledTimes ( 1 ) ;
339
+ expect ( findComposition ) . toBeTruthy ( ) ;
340
+ expect ( findComposition && findComposition . isLoading . value ) . toBeFalsy ( ) ;
341
+ expect ( findComposition && findComposition . data . value ) . toStrictEqual ( [ additionalTestModel ] ) ;
342
+ } ) ;
343
+
178
344
describe ( 'Event Handlers' , ( ) => {
179
345
it ( 'should listen to "create" events' , ( ) => {
180
346
expect . assertions ( 2 ) ;
0 commit comments