@data-client/vue 0.18.0 → 0.18.2
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/CHANGELOG.md +381 -1
- package/README.md +86 -41
- package/dist/index.js +223 -158
- package/dist/index.umd.min.js +1 -1
- package/lib/consumers/refetchTriggers.d.ts +12 -0
- package/lib/consumers/refetchTriggers.d.ts.map +1 -0
- package/lib/consumers/refetchTriggers.js +9 -0
- package/lib/consumers/useCache.d.ts +1 -1
- package/lib/consumers/useCache.d.ts.map +1 -1
- package/lib/consumers/useCache.js +3 -53
- package/lib/consumers/useCacheResponse.d.ts +20 -0
- package/lib/consumers/useCacheResponse.d.ts.map +1 -0
- package/lib/consumers/useCacheResponse.js +55 -0
- package/lib/consumers/useCountRef.d.ts +7 -0
- package/lib/consumers/useCountRef.d.ts.map +1 -0
- package/lib/consumers/useCountRef.js +10 -0
- package/lib/consumers/useDLE.d.ts +1 -1
- package/lib/consumers/useDLE.d.ts.map +1 -1
- package/lib/consumers/useDLE.js +16 -62
- package/lib/consumers/useDebounce.d.ts +1 -1
- package/lib/consumers/useDebounce.d.ts.map +1 -1
- package/lib/consumers/useDebounce.js +1 -2
- package/lib/consumers/useExpired.d.ts +12 -0
- package/lib/consumers/useExpired.d.ts.map +1 -0
- package/lib/consumers/useExpired.js +19 -0
- package/lib/consumers/useFetch.d.ts +8 -11
- package/lib/consumers/useFetch.d.ts.map +1 -1
- package/lib/consumers/useFetch.js +13 -24
- package/lib/consumers/useLive.js +1 -1
- package/lib/consumers/useLoading.d.ts +1 -1
- package/lib/consumers/useLoading.d.ts.map +1 -1
- package/lib/consumers/useLoading.js +1 -2
- package/lib/consumers/useQuery.d.ts +1 -1
- package/lib/consumers/useQuery.d.ts.map +1 -1
- package/lib/consumers/useQuery.js +6 -12
- package/lib/consumers/useResponseMeta.d.ts +23 -0
- package/lib/consumers/useResponseMeta.d.ts.map +1 -0
- package/lib/consumers/useResponseMeta.js +36 -0
- package/lib/consumers/useSubscription.js +4 -4
- package/lib/consumers/useSuspense.d.ts +1 -1
- package/lib/consumers/useSuspense.d.ts.map +1 -1
- package/lib/consumers/useSuspense.js +58 -34
- package/lib/context.d.ts +1 -1
- package/lib/context.d.ts.map +1 -1
- package/lib/context.js +3 -3
- package/lib/index.d.ts +2 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +4 -2
- package/lib/internal.d.ts +6 -0
- package/lib/internal.d.ts.map +1 -0
- package/lib/internal.js +6 -0
- package/lib/providers/DataClientPlugin.d.ts +1 -1
- package/lib/providers/DataClientPlugin.d.ts.map +1 -1
- package/lib/providers/DataClientPlugin.js +16 -4
- package/lib/providers/createDataClient.d.ts +10 -4
- package/lib/providers/createDataClient.d.ts.map +1 -1
- package/lib/providers/createDataClient.js +11 -5
- package/lib/providers/getDefaultManagers.d.ts.map +1 -1
- package/lib/providers/getDefaultManagers.js +1 -1
- package/lib/providers/index.d.ts +2 -1
- package/lib/providers/index.d.ts.map +1 -1
- package/lib/providers/index.js +1 -1
- package/lib/test/MockPlugin.d.ts +1 -1
- package/lib/test/MockPlugin.d.ts.map +1 -1
- package/lib/test/MockPlugin.js +1 -1
- package/lib/test/index.d.ts +2 -1
- package/lib/test/index.d.ts.map +1 -1
- package/lib/test/index.js +1 -1
- package/lib/test/mountDataClient.d.ts +5 -3
- package/lib/test/mountDataClient.d.ts.map +1 -1
- package/lib/test/mountDataClient.js +15 -10
- package/lib/test/renderDataCompose.d.ts +10 -4
- package/lib/test/renderDataCompose.d.ts.map +1 -1
- package/lib/test/renderDataCompose.js +2 -3
- package/package.json +21 -6
- package/src/__tests__/__snapshots__/context.test.ts.snap +2 -2
- package/src/__tests__/context.test.ts +1 -1
- package/src/__tests__/integration-garbage-collection.web.ts +80 -4
- package/src/__tests__/useCache.web.ts +160 -93
- package/src/__tests__/useDLE.web.ts +204 -96
- package/src/__tests__/useFetch.web.ts +100 -37
- package/src/__tests__/useQuery.web.ts +60 -50
- package/src/__tests__/useSubscription.web.ts +74 -75
- package/src/__tests__/useSuspense.web.ts +455 -46
- package/src/consumers/refetchTriggers.ts +22 -0
- package/src/consumers/useCache.ts +7 -70
- package/src/consumers/useCacheResponse.ts +56 -0
- package/src/consumers/useCountRef.ts +15 -0
- package/src/consumers/useDLE.ts +24 -83
- package/src/consumers/useDebounce.ts +2 -1
- package/src/consumers/useExpired.ts +26 -0
- package/src/consumers/useFetch.ts +34 -49
- package/src/consumers/useLive.ts +1 -1
- package/src/consumers/useLoading.ts +2 -1
- package/src/consumers/useQuery.ts +10 -13
- package/src/consumers/useResponseMeta.ts +41 -0
- package/src/consumers/useSubscription.ts +3 -3
- package/src/consumers/useSuspense.ts +66 -58
- package/src/context.ts +4 -3
- package/src/index.ts +7 -1
- package/src/internal.ts +12 -0
- package/src/providers/DataClientPlugin.ts +17 -3
- package/src/providers/__tests__/DataClientPlugin.test.ts +40 -1
- package/src/providers/createDataClient.ts +15 -5
- package/src/providers/getDefaultManagers.ts +2 -6
- package/src/providers/index.ts +2 -5
- package/src/test/MockPlugin.ts +3 -3
- package/src/test/README.md +10 -304
- package/src/test/__tests__/mountDataClient.test.ts +38 -0
- package/src/test/index.ts +2 -1
- package/src/test/mountDataClient.ts +22 -17
- package/src/test/renderDataCompose.ts +11 -7
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { schema, All, Collection } from '@data-client/endpoint';
|
|
2
2
|
import { resource } from '@data-client/rest';
|
|
3
3
|
import { reactive, computed } from 'vue';
|
|
4
|
+
import type { MaybeRefOrGetter } from 'vue';
|
|
4
5
|
|
|
5
6
|
import {
|
|
6
7
|
ArticleWithSlug,
|
|
@@ -15,6 +16,12 @@ import {
|
|
|
15
16
|
import useQuery from '../consumers/useQuery';
|
|
16
17
|
import { renderDataCompose } from '../test';
|
|
17
18
|
|
|
19
|
+
// Each arg form a composable accepts that should re-evaluate reactively
|
|
20
|
+
const argForms: [string, <T>(fn: () => T) => MaybeRefOrGetter<T>][] = [
|
|
21
|
+
['computed', fn => computed(fn)],
|
|
22
|
+
['getter', fn => fn],
|
|
23
|
+
];
|
|
24
|
+
|
|
18
25
|
// Inline fixtures (duplicated from React tests to avoid cross-project imports)
|
|
19
26
|
const payloadSlug = {
|
|
20
27
|
id: 5,
|
|
@@ -168,7 +175,7 @@ describe('vue useQuery()', () => {
|
|
|
168
175
|
},
|
|
169
176
|
},
|
|
170
177
|
];
|
|
171
|
-
const { result, controller,
|
|
178
|
+
const { result, controller, allSettled } = await renderDataCompose(
|
|
172
179
|
() => {
|
|
173
180
|
return useQuery(ArticleResource.getList.schema, {});
|
|
174
181
|
},
|
|
@@ -182,7 +189,7 @@ describe('vue useQuery()', () => {
|
|
|
182
189
|
title: 'newly added',
|
|
183
190
|
content: 'this one is pushed',
|
|
184
191
|
});
|
|
185
|
-
await
|
|
192
|
+
await allSettled();
|
|
186
193
|
expect(result.value.length).toBe(nested.length + 1);
|
|
187
194
|
});
|
|
188
195
|
|
|
@@ -321,7 +328,7 @@ describe('vue useQuery()', () => {
|
|
|
321
328
|
schema: UnionSchema,
|
|
322
329
|
});
|
|
323
330
|
|
|
324
|
-
const { result, controller,
|
|
331
|
+
const { result, controller, allSettled } = await renderDataCompose(
|
|
325
332
|
() => {
|
|
326
333
|
return useQuery(UnionResource.getList.schema);
|
|
327
334
|
},
|
|
@@ -362,62 +369,65 @@ describe('vue useQuery()', () => {
|
|
|
362
369
|
type: 'second',
|
|
363
370
|
id: '100',
|
|
364
371
|
});
|
|
365
|
-
await
|
|
372
|
+
await allSettled();
|
|
366
373
|
expect(result.value[4]).toBeInstanceOf(SecondUnion);
|
|
367
374
|
expect(result.value).toMatchSnapshot();
|
|
368
375
|
warnSpy.mockRestore();
|
|
369
376
|
});
|
|
370
377
|
|
|
371
378
|
describe('changing args', () => {
|
|
372
|
-
it(
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
379
|
+
it.each(argForms)(
|
|
380
|
+
'should update result when Entity args change (%s args)',
|
|
381
|
+
async (_, toArg) => {
|
|
382
|
+
const payload1 = {
|
|
383
|
+
id: 1,
|
|
384
|
+
title: 'First Article',
|
|
385
|
+
slug: 'first-article',
|
|
386
|
+
content: 'content 1',
|
|
387
|
+
tags: ['tag1'],
|
|
388
|
+
};
|
|
389
|
+
const payload2 = {
|
|
390
|
+
id: 2,
|
|
391
|
+
title: 'Second Article',
|
|
392
|
+
slug: 'second-article',
|
|
393
|
+
content: 'content 2',
|
|
394
|
+
tags: ['tag2'],
|
|
395
|
+
};
|
|
387
396
|
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
397
|
+
const props = reactive({ id: 1 });
|
|
398
|
+
const { result } = await renderDataCompose(
|
|
399
|
+
() => {
|
|
400
|
+
return useQuery(
|
|
401
|
+
ArticleWithSlug,
|
|
402
|
+
toArg(() => ({ id: props.id })),
|
|
403
|
+
);
|
|
404
|
+
},
|
|
405
|
+
{
|
|
406
|
+
initialFixtures: [
|
|
407
|
+
{
|
|
408
|
+
endpoint: ArticleSlugResource.get,
|
|
409
|
+
args: [{ id: 1 }],
|
|
410
|
+
response: payload1,
|
|
411
|
+
},
|
|
412
|
+
{
|
|
413
|
+
endpoint: ArticleSlugResource.get,
|
|
414
|
+
args: [{ id: 2 }],
|
|
415
|
+
response: payload2,
|
|
416
|
+
},
|
|
417
|
+
],
|
|
418
|
+
},
|
|
419
|
+
);
|
|
411
420
|
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
421
|
+
expect(result.value).toEqual(ArticleWithSlug.fromJS(payload1));
|
|
422
|
+
expect(result.value?.id).toBe(1);
|
|
423
|
+
expect(result.value?.title).toBe('First Article');
|
|
415
424
|
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
425
|
+
props.id = 2;
|
|
426
|
+
expect(result.value).toEqual(ArticleWithSlug.fromJS(payload2));
|
|
427
|
+
expect(result.value?.id).toBe(2);
|
|
428
|
+
expect(result.value?.title).toBe('Second Article');
|
|
429
|
+
},
|
|
430
|
+
);
|
|
421
431
|
|
|
422
432
|
it('should update result when Entity args change from slug to id', async () => {
|
|
423
433
|
const props = reactive({
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { VueWrapper } from '@vue/test-utils';
|
|
2
2
|
import { computed, defineComponent, h, nextTick, reactive } from 'vue';
|
|
3
|
+
import type { MaybeRefOrGetter } from 'vue';
|
|
3
4
|
|
|
4
5
|
// Endpoints/entities from React subscriptions test
|
|
5
6
|
import { PollingArticleResource } from '../../../../__tests__/new';
|
|
@@ -7,6 +8,12 @@ import useSubscription from '../consumers/useSubscription';
|
|
|
7
8
|
import useSuspense from '../consumers/useSuspense';
|
|
8
9
|
import { renderDataCompose, mountDataClient } from '../test';
|
|
9
10
|
|
|
11
|
+
// Each arg form a composable accepts that should re-evaluate reactively
|
|
12
|
+
const argForms: [string, <T>(fn: () => T) => MaybeRefOrGetter<T>][] = [
|
|
13
|
+
['computed', fn => computed(fn)],
|
|
14
|
+
['getter', fn => fn],
|
|
15
|
+
];
|
|
16
|
+
|
|
10
17
|
describe('vue useSubscription()', () => {
|
|
11
18
|
async function flushUntilWithFakeTimers(
|
|
12
19
|
wrapper: VueWrapper<any>,
|
|
@@ -45,30 +52,28 @@ describe('vue useSubscription()', () => {
|
|
|
45
52
|
const responseMock = jest.fn(() => payload);
|
|
46
53
|
const propsRef = reactive({ active: true });
|
|
47
54
|
|
|
48
|
-
const { result,
|
|
49
|
-
|
|
50
|
-
(
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
);
|
|
55
|
+
const { result, allSettled, cleanup } = await renderDataCompose(
|
|
56
|
+
({ active }: { active: boolean }) => {
|
|
57
|
+
const args = computed(() => (active ? { id: payload.id } : null));
|
|
58
|
+
useSubscription(PollingArticleResource.get, args);
|
|
59
|
+
return useSuspense(PollingArticleResource.get, {
|
|
60
|
+
id: payload.id,
|
|
61
|
+
});
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
props: propsRef,
|
|
65
|
+
resolverFixtures: [
|
|
66
|
+
{
|
|
67
|
+
endpoint: PollingArticleResource.get,
|
|
68
|
+
response: responseMock,
|
|
69
|
+
},
|
|
70
|
+
],
|
|
71
|
+
},
|
|
72
|
+
);
|
|
67
73
|
|
|
68
74
|
// Wait for initial render
|
|
69
75
|
jest.advanceTimersByTime(frequency);
|
|
70
76
|
await allSettled();
|
|
71
|
-
await waitForNextUpdate();
|
|
72
77
|
|
|
73
78
|
// Verify initial values
|
|
74
79
|
const initialArticleRef = await result;
|
|
@@ -85,7 +90,6 @@ describe('vue useSubscription()', () => {
|
|
|
85
90
|
// Advance time to trigger another poll
|
|
86
91
|
jest.advanceTimersByTime(frequency);
|
|
87
92
|
await allSettled();
|
|
88
|
-
await nextTick();
|
|
89
93
|
|
|
90
94
|
// Verify the article was updated
|
|
91
95
|
const updatedArticleRef = await result;
|
|
@@ -96,20 +100,19 @@ describe('vue useSubscription()', () => {
|
|
|
96
100
|
cleanup();
|
|
97
101
|
});
|
|
98
102
|
|
|
99
|
-
it(
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
+
it.each(argForms)(
|
|
104
|
+
'should not receive updates after unsubscribing by setting active to null (%s args)',
|
|
105
|
+
async (_, toArg) => {
|
|
106
|
+
jest.useFakeTimers();
|
|
107
|
+
const frequency = PollingArticleResource.get.pollFrequency as number;
|
|
108
|
+
expect(frequency).toBe(5000);
|
|
103
109
|
|
|
104
|
-
|
|
105
|
-
|
|
110
|
+
const responseMock = jest.fn(() => payload);
|
|
111
|
+
const propsRef = reactive({ active: true });
|
|
106
112
|
|
|
107
|
-
|
|
108
|
-
await renderDataCompose(
|
|
113
|
+
const { result, allSettled, cleanup } = await renderDataCompose(
|
|
109
114
|
(props: { active: boolean }) => {
|
|
110
|
-
const args =
|
|
111
|
-
props.active ? { id: payload.id } : null,
|
|
112
|
-
);
|
|
115
|
+
const args = toArg(() => (props.active ? { id: payload.id } : null));
|
|
113
116
|
useSubscription(PollingArticleResource.get, args);
|
|
114
117
|
return useSuspense(PollingArticleResource.get, {
|
|
115
118
|
id: payload.id,
|
|
@@ -126,56 +129,54 @@ describe('vue useSubscription()', () => {
|
|
|
126
129
|
},
|
|
127
130
|
);
|
|
128
131
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
await waitForNextUpdate();
|
|
132
|
+
// Wait for initial render
|
|
133
|
+
jest.advanceTimersByTime(frequency);
|
|
134
|
+
await allSettled();
|
|
133
135
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
136
|
+
// Verify initial values
|
|
137
|
+
const initialArticleRef = await result;
|
|
138
|
+
expect(initialArticleRef!.value!.title).toBe(payload.title);
|
|
139
|
+
expect(responseMock).toHaveBeenCalledTimes(1);
|
|
138
140
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
141
|
+
// Change the mock response
|
|
142
|
+
responseMock.mockReturnValue({
|
|
143
|
+
...payload,
|
|
144
|
+
title: 'after first poll',
|
|
145
|
+
});
|
|
144
146
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
await nextTick();
|
|
147
|
+
// Advance time to trigger another poll
|
|
148
|
+
jest.advanceTimersByTime(frequency);
|
|
149
|
+
await allSettled();
|
|
149
150
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
151
|
+
// Verify the article was updated
|
|
152
|
+
const updatedArticleRef = await result;
|
|
153
|
+
expect(updatedArticleRef!.value!.title).toBe('after first poll');
|
|
154
|
+
expect(responseMock).toHaveBeenCalledTimes(2);
|
|
154
155
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
156
|
+
// Now unsubscribe by setting active to false
|
|
157
|
+
propsRef.active = false;
|
|
158
|
+
await nextTick();
|
|
158
159
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
160
|
+
// Change the mock response again
|
|
161
|
+
responseMock.mockReturnValue({
|
|
162
|
+
...payload,
|
|
163
|
+
title: 'should not see this',
|
|
164
|
+
});
|
|
164
165
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
await nextTick();
|
|
166
|
+
// Advance time - subscription should not trigger
|
|
167
|
+
jest.advanceTimersByTime(frequency);
|
|
168
|
+
await allSettled();
|
|
169
169
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
170
|
+
// Verify the article was NOT updated (still has old value)
|
|
171
|
+
const finalArticleRef = await result;
|
|
172
|
+
expect(finalArticleRef!.value!.title).toBe('after first poll');
|
|
173
|
+
// Should still be 2 calls, no new poll happened
|
|
174
|
+
expect(responseMock).toHaveBeenCalledTimes(2);
|
|
175
175
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
176
|
+
jest.useRealTimers();
|
|
177
|
+
cleanup();
|
|
178
|
+
},
|
|
179
|
+
);
|
|
179
180
|
|
|
180
181
|
it('should unsubscribe when subscribed component unmounts', async () => {
|
|
181
182
|
jest.useFakeTimers();
|
|
@@ -251,7 +252,6 @@ describe('vue useSubscription()', () => {
|
|
|
251
252
|
// Advance time to trigger poll
|
|
252
253
|
jest.advanceTimersByTime(frequency);
|
|
253
254
|
await allSettled();
|
|
254
|
-
await nextTick();
|
|
255
255
|
|
|
256
256
|
// Verify the article was updated
|
|
257
257
|
expect(wrapper.find('h3').text()).toBe('first update');
|
|
@@ -270,7 +270,6 @@ describe('vue useSubscription()', () => {
|
|
|
270
270
|
// Advance time - subscription should not trigger
|
|
271
271
|
jest.advanceTimersByTime(frequency);
|
|
272
272
|
await allSettled();
|
|
273
|
-
await nextTick();
|
|
274
273
|
|
|
275
274
|
// The reading component should still show old data since no subscription is active
|
|
276
275
|
expect(wrapper.find('h3').text()).toBe('first update');
|