@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,30 +1,15 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import { defineComponent, h, nextTick, Suspense, inject } from 'vue';
|
|
1
|
+
import { VueWrapper } from '@vue/test-utils';
|
|
2
|
+
import { computed, defineComponent, h, nextTick, reactive } from 'vue';
|
|
4
3
|
|
|
5
4
|
// Endpoints/entities from React subscriptions test
|
|
6
|
-
import {
|
|
7
|
-
PollingArticleResource,
|
|
8
|
-
ArticleResource,
|
|
9
|
-
} from '../../../../__tests__/new';
|
|
5
|
+
import { PollingArticleResource } from '../../../../__tests__/new';
|
|
10
6
|
import useSubscription from '../consumers/useSubscription';
|
|
11
7
|
import useSuspense from '../consumers/useSuspense';
|
|
12
|
-
import {
|
|
13
|
-
import { DataClientPlugin } from '../providers/DataClientPlugin';
|
|
8
|
+
import { renderDataCompose, mountDataClient } from '../test';
|
|
14
9
|
|
|
15
10
|
describe('vue useSubscription()', () => {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
title: 'hi ho',
|
|
19
|
-
content: 'whatever',
|
|
20
|
-
tags: ['a', 'best', 'react'],
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
// Mutable payload to simulate server-side updates with polling
|
|
24
|
-
let currentPollingPayload: typeof payload = { ...payload };
|
|
25
|
-
|
|
26
|
-
async function flushUntil(
|
|
27
|
-
wrapper: any,
|
|
11
|
+
async function flushUntilWithFakeTimers(
|
|
12
|
+
wrapper: VueWrapper<any>,
|
|
28
13
|
predicate: () => boolean,
|
|
29
14
|
tries = 100,
|
|
30
15
|
) {
|
|
@@ -32,133 +17,262 @@ describe('vue useSubscription()', () => {
|
|
|
32
17
|
if (predicate()) return;
|
|
33
18
|
await Promise.resolve();
|
|
34
19
|
await nextTick();
|
|
35
|
-
|
|
20
|
+
// Don't use setTimeout with fake timers
|
|
36
21
|
}
|
|
37
22
|
}
|
|
38
23
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
'Access-Control-Allow-Headers': 'Access-Token',
|
|
46
|
-
'Content-Type': 'application/json',
|
|
47
|
-
})
|
|
48
|
-
.options(/.*/)
|
|
49
|
-
.reply(200)
|
|
50
|
-
// ArticleResource and PollingArticleResource both hit /article/:id
|
|
51
|
-
.get(`/article/${payload.id}`)
|
|
52
|
-
.reply(200, () => currentPollingPayload)
|
|
53
|
-
.put(`/article/${payload.id}`)
|
|
54
|
-
.reply(200, (uri, requestBody: any) => ({
|
|
55
|
-
...currentPollingPayload,
|
|
56
|
-
...requestBody,
|
|
57
|
-
}));
|
|
58
|
-
});
|
|
24
|
+
const payload = {
|
|
25
|
+
id: 5,
|
|
26
|
+
title: 'hi ho',
|
|
27
|
+
content: 'whatever',
|
|
28
|
+
tags: ['a', 'best', 'react'],
|
|
29
|
+
};
|
|
59
30
|
|
|
60
|
-
|
|
61
|
-
|
|
31
|
+
afterEach(() => {
|
|
32
|
+
jest.useRealTimers();
|
|
62
33
|
});
|
|
63
34
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
expose({ controller });
|
|
69
|
-
return () =>
|
|
70
|
-
h(
|
|
71
|
-
Suspense,
|
|
72
|
-
{},
|
|
73
|
-
{
|
|
74
|
-
default: () => (slots.default ? slots.default() : null),
|
|
75
|
-
fallback: () => h('div', { class: 'fallback' }, 'Loading'),
|
|
76
|
-
},
|
|
77
|
-
);
|
|
78
|
-
},
|
|
79
|
-
});
|
|
35
|
+
it('subscribes and re-renders on updates (simulated poll)', async () => {
|
|
36
|
+
jest.useFakeTimers();
|
|
37
|
+
const frequency = PollingArticleResource.get.pollFrequency as number;
|
|
38
|
+
expect(frequency).toBe(5000);
|
|
80
39
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
40
|
+
const responseMock = jest.fn(() => payload);
|
|
41
|
+
const propsRef = reactive({ active: true });
|
|
42
|
+
|
|
43
|
+
const { result, waitForNextUpdate, allSettled, cleanup } =
|
|
44
|
+
await renderDataCompose(
|
|
45
|
+
({ active }: { active: boolean }) => {
|
|
46
|
+
const args = computed(() => (active ? { id: payload.id } : null));
|
|
47
|
+
useSubscription(PollingArticleResource.get, args);
|
|
48
|
+
return useSuspense(PollingArticleResource.get, {
|
|
49
|
+
id: payload.id,
|
|
50
|
+
});
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
props: propsRef,
|
|
54
|
+
resolverFixtures: [
|
|
55
|
+
{
|
|
56
|
+
endpoint: PollingArticleResource.get,
|
|
57
|
+
response: responseMock,
|
|
58
|
+
},
|
|
59
|
+
],
|
|
60
|
+
},
|
|
89
61
|
);
|
|
90
|
-
const article = await useSuspense(PollingArticleResource.get, {
|
|
91
|
-
id: payload.id,
|
|
92
|
-
});
|
|
93
|
-
return () =>
|
|
94
|
-
h('div', [
|
|
95
|
-
h('h3', (article as any).value.title),
|
|
96
|
-
h('p', (article as any).value.content),
|
|
97
|
-
]);
|
|
98
|
-
},
|
|
99
|
-
});
|
|
100
62
|
|
|
101
|
-
|
|
102
|
-
|
|
63
|
+
// Wait for initial render
|
|
64
|
+
jest.advanceTimersByTime(frequency);
|
|
65
|
+
await allSettled();
|
|
66
|
+
await waitForNextUpdate();
|
|
103
67
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
68
|
+
// Verify initial values
|
|
69
|
+
const initialArticleRef = await result;
|
|
70
|
+
expect(initialArticleRef!.value.title).toBe(payload.title);
|
|
71
|
+
expect(initialArticleRef!.value.content).toBe(payload.content);
|
|
72
|
+
|
|
73
|
+
// Change the mock response for the next poll
|
|
74
|
+
responseMock.mockReturnValue({
|
|
75
|
+
...payload,
|
|
76
|
+
title: 'updated title',
|
|
77
|
+
content: 'updated content',
|
|
109
78
|
});
|
|
110
79
|
|
|
111
|
-
//
|
|
112
|
-
|
|
80
|
+
// Advance time to trigger another poll
|
|
81
|
+
jest.advanceTimersByTime(frequency);
|
|
82
|
+
await allSettled();
|
|
83
|
+
await nextTick();
|
|
84
|
+
|
|
85
|
+
// Verify the article was updated
|
|
86
|
+
const updatedArticleRef = await result;
|
|
87
|
+
expect(updatedArticleRef!.value.title).toBe('updated title');
|
|
88
|
+
expect(updatedArticleRef!.value.content).toBe('updated content');
|
|
89
|
+
|
|
90
|
+
jest.useRealTimers();
|
|
91
|
+
cleanup();
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('should not receive updates after unsubscribing by setting active to null', async () => {
|
|
95
|
+
jest.useFakeTimers();
|
|
96
|
+
const frequency = PollingArticleResource.get.pollFrequency as number;
|
|
97
|
+
expect(frequency).toBe(5000);
|
|
98
|
+
|
|
99
|
+
const responseMock = jest.fn(() => payload);
|
|
100
|
+
const propsRef = reactive({ active: true });
|
|
101
|
+
|
|
102
|
+
const { result, waitForNextUpdate, allSettled, cleanup } =
|
|
103
|
+
await renderDataCompose(
|
|
104
|
+
(props: { active: boolean }) => {
|
|
105
|
+
const args = computed(() =>
|
|
106
|
+
props.active ? { id: payload.id } : null,
|
|
107
|
+
);
|
|
108
|
+
useSubscription(PollingArticleResource.get, args);
|
|
109
|
+
return useSuspense(PollingArticleResource.get, {
|
|
110
|
+
id: payload.id,
|
|
111
|
+
});
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
props: propsRef,
|
|
115
|
+
resolverFixtures: [
|
|
116
|
+
{
|
|
117
|
+
endpoint: PollingArticleResource.get,
|
|
118
|
+
response: responseMock,
|
|
119
|
+
},
|
|
120
|
+
],
|
|
121
|
+
},
|
|
122
|
+
);
|
|
113
123
|
|
|
114
|
-
//
|
|
115
|
-
|
|
124
|
+
// Wait for initial render
|
|
125
|
+
jest.advanceTimersByTime(frequency);
|
|
126
|
+
await allSettled();
|
|
127
|
+
await waitForNextUpdate();
|
|
116
128
|
|
|
117
129
|
// Verify initial values
|
|
118
|
-
|
|
119
|
-
expect(
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
130
|
+
const initialArticleRef = await result;
|
|
131
|
+
expect(initialArticleRef!.value!.title).toBe(payload.title);
|
|
132
|
+
expect(responseMock).toHaveBeenCalledTimes(1);
|
|
133
|
+
|
|
134
|
+
// Change the mock response
|
|
135
|
+
responseMock.mockReturnValue({
|
|
136
|
+
...payload,
|
|
137
|
+
title: 'after first poll',
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
// Advance time to trigger another poll
|
|
141
|
+
jest.advanceTimersByTime(frequency);
|
|
142
|
+
await allSettled();
|
|
143
|
+
await nextTick();
|
|
144
|
+
|
|
145
|
+
// Verify the article was updated
|
|
146
|
+
const updatedArticleRef = await result;
|
|
147
|
+
expect(updatedArticleRef!.value!.title).toBe('after first poll');
|
|
148
|
+
expect(responseMock).toHaveBeenCalledTimes(2);
|
|
149
|
+
|
|
150
|
+
// Now unsubscribe by setting active to false
|
|
151
|
+
propsRef.active = false;
|
|
152
|
+
await nextTick();
|
|
153
|
+
|
|
154
|
+
// Change the mock response again
|
|
155
|
+
responseMock.mockReturnValue({
|
|
156
|
+
...payload,
|
|
157
|
+
title: 'should not see this',
|
|
127
158
|
});
|
|
128
159
|
|
|
129
|
-
|
|
130
|
-
|
|
160
|
+
// Advance time - subscription should not trigger
|
|
161
|
+
jest.advanceTimersByTime(frequency);
|
|
162
|
+
await allSettled();
|
|
163
|
+
await nextTick();
|
|
164
|
+
|
|
165
|
+
// Verify the article was NOT updated (still has old value)
|
|
166
|
+
const finalArticleRef = await result;
|
|
167
|
+
expect(finalArticleRef!.value!.title).toBe('after first poll');
|
|
168
|
+
// Should still be 2 calls, no new poll happened
|
|
169
|
+
expect(responseMock).toHaveBeenCalledTimes(2);
|
|
170
|
+
|
|
171
|
+
jest.useRealTimers();
|
|
172
|
+
cleanup();
|
|
131
173
|
});
|
|
132
174
|
|
|
133
|
-
it('
|
|
134
|
-
|
|
135
|
-
const
|
|
175
|
+
it('should unsubscribe when subscribed component unmounts', async () => {
|
|
176
|
+
jest.useFakeTimers();
|
|
177
|
+
const frequency = PollingArticleResource.get.pollFrequency as number;
|
|
178
|
+
expect(frequency).toBe(5000);
|
|
136
179
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
180
|
+
const responseMock = jest.fn(() => payload);
|
|
181
|
+
|
|
182
|
+
// Subscriber component that only subscribes
|
|
183
|
+
const SubscriberComponent = defineComponent({
|
|
184
|
+
name: 'SubscriberComponent',
|
|
185
|
+
setup() {
|
|
186
|
+
useSubscription(PollingArticleResource.get, { id: payload.id });
|
|
187
|
+
return () => h('div', 'Subscriber');
|
|
188
|
+
},
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
// Reader component that reads data with useSuspense
|
|
192
|
+
const ReaderComponent = defineComponent({
|
|
193
|
+
name: 'ReaderComponent',
|
|
140
194
|
async setup() {
|
|
141
|
-
|
|
142
|
-
useSubscription(ArticleResource.get, { id: payload.id } as any);
|
|
143
|
-
// Then resolve suspense for stable render
|
|
144
|
-
const article = await useSuspense(ArticleResource.get, {
|
|
195
|
+
const article = await useSuspense(PollingArticleResource.get, {
|
|
145
196
|
id: payload.id,
|
|
146
197
|
});
|
|
147
|
-
return () => h('div', (article
|
|
198
|
+
return () => h('div', [h('h3', article.value.title)]);
|
|
148
199
|
},
|
|
149
200
|
});
|
|
150
201
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
202
|
+
// Parent component that conditionally renders the subscriber
|
|
203
|
+
const ParentComponent = defineComponent({
|
|
204
|
+
name: 'ParentComponent',
|
|
205
|
+
props: {
|
|
206
|
+
showSubscriber: {
|
|
207
|
+
type: Boolean,
|
|
208
|
+
default: true,
|
|
209
|
+
},
|
|
210
|
+
},
|
|
211
|
+
setup(props) {
|
|
212
|
+
return () =>
|
|
213
|
+
h('div', [
|
|
214
|
+
props.showSubscriber ? h(SubscriberComponent) : null,
|
|
215
|
+
h(ReaderComponent),
|
|
216
|
+
]);
|
|
155
217
|
},
|
|
156
218
|
});
|
|
157
219
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
220
|
+
const props = reactive({ showSubscriber: true });
|
|
221
|
+
const { wrapper, allSettled, cleanup } = mountDataClient(ParentComponent, {
|
|
222
|
+
props,
|
|
223
|
+
resolverFixtures: [
|
|
224
|
+
{
|
|
225
|
+
endpoint: PollingArticleResource.get,
|
|
226
|
+
response: responseMock,
|
|
227
|
+
},
|
|
228
|
+
],
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
// Wait for initial render
|
|
232
|
+
jest.advanceTimersByTime(frequency);
|
|
233
|
+
await allSettled();
|
|
234
|
+
await flushUntilWithFakeTimers(wrapper, () => wrapper.find('h3').exists());
|
|
235
|
+
|
|
236
|
+
// Verify initial values
|
|
237
|
+
expect(wrapper.find('h3').text()).toBe(payload.title);
|
|
238
|
+
expect(responseMock).toHaveBeenCalledTimes(1);
|
|
239
|
+
|
|
240
|
+
// Change the mock response
|
|
241
|
+
responseMock.mockReturnValue({
|
|
242
|
+
...payload,
|
|
243
|
+
title: 'first update',
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
// Advance time to trigger poll
|
|
247
|
+
jest.advanceTimersByTime(frequency);
|
|
248
|
+
await allSettled();
|
|
249
|
+
await nextTick();
|
|
250
|
+
|
|
251
|
+
// Verify the article was updated
|
|
252
|
+
expect(wrapper.find('h3').text()).toBe('first update');
|
|
253
|
+
expect(responseMock).toHaveBeenCalledTimes(2);
|
|
254
|
+
|
|
255
|
+
// Now unmount the subscribing component by setting showSubscriber to false
|
|
256
|
+
props.showSubscriber = false;
|
|
257
|
+
await nextTick();
|
|
258
|
+
|
|
259
|
+
// Change the mock response again
|
|
260
|
+
responseMock.mockReturnValue({
|
|
261
|
+
...payload,
|
|
262
|
+
title: 'should not see this',
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
// Advance time - subscription should not trigger
|
|
266
|
+
jest.advanceTimersByTime(frequency);
|
|
267
|
+
await allSettled();
|
|
268
|
+
await nextTick();
|
|
269
|
+
|
|
270
|
+
// The reading component should still show old data since no subscription is active
|
|
271
|
+
expect(wrapper.find('h3').text()).toBe('first update');
|
|
272
|
+
// Should still be 2 calls, no new poll happened
|
|
273
|
+
expect(responseMock).toHaveBeenCalledTimes(2);
|
|
161
274
|
|
|
162
|
-
|
|
275
|
+
jest.useRealTimers();
|
|
276
|
+
cleanup();
|
|
163
277
|
});
|
|
164
278
|
});
|