@data-client/vue 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -24
- package/dist/index.js +185 -35
- package/dist/index.umd.min.js +1 -1
- package/lib/consumers/index.d.ts +2 -0
- package/lib/consumers/index.d.ts.map +1 -1
- package/lib/consumers/index.js +3 -1
- package/lib/consumers/useCache.d.ts +12 -0
- package/lib/consumers/useCache.d.ts.map +1 -0
- package/lib/consumers/useCache.js +63 -0
- package/lib/consumers/useDLE.d.ts +18 -0
- package/lib/consumers/useDLE.d.ts.map +1 -0
- package/lib/consumers/useDLE.js +88 -0
- package/lib/consumers/useFetch.d.ts +3 -2
- package/lib/consumers/useFetch.d.ts.map +1 -1
- package/lib/consumers/useFetch.js +10 -8
- package/lib/consumers/useLive.d.ts +3 -2
- package/lib/consumers/useLive.d.ts.map +1 -1
- package/lib/consumers/useLive.js +1 -1
- package/lib/consumers/useQuery.d.ts +3 -2
- package/lib/consumers/useQuery.d.ts.map +1 -1
- package/lib/consumers/useQuery.js +7 -3
- package/lib/consumers/useSubscription.d.ts +2 -1
- package/lib/consumers/useSubscription.d.ts.map +1 -1
- package/lib/consumers/useSubscription.js +1 -2
- package/lib/consumers/useSuspense.d.ts +3 -2
- package/lib/consumers/useSuspense.d.ts.map +1 -1
- package/lib/consumers/useSuspense.js +15 -20
- package/lib/index.d.ts +1 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -1
- package/lib/providers/DataClientPlugin.d.ts +6 -0
- package/lib/providers/DataClientPlugin.d.ts.map +1 -1
- package/lib/providers/DataClientPlugin.js +15 -11
- package/lib/providers/index.d.ts +1 -1
- package/lib/providers/index.d.ts.map +1 -1
- package/lib/providers/index.js +1 -1
- package/lib/test/index.d.ts +3 -4
- package/lib/test/index.d.ts.map +1 -1
- package/lib/test/index.js +3 -5
- package/lib/test/mountDataClient.d.ts +29 -0
- package/lib/test/mountDataClient.d.ts.map +1 -0
- package/lib/test/mountDataClient.js +114 -0
- package/lib/test/renderDataCompose.d.ts +19 -0
- package/lib/test/renderDataCompose.d.ts.map +1 -0
- package/lib/test/renderDataCompose.js +135 -0
- package/lib/types.d.ts +10 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/types.js +2 -0
- package/package.json +6 -6
- package/src/__tests__/context.test.ts +195 -0
- package/src/__tests__/integration-garbage-collection.web.ts +12 -15
- package/src/__tests__/useCache.web.ts +391 -0
- package/src/__tests__/useDLE.web.ts +660 -0
- package/src/__tests__/useQuery.web.ts +332 -50
- package/src/__tests__/useSubscription.web.ts +233 -119
- package/src/__tests__/useSuspense.web.ts +176 -50
- package/src/consumers/index.ts +2 -0
- package/src/consumers/useCache.ts +122 -0
- package/src/consumers/useDLE.ts +160 -0
- package/src/consumers/useFetch.ts +24 -13
- package/src/consumers/useLive.ts +6 -2
- package/src/consumers/useQuery.ts +14 -5
- package/src/consumers/useSubscription.ts +5 -1
- package/src/consumers/useSuspense.ts +29 -17
- package/src/index.ts +4 -0
- package/src/providers/DataClientPlugin.ts +25 -10
- package/src/providers/__tests__/DataClientPlugin.test.ts +204 -4
- package/src/providers/__tests__/createDataClient.test.ts +234 -0
- package/src/providers/__tests__/getDefaultManagers.test.ts +180 -0
- package/src/providers/index.ts +5 -1
- package/src/test/README.md +77 -16
- package/src/test/__tests__/mountDataClient.test.ts +426 -0
- package/src/test/__tests__/renderDataCompose.test.ts +586 -0
- package/src/test/index.ts +3 -9
- package/src/test/mountDataClient.ts +169 -0
- package/src/test/renderDataCompose.ts +150 -0
- package/src/types.ts +11 -0
- package/ts3.4/consumers/index.d.ts +2 -0
- package/ts3.4/consumers/useCache.d.ts +14 -0
- package/ts3.4/consumers/useDLE.d.ts +20 -0
- package/ts3.4/consumers/useFetch.d.ts +3 -6
- package/ts3.4/consumers/useLive.d.ts +3 -6
- package/ts3.4/consumers/useQuery.d.ts +3 -2
- package/ts3.4/consumers/useSubscription.d.ts +2 -3
- package/ts3.4/consumers/useSuspense.d.ts +3 -6
- package/ts3.4/index.d.ts +1 -0
- package/ts3.4/providers/DataClientPlugin.d.ts +6 -0
- package/ts3.4/providers/index.d.ts +1 -1
- package/ts3.4/test/index.d.ts +3 -4
- package/ts3.4/test/mountDataClient.d.ts +29 -0
- package/ts3.4/test/renderDataCompose.d.ts +19 -0
- package/ts3.4/types.d.ts +10 -0
- package/lib/test/renderDataClient.d.ts +0 -40
- package/lib/test/renderDataClient.d.ts.map +0 -1
- package/lib/test/renderDataClient.js +0 -264
- package/src/__tests__/__snapshots__/useSubscription.web.ts.snap +0 -7
- package/src/test/renderDataClient.ts +0 -316
- package/ts3.4/test/renderDataClient.d.ts +0 -40
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { schema } from '@data-client/endpoint';
|
|
2
2
|
import { resource } from '@data-client/rest';
|
|
3
|
+
import { reactive, computed } from 'vue';
|
|
3
4
|
|
|
4
5
|
import {
|
|
5
6
|
ArticleWithSlug,
|
|
@@ -12,7 +13,7 @@ import {
|
|
|
12
13
|
SecondUnion,
|
|
13
14
|
} from '../../../../__tests__/new';
|
|
14
15
|
import useQuery from '../consumers/useQuery';
|
|
15
|
-
import {
|
|
16
|
+
import { renderDataCompose } from '../test';
|
|
16
17
|
|
|
17
18
|
// Inline fixtures (duplicated from React tests to avoid cross-project imports)
|
|
18
19
|
const payloadSlug = {
|
|
@@ -47,36 +48,36 @@ const nested = [
|
|
|
47
48
|
];
|
|
48
49
|
|
|
49
50
|
describe('vue useQuery()', () => {
|
|
50
|
-
it('should be undefined with empty state', () => {
|
|
51
|
-
const { result } =
|
|
51
|
+
it('should be undefined with empty state', async () => {
|
|
52
|
+
const { result } = await renderDataCompose(() => {
|
|
52
53
|
return useQuery(ArticleWithSlug, { id: payloadSlug.id });
|
|
53
54
|
}, {});
|
|
54
55
|
// @ts-expect-error
|
|
55
|
-
result.
|
|
56
|
-
expect(result.
|
|
56
|
+
result.value?.doesnotexist;
|
|
57
|
+
expect(result.value).toBe(undefined);
|
|
57
58
|
});
|
|
58
59
|
|
|
59
|
-
it('All should be undefined with empty state', () => {
|
|
60
|
-
const { result } =
|
|
60
|
+
it('All should be undefined with empty state', async () => {
|
|
61
|
+
const { result } = await renderDataCompose(() => {
|
|
61
62
|
return useQuery(new schema.All(ArticleWithSlug));
|
|
62
63
|
}, {});
|
|
63
64
|
// @ts-expect-error
|
|
64
|
-
result.
|
|
65
|
-
expect(result.
|
|
65
|
+
result.value?.doesnotexist;
|
|
66
|
+
expect(result.value).toBe(undefined);
|
|
66
67
|
});
|
|
67
68
|
|
|
68
|
-
it('should fail on schema.Array', () => {
|
|
69
|
-
const { result } =
|
|
69
|
+
it('should fail on schema.Array', async () => {
|
|
70
|
+
const { result } = await renderDataCompose(() => {
|
|
70
71
|
// @ts-expect-error
|
|
71
72
|
return useQuery(new schema.Array(ArticleWithSlug));
|
|
72
73
|
}, {});
|
|
73
74
|
// @ts-expect-error
|
|
74
|
-
result.
|
|
75
|
-
expect(result.
|
|
75
|
+
result.value?.doesnotexist;
|
|
76
|
+
expect(result.value).toBe(undefined);
|
|
76
77
|
});
|
|
77
78
|
|
|
78
79
|
it('should find Entity by pk', async () => {
|
|
79
|
-
const { result } =
|
|
80
|
+
const { result } = await renderDataCompose(
|
|
80
81
|
() => {
|
|
81
82
|
return useQuery(ArticleWithSlug, { id: payloadSlug.id });
|
|
82
83
|
},
|
|
@@ -90,7 +91,7 @@ describe('vue useQuery()', () => {
|
|
|
90
91
|
],
|
|
91
92
|
},
|
|
92
93
|
);
|
|
93
|
-
expect(result.
|
|
94
|
+
expect(result.value).toEqual(ArticleWithSlug.fromJS(payloadSlug));
|
|
94
95
|
|
|
95
96
|
// @ts-expect-error
|
|
96
97
|
() => useQuery(ArticleWithSlug);
|
|
@@ -105,7 +106,7 @@ describe('vue useQuery()', () => {
|
|
|
105
106
|
});
|
|
106
107
|
|
|
107
108
|
it('should find Entity by slug', async () => {
|
|
108
|
-
const { result } =
|
|
109
|
+
const { result } = await renderDataCompose(
|
|
109
110
|
() => {
|
|
110
111
|
return useQuery(ArticleWithSlug, { slug: payloadSlug.slug });
|
|
111
112
|
},
|
|
@@ -119,10 +120,10 @@ describe('vue useQuery()', () => {
|
|
|
119
120
|
],
|
|
120
121
|
},
|
|
121
122
|
);
|
|
122
|
-
expect(result.
|
|
123
|
+
expect(result.value).toEqual(ArticleWithSlug.fromJS(payloadSlug));
|
|
123
124
|
});
|
|
124
125
|
|
|
125
|
-
it('should select Collections', () => {
|
|
126
|
+
it('should select Collections', async () => {
|
|
126
127
|
const initialFixtures = [
|
|
127
128
|
{
|
|
128
129
|
endpoint: ArticleResource.getList,
|
|
@@ -130,17 +131,17 @@ describe('vue useQuery()', () => {
|
|
|
130
131
|
response: nested,
|
|
131
132
|
},
|
|
132
133
|
];
|
|
133
|
-
const { result } =
|
|
134
|
+
const { result } = await renderDataCompose(
|
|
134
135
|
() => {
|
|
135
136
|
return useQuery(ArticleResource.getList.schema);
|
|
136
137
|
},
|
|
137
138
|
{ initialFixtures },
|
|
138
139
|
);
|
|
139
|
-
expect(result.
|
|
140
|
-
if (!result.
|
|
141
|
-
expect(result.
|
|
142
|
-
expect(result.
|
|
143
|
-
expect(result.
|
|
140
|
+
expect(result.value).toBeDefined();
|
|
141
|
+
if (!result.value) return;
|
|
142
|
+
expect(result.value.length).toBe(nested.length);
|
|
143
|
+
expect(result.value[0]).toBeInstanceOf(Article);
|
|
144
|
+
expect(result.value).toMatchSnapshot();
|
|
144
145
|
});
|
|
145
146
|
|
|
146
147
|
it('should update Collections when pushed', async () => {
|
|
@@ -159,25 +160,25 @@ describe('vue useQuery()', () => {
|
|
|
159
160
|
},
|
|
160
161
|
},
|
|
161
162
|
];
|
|
162
|
-
const { result, controller, waitForNextUpdate } =
|
|
163
|
+
const { result, controller, waitForNextUpdate } = await renderDataCompose(
|
|
163
164
|
() => {
|
|
164
165
|
return useQuery(ArticleResource.getList.schema, {});
|
|
165
166
|
},
|
|
166
167
|
{ initialFixtures, resolverFixtures },
|
|
167
168
|
);
|
|
168
|
-
expect(result.
|
|
169
|
-
if (!result.
|
|
170
|
-
expect(result.
|
|
169
|
+
expect(result.value).toBeDefined();
|
|
170
|
+
if (!result.value) return;
|
|
171
|
+
expect(result.value.length).toBe(nested.length);
|
|
171
172
|
await controller.fetch(ArticleResource.getList.push, {
|
|
172
173
|
id: 50,
|
|
173
174
|
title: 'newly added',
|
|
174
175
|
content: 'this one is pushed',
|
|
175
176
|
});
|
|
176
177
|
await waitForNextUpdate();
|
|
177
|
-
expect(result.
|
|
178
|
+
expect(result.value.length).toBe(nested.length + 1);
|
|
178
179
|
});
|
|
179
180
|
|
|
180
|
-
it('should retrieve a nested collection', () => {
|
|
181
|
+
it('should retrieve a nested collection', async () => {
|
|
181
182
|
class Todo extends IDEntity {
|
|
182
183
|
userId = 0;
|
|
183
184
|
title = '';
|
|
@@ -227,17 +228,17 @@ describe('vue useQuery()', () => {
|
|
|
227
228
|
},
|
|
228
229
|
},
|
|
229
230
|
];
|
|
230
|
-
const { result } =
|
|
231
|
+
const { result } = await renderDataCompose(
|
|
231
232
|
() => {
|
|
232
233
|
return useQuery(userTodos, { userId: '1' });
|
|
233
234
|
},
|
|
234
235
|
{ initialFixtures },
|
|
235
236
|
);
|
|
236
|
-
expect(result.
|
|
237
|
-
if (!result.
|
|
238
|
-
expect(result.
|
|
239
|
-
expect(result.
|
|
240
|
-
expect(result.
|
|
237
|
+
expect(result.value).toBeDefined();
|
|
238
|
+
if (!result.value) return;
|
|
239
|
+
expect(result.value.length).toBe(1);
|
|
240
|
+
expect(result.value[0]).toBeInstanceOf(Todo);
|
|
241
|
+
expect(result.value).toMatchSnapshot();
|
|
241
242
|
|
|
242
243
|
// @ts-expect-error
|
|
243
244
|
() => useQuery(userTodos, { userId: { hi: '5' } });
|
|
@@ -260,7 +261,7 @@ describe('vue useQuery()', () => {
|
|
|
260
261
|
schema: UnionSchema,
|
|
261
262
|
});
|
|
262
263
|
|
|
263
|
-
const { result } =
|
|
264
|
+
const { result } = await renderDataCompose(
|
|
264
265
|
() => {
|
|
265
266
|
return useQuery(UnionResource.get.schema, { id: '5', type: 'first' });
|
|
266
267
|
},
|
|
@@ -277,13 +278,13 @@ describe('vue useQuery()', () => {
|
|
|
277
278
|
],
|
|
278
279
|
},
|
|
279
280
|
);
|
|
280
|
-
expect(result.
|
|
281
|
-
if (!result.
|
|
281
|
+
expect(result.value).toBeDefined();
|
|
282
|
+
if (!result.value) {
|
|
282
283
|
warnSpy.mockRestore();
|
|
283
284
|
return;
|
|
284
285
|
}
|
|
285
|
-
expect(result.
|
|
286
|
-
expect(result.
|
|
286
|
+
expect(result.value.type).toBe('first');
|
|
287
|
+
expect(result.value).toBeInstanceOf(FirstUnion);
|
|
287
288
|
|
|
288
289
|
// these are the 'fallback case' where it cannot determine type discriminator, so just enumerates
|
|
289
290
|
() => useQuery(UnionResource.get.schema, { id: '5' });
|
|
@@ -312,7 +313,7 @@ describe('vue useQuery()', () => {
|
|
|
312
313
|
schema: UnionSchema,
|
|
313
314
|
});
|
|
314
315
|
|
|
315
|
-
const { result, controller, waitForNextUpdate } =
|
|
316
|
+
const { result, controller, waitForNextUpdate } = await renderDataCompose(
|
|
316
317
|
() => {
|
|
317
318
|
return useQuery(UnionResource.getList.schema);
|
|
318
319
|
},
|
|
@@ -340,12 +341,12 @@ describe('vue useQuery()', () => {
|
|
|
340
341
|
],
|
|
341
342
|
},
|
|
342
343
|
);
|
|
343
|
-
expect(result.
|
|
344
|
-
if (!result.
|
|
345
|
-
expect(result.
|
|
346
|
-
expect(result.
|
|
347
|
-
expect(result.
|
|
348
|
-
expect(result.
|
|
344
|
+
expect(result.value).toBeDefined();
|
|
345
|
+
if (!result.value) return;
|
|
346
|
+
expect(result.value[0]).toBeNull();
|
|
347
|
+
expect(result.value[1]).toBeInstanceOf(FirstUnion);
|
|
348
|
+
expect(result.value[2]).not.toBeInstanceOf(FirstUnion);
|
|
349
|
+
expect(result.value[3]).not.toBeInstanceOf(FirstUnion);
|
|
349
350
|
expect(warnSpy.mock.calls).toMatchSnapshot();
|
|
350
351
|
|
|
351
352
|
await controller.fetch(UnionResource.getList.push, {
|
|
@@ -354,8 +355,289 @@ describe('vue useQuery()', () => {
|
|
|
354
355
|
id: '100',
|
|
355
356
|
});
|
|
356
357
|
await waitForNextUpdate();
|
|
357
|
-
expect(result.
|
|
358
|
-
expect(result.
|
|
358
|
+
expect(result.value[4]).toBeInstanceOf(SecondUnion);
|
|
359
|
+
expect(result.value).toMatchSnapshot();
|
|
359
360
|
warnSpy.mockRestore();
|
|
360
361
|
});
|
|
362
|
+
|
|
363
|
+
describe('changing args', () => {
|
|
364
|
+
it('should update result when Entity args change', async () => {
|
|
365
|
+
const payload1 = {
|
|
366
|
+
id: 1,
|
|
367
|
+
title: 'First Article',
|
|
368
|
+
slug: 'first-article',
|
|
369
|
+
content: 'content 1',
|
|
370
|
+
tags: ['tag1'],
|
|
371
|
+
};
|
|
372
|
+
const payload2 = {
|
|
373
|
+
id: 2,
|
|
374
|
+
title: 'Second Article',
|
|
375
|
+
slug: 'second-article',
|
|
376
|
+
content: 'content 2',
|
|
377
|
+
tags: ['tag2'],
|
|
378
|
+
};
|
|
379
|
+
|
|
380
|
+
const props = reactive({ id: 1 });
|
|
381
|
+
const { result } = await renderDataCompose(
|
|
382
|
+
() => {
|
|
383
|
+
return useQuery(
|
|
384
|
+
ArticleWithSlug,
|
|
385
|
+
computed(() => ({ id: props.id })),
|
|
386
|
+
);
|
|
387
|
+
},
|
|
388
|
+
{
|
|
389
|
+
initialFixtures: [
|
|
390
|
+
{
|
|
391
|
+
endpoint: ArticleSlugResource.get,
|
|
392
|
+
args: [{ id: 1 }],
|
|
393
|
+
response: payload1,
|
|
394
|
+
},
|
|
395
|
+
{
|
|
396
|
+
endpoint: ArticleSlugResource.get,
|
|
397
|
+
args: [{ id: 2 }],
|
|
398
|
+
response: payload2,
|
|
399
|
+
},
|
|
400
|
+
],
|
|
401
|
+
},
|
|
402
|
+
);
|
|
403
|
+
|
|
404
|
+
expect(result.value).toEqual(ArticleWithSlug.fromJS(payload1));
|
|
405
|
+
expect(result.value?.id).toBe(1);
|
|
406
|
+
expect(result.value?.title).toBe('First Article');
|
|
407
|
+
|
|
408
|
+
props.id = 2;
|
|
409
|
+
expect(result.value).toEqual(ArticleWithSlug.fromJS(payload2));
|
|
410
|
+
expect(result.value?.id).toBe(2);
|
|
411
|
+
expect(result.value?.title).toBe('Second Article');
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
it('should update result when Entity args change from slug to id', async () => {
|
|
415
|
+
const props = reactive({
|
|
416
|
+
id: 5 as number | undefined,
|
|
417
|
+
slug: undefined as string | undefined,
|
|
418
|
+
});
|
|
419
|
+
const { result } = await renderDataCompose(
|
|
420
|
+
() => {
|
|
421
|
+
return useQuery(
|
|
422
|
+
ArticleWithSlug,
|
|
423
|
+
computed(() => props as any),
|
|
424
|
+
);
|
|
425
|
+
},
|
|
426
|
+
{
|
|
427
|
+
initialFixtures: [
|
|
428
|
+
{
|
|
429
|
+
endpoint: ArticleSlugResource.get,
|
|
430
|
+
args: [{ id: 5 }],
|
|
431
|
+
response: payloadSlug,
|
|
432
|
+
},
|
|
433
|
+
],
|
|
434
|
+
},
|
|
435
|
+
);
|
|
436
|
+
|
|
437
|
+
expect(result.value).toEqual(ArticleWithSlug.fromJS(payloadSlug));
|
|
438
|
+
expect(result.value?.id).toBe(5);
|
|
439
|
+
|
|
440
|
+
props.id = undefined;
|
|
441
|
+
props.slug = payloadSlug.slug;
|
|
442
|
+
expect(result.value).toEqual(ArticleWithSlug.fromJS(payloadSlug));
|
|
443
|
+
expect(result.value?.slug).toBe(payloadSlug.slug);
|
|
444
|
+
});
|
|
445
|
+
|
|
446
|
+
it('should return undefined when changing to non-existent entity', async () => {
|
|
447
|
+
const payload1 = {
|
|
448
|
+
id: 1,
|
|
449
|
+
title: 'First Article',
|
|
450
|
+
slug: 'first-article',
|
|
451
|
+
content: 'content 1',
|
|
452
|
+
tags: ['tag1'],
|
|
453
|
+
};
|
|
454
|
+
|
|
455
|
+
const props = reactive({ id: 1 });
|
|
456
|
+
const { result } = await renderDataCompose(
|
|
457
|
+
() => {
|
|
458
|
+
return useQuery(
|
|
459
|
+
ArticleWithSlug,
|
|
460
|
+
computed(() => ({ id: props.id })),
|
|
461
|
+
);
|
|
462
|
+
},
|
|
463
|
+
{
|
|
464
|
+
initialFixtures: [
|
|
465
|
+
{
|
|
466
|
+
endpoint: ArticleSlugResource.get,
|
|
467
|
+
args: [{ id: 1 }],
|
|
468
|
+
response: payload1,
|
|
469
|
+
},
|
|
470
|
+
],
|
|
471
|
+
},
|
|
472
|
+
);
|
|
473
|
+
|
|
474
|
+
expect(result.value).toEqual(ArticleWithSlug.fromJS(payload1));
|
|
475
|
+
expect(result.value?.id).toBe(1);
|
|
476
|
+
|
|
477
|
+
props.id = 999;
|
|
478
|
+
expect(result.value).toBe(undefined);
|
|
479
|
+
});
|
|
480
|
+
|
|
481
|
+
it('should update nested collection when args change', async () => {
|
|
482
|
+
class Todo extends IDEntity {
|
|
483
|
+
userId = 0;
|
|
484
|
+
title = '';
|
|
485
|
+
completed = false;
|
|
486
|
+
|
|
487
|
+
static key = 'Todo';
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
class User extends IDEntity {
|
|
491
|
+
name = '';
|
|
492
|
+
username = '';
|
|
493
|
+
email = '';
|
|
494
|
+
todos: Todo[] = [];
|
|
495
|
+
|
|
496
|
+
static key = 'User';
|
|
497
|
+
static schema = {
|
|
498
|
+
todos: new schema.Collection(new schema.Array(Todo), {
|
|
499
|
+
nestKey: parent => ({
|
|
500
|
+
userId: parent.id,
|
|
501
|
+
}),
|
|
502
|
+
}),
|
|
503
|
+
};
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
const userTodos = new schema.Collection(new schema.Array(Todo), {
|
|
507
|
+
argsKey: ({ userId }: { userId: string }) => ({
|
|
508
|
+
userId,
|
|
509
|
+
}),
|
|
510
|
+
});
|
|
511
|
+
|
|
512
|
+
const UserResource = resource({ schema: User, path: '/users/:id' });
|
|
513
|
+
|
|
514
|
+
const props = reactive({ userId: '1' });
|
|
515
|
+
const { result } = await renderDataCompose(
|
|
516
|
+
() => {
|
|
517
|
+
return useQuery(
|
|
518
|
+
userTodos,
|
|
519
|
+
computed(() => ({ userId: props.userId })),
|
|
520
|
+
);
|
|
521
|
+
},
|
|
522
|
+
{
|
|
523
|
+
initialFixtures: [
|
|
524
|
+
{
|
|
525
|
+
endpoint: UserResource.get,
|
|
526
|
+
args: [{ id: '1' }],
|
|
527
|
+
response: {
|
|
528
|
+
id: '1',
|
|
529
|
+
todos: [
|
|
530
|
+
{
|
|
531
|
+
id: '5',
|
|
532
|
+
title: 'finish collections',
|
|
533
|
+
userId: '1',
|
|
534
|
+
},
|
|
535
|
+
{
|
|
536
|
+
id: '6',
|
|
537
|
+
title: 'write tests',
|
|
538
|
+
userId: '1',
|
|
539
|
+
},
|
|
540
|
+
],
|
|
541
|
+
username: 'bob',
|
|
542
|
+
},
|
|
543
|
+
},
|
|
544
|
+
{
|
|
545
|
+
endpoint: UserResource.get,
|
|
546
|
+
args: [{ id: '2' }],
|
|
547
|
+
response: {
|
|
548
|
+
id: '2',
|
|
549
|
+
todos: [
|
|
550
|
+
{
|
|
551
|
+
id: '7',
|
|
552
|
+
title: 'review code',
|
|
553
|
+
userId: '2',
|
|
554
|
+
},
|
|
555
|
+
],
|
|
556
|
+
username: 'alice',
|
|
557
|
+
},
|
|
558
|
+
},
|
|
559
|
+
],
|
|
560
|
+
},
|
|
561
|
+
);
|
|
562
|
+
|
|
563
|
+
expect(result.value).toBeDefined();
|
|
564
|
+
expect(result.value?.length).toBe(2);
|
|
565
|
+
expect((result.value?.[0] as any)?.title).toBe('finish collections');
|
|
566
|
+
expect((result.value?.[1] as any)?.title).toBe('write tests');
|
|
567
|
+
|
|
568
|
+
props.userId = '2';
|
|
569
|
+
expect(result.value).toBeDefined();
|
|
570
|
+
expect(result.value?.length).toBe(1);
|
|
571
|
+
expect((result.value?.[0] as any)?.title).toBe('review code');
|
|
572
|
+
});
|
|
573
|
+
|
|
574
|
+
it('should return undefined when changing collection args to non-existent collection', async () => {
|
|
575
|
+
class Todo extends IDEntity {
|
|
576
|
+
userId = 0;
|
|
577
|
+
title = '';
|
|
578
|
+
completed = false;
|
|
579
|
+
|
|
580
|
+
static key = 'Todo';
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
class User extends IDEntity {
|
|
584
|
+
name = '';
|
|
585
|
+
username = '';
|
|
586
|
+
email = '';
|
|
587
|
+
todos: Todo[] = [];
|
|
588
|
+
|
|
589
|
+
static key = 'User';
|
|
590
|
+
static schema = {
|
|
591
|
+
todos: new schema.Collection(new schema.Array(Todo), {
|
|
592
|
+
nestKey: parent => ({
|
|
593
|
+
userId: parent.id,
|
|
594
|
+
}),
|
|
595
|
+
}),
|
|
596
|
+
};
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
const userTodos = new schema.Collection(new schema.Array(Todo), {
|
|
600
|
+
argsKey: ({ userId }: { userId: string }) => ({
|
|
601
|
+
userId,
|
|
602
|
+
}),
|
|
603
|
+
});
|
|
604
|
+
|
|
605
|
+
const UserResource = resource({ schema: User, path: '/users/:id' });
|
|
606
|
+
|
|
607
|
+
const props = reactive({ userId: '1' });
|
|
608
|
+
const { result } = await renderDataCompose(
|
|
609
|
+
() => {
|
|
610
|
+
return useQuery(
|
|
611
|
+
userTodos,
|
|
612
|
+
computed(() => ({ userId: props.userId })),
|
|
613
|
+
);
|
|
614
|
+
},
|
|
615
|
+
{
|
|
616
|
+
initialFixtures: [
|
|
617
|
+
{
|
|
618
|
+
endpoint: UserResource.get,
|
|
619
|
+
args: [{ id: '1' }],
|
|
620
|
+
response: {
|
|
621
|
+
id: '1',
|
|
622
|
+
todos: [
|
|
623
|
+
{
|
|
624
|
+
id: '5',
|
|
625
|
+
title: 'finish collections',
|
|
626
|
+
userId: '1',
|
|
627
|
+
},
|
|
628
|
+
],
|
|
629
|
+
username: 'bob',
|
|
630
|
+
},
|
|
631
|
+
},
|
|
632
|
+
],
|
|
633
|
+
},
|
|
634
|
+
);
|
|
635
|
+
|
|
636
|
+
expect(result.value).toBeDefined();
|
|
637
|
+
expect(result.value?.length).toBe(1);
|
|
638
|
+
|
|
639
|
+
props.userId = '999';
|
|
640
|
+
expect(result.value).toBe(undefined);
|
|
641
|
+
});
|
|
642
|
+
});
|
|
361
643
|
});
|