@data-client/vue 0.2.0 → 0.3.1
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 +2 -25
- package/dist/index.js +200 -42
- 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 -4
- package/lib/consumers/useFetch.d.ts.map +1 -1
- package/lib/consumers/useFetch.js +10 -10
- 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 +5 -6
- package/lib/consumers/useQuery.d.ts.map +1 -1
- package/lib/consumers/useQuery.js +9 -7
- 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 +11 -2
- package/lib/consumers/useSuspense.d.ts.map +1 -1
- package/lib/consumers/useSuspense.js +25 -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/getDefaultManagers.d.ts +4 -1
- package/lib/providers/getDefaultManagers.d.ts.map +1 -1
- package/lib/providers/getDefaultManagers.js +5 -2
- 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 +11 -9
- 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 -15
- package/src/consumers/useLive.ts +6 -2
- package/src/consumers/useQuery.ts +16 -9
- package/src/consumers/useSubscription.ts +5 -1
- package/src/consumers/useSuspense.ts +37 -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/getDefaultManagers.ts +4 -1
- 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/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/consumers/index.d.ts +0 -9
- package/ts3.4/consumers/useDebounce.d.ts +0 -19
- package/ts3.4/consumers/useFetch.d.ts +0 -16
- package/ts3.4/consumers/useLive.d.ts +0 -19
- package/ts3.4/consumers/useLoading.d.ts +0 -22
- package/ts3.4/consumers/useQuery.d.ts +0 -12
- package/ts3.4/consumers/useSubscription.d.ts +0 -12
- package/ts3.4/consumers/useSuspense.d.ts +0 -11
- package/ts3.4/context.d.ts +0 -10
- package/ts3.4/index.d.ts +0 -6
- package/ts3.4/managers/index.d.ts +0 -2
- package/ts3.4/providers/DataClientPlugin.d.ts +0 -22
- package/ts3.4/providers/createDataClient.d.ts +0 -25
- package/ts3.4/providers/getDefaultManagers.d.ts +0 -14
- package/ts3.4/providers/index.d.ts +0 -4
- package/ts3.4/test/MockController.d.ts +0 -8
- package/ts3.4/test/collapseFixture.d.ts +0 -6
- package/ts3.4/test/createFixtureMap.d.ts +0 -6
- package/ts3.4/test/fixtureTypes.d.ts +0 -99
- package/ts3.4/test/index.d.ts +0 -8
- package/ts3.4/test/mockState.d.ts +0 -4
- package/ts3.4/test/renderDataClient.d.ts +0 -40
|
@@ -0,0 +1,660 @@
|
|
|
1
|
+
import nock from 'nock';
|
|
2
|
+
import { computed, nextTick, reactive } from 'vue';
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
CoolerArticle,
|
|
6
|
+
CoolerArticleResource,
|
|
7
|
+
InvalidIfStaleArticleResource,
|
|
8
|
+
PaginatedArticleResource,
|
|
9
|
+
TypedArticleResource,
|
|
10
|
+
} from '../../../../__tests__/new';
|
|
11
|
+
import useDLE from '../consumers/useDLE';
|
|
12
|
+
import { renderDataCompose } from '../test';
|
|
13
|
+
|
|
14
|
+
// Minimal shared fixtures (copied from React test fixtures)
|
|
15
|
+
const payload = {
|
|
16
|
+
id: 5,
|
|
17
|
+
title: 'hi ho',
|
|
18
|
+
content: 'whatever',
|
|
19
|
+
tags: ['a', 'best', 'react'],
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const payload2 = {
|
|
23
|
+
id: 6,
|
|
24
|
+
title: 'next',
|
|
25
|
+
content: 'my best content yet',
|
|
26
|
+
tags: ['b'],
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const users = [
|
|
30
|
+
{ id: 23, username: 'bob' },
|
|
31
|
+
{ id: 24, username: 'alice' },
|
|
32
|
+
];
|
|
33
|
+
|
|
34
|
+
const nested = [
|
|
35
|
+
{
|
|
36
|
+
id: 5,
|
|
37
|
+
title: 'hi ho',
|
|
38
|
+
content: 'whatever',
|
|
39
|
+
tags: ['a', 'best', 'react'],
|
|
40
|
+
author: {
|
|
41
|
+
id: 23,
|
|
42
|
+
username: 'bob',
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
id: 3,
|
|
47
|
+
title: 'the next time',
|
|
48
|
+
content: 'whatever',
|
|
49
|
+
author: {
|
|
50
|
+
id: 23,
|
|
51
|
+
username: 'charles',
|
|
52
|
+
email: 'bob@bob.com',
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
];
|
|
56
|
+
|
|
57
|
+
describe('vue useDLE()', () => {
|
|
58
|
+
beforeAll(() => {
|
|
59
|
+
nock(/.*/)
|
|
60
|
+
.persist()
|
|
61
|
+
.defaultReplyHeaders({
|
|
62
|
+
'Access-Control-Allow-Origin': '*',
|
|
63
|
+
'Content-Type': 'application/json',
|
|
64
|
+
})
|
|
65
|
+
.options(/.*/)
|
|
66
|
+
.reply(200)
|
|
67
|
+
.get(`/article-cooler/${payload.id}`)
|
|
68
|
+
.reply(200, payload)
|
|
69
|
+
.get(`/article-cooler/${payload2.id}`)
|
|
70
|
+
.reply(200, payload2)
|
|
71
|
+
.delete(`/article-cooler/${payload.id}`)
|
|
72
|
+
.reply(204, '')
|
|
73
|
+
.delete(`/article/${payload.id}`)
|
|
74
|
+
.reply(200, {})
|
|
75
|
+
.get(`/article-cooler/0`)
|
|
76
|
+
.reply(403, {})
|
|
77
|
+
.get(`/article-cooler/666`)
|
|
78
|
+
.reply(200, '')
|
|
79
|
+
.get(`/article-cooler`)
|
|
80
|
+
.reply(200, nested)
|
|
81
|
+
.get(/article-cooler\/.*/)
|
|
82
|
+
.reply(404, 'not found')
|
|
83
|
+
.put(/article-cooler\/[^5].*/)
|
|
84
|
+
.reply(404, 'not found')
|
|
85
|
+
.get(`/user`)
|
|
86
|
+
.reply(200, users);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
afterAll(() => {
|
|
90
|
+
nock.cleanAll();
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('should work on good network', async () => {
|
|
94
|
+
const { result, waitForNextUpdate, cleanup } = await renderDataCompose(
|
|
95
|
+
() => {
|
|
96
|
+
return useDLE(CoolerArticleResource.get, { id: payload.id });
|
|
97
|
+
},
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
expect(result.data.value).toBe(undefined);
|
|
101
|
+
expect(result.error.value).toBe(undefined);
|
|
102
|
+
expect(result.loading.value).toBe(true);
|
|
103
|
+
|
|
104
|
+
await waitForNextUpdate();
|
|
105
|
+
|
|
106
|
+
expect(result.loading.value).toBe(false);
|
|
107
|
+
expect(result.error.value).toBeUndefined();
|
|
108
|
+
expect(result.data.value).toEqual(CoolerArticle.fromJS(payload));
|
|
109
|
+
|
|
110
|
+
cleanup();
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('should work with no schema', async () => {
|
|
114
|
+
const { result, waitForNextUpdate, cleanup } = await renderDataCompose(
|
|
115
|
+
() => {
|
|
116
|
+
return useDLE(CoolerArticleResource.get.extend({ schema: undefined }), {
|
|
117
|
+
id: payload.id,
|
|
118
|
+
});
|
|
119
|
+
},
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
expect(result.data.value).toBe(undefined);
|
|
123
|
+
expect(result.error.value).toBe(undefined);
|
|
124
|
+
expect(result.loading.value).toBe(true);
|
|
125
|
+
|
|
126
|
+
await waitForNextUpdate();
|
|
127
|
+
|
|
128
|
+
expect(result.loading.value).toBe(false);
|
|
129
|
+
expect(result.error.value).toBeUndefined();
|
|
130
|
+
expect(result.data.value).toEqual(payload);
|
|
131
|
+
|
|
132
|
+
cleanup();
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('should work on good network with endpoint', async () => {
|
|
136
|
+
const { result, waitForNextUpdate, cleanup } = await renderDataCompose(
|
|
137
|
+
() => {
|
|
138
|
+
return useDLE(TypedArticleResource.get, { id: payload.id });
|
|
139
|
+
},
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
expect(result.data.value).toBe(undefined);
|
|
143
|
+
expect(result.error.value).toBe(undefined);
|
|
144
|
+
expect(result.loading.value).toBe(true);
|
|
145
|
+
|
|
146
|
+
await waitForNextUpdate();
|
|
147
|
+
|
|
148
|
+
expect(result.loading.value).toBe(false);
|
|
149
|
+
expect(result.error.value).toBeUndefined();
|
|
150
|
+
expect(result.data.value).toEqual(CoolerArticle.fromJS(payload));
|
|
151
|
+
|
|
152
|
+
cleanup();
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('should properly discriminate null args types', () => {
|
|
156
|
+
() => {
|
|
157
|
+
const result = useDLE(
|
|
158
|
+
TypedArticleResource.get,
|
|
159
|
+
(true as boolean) ? { id: payload.id } : null,
|
|
160
|
+
);
|
|
161
|
+
if (!result.loading.value && !result.error.value) {
|
|
162
|
+
// @ts-expect-error
|
|
163
|
+
result.data.value.title;
|
|
164
|
+
|
|
165
|
+
result.data.value && result.data.value.title;
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
() => {
|
|
169
|
+
const result = useDLE(TypedArticleResource.get, { id: payload.id });
|
|
170
|
+
if (!result.loading.value && !result.error.value) {
|
|
171
|
+
result.data.value && result.data.value.title;
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it('should return errors on bad network', async () => {
|
|
177
|
+
const { result, waitForNextUpdate, cleanup } = await renderDataCompose(
|
|
178
|
+
() => {
|
|
179
|
+
return useDLE(CoolerArticleResource.get, { title: '0' });
|
|
180
|
+
},
|
|
181
|
+
);
|
|
182
|
+
|
|
183
|
+
expect(result.data.value).toBe(undefined);
|
|
184
|
+
expect(result.error.value).toBe(undefined);
|
|
185
|
+
expect(result.loading.value).toBe(true);
|
|
186
|
+
|
|
187
|
+
await waitForNextUpdate();
|
|
188
|
+
|
|
189
|
+
expect(result.loading.value).toBe(false);
|
|
190
|
+
expect(result.error.value).toBeDefined();
|
|
191
|
+
expect((result.error.value as any).status).toBe(403);
|
|
192
|
+
|
|
193
|
+
cleanup();
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it('should pass with exact params', async () => {
|
|
197
|
+
const { result, waitForNextUpdate, cleanup } = await renderDataCompose(
|
|
198
|
+
() => {
|
|
199
|
+
return useDLE(TypedArticleResource.get, { id: payload.id });
|
|
200
|
+
},
|
|
201
|
+
);
|
|
202
|
+
|
|
203
|
+
expect(result.data.value).toBeUndefined();
|
|
204
|
+
|
|
205
|
+
await waitForNextUpdate();
|
|
206
|
+
|
|
207
|
+
// type discrimination forces it to be resolved
|
|
208
|
+
if (
|
|
209
|
+
!result.loading.value &&
|
|
210
|
+
result.error.value === undefined &&
|
|
211
|
+
result.data.value
|
|
212
|
+
) {
|
|
213
|
+
expect(result.data.value.title).toBe(payload.title);
|
|
214
|
+
// @ts-expect-error ensure this isn't "any"
|
|
215
|
+
result.data.value.doesnotexist;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
cleanup();
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
it('should fail with improperly typed param', async () => {
|
|
222
|
+
const { result, waitForNextUpdate, cleanup } = await renderDataCompose(
|
|
223
|
+
() => {
|
|
224
|
+
// @ts-expect-error
|
|
225
|
+
return useDLE(TypedArticleResource.get, {
|
|
226
|
+
id: "{ a: 'five' }" as any as Date,
|
|
227
|
+
});
|
|
228
|
+
},
|
|
229
|
+
);
|
|
230
|
+
|
|
231
|
+
expect(result.data.value).toBeUndefined();
|
|
232
|
+
expect(result.error.value).toBeUndefined();
|
|
233
|
+
expect(result.loading.value).toBe(true);
|
|
234
|
+
|
|
235
|
+
await waitForNextUpdate();
|
|
236
|
+
|
|
237
|
+
expect(result.loading.value).toBe(false);
|
|
238
|
+
expect(result.error.value).toBeDefined();
|
|
239
|
+
if (result.error.value) {
|
|
240
|
+
expect((result.error.value as any).status).toBe(404);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
cleanup();
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
it('should fetch anew with param changes', async () => {
|
|
247
|
+
const props = reactive({ id: payload.id });
|
|
248
|
+
const { result, waitForNextUpdate, cleanup } = await renderDataCompose(
|
|
249
|
+
(props: { id: number }) => {
|
|
250
|
+
return useDLE(
|
|
251
|
+
CoolerArticleResource.get,
|
|
252
|
+
computed(() => ({ id: props.id })),
|
|
253
|
+
);
|
|
254
|
+
},
|
|
255
|
+
{ props },
|
|
256
|
+
);
|
|
257
|
+
|
|
258
|
+
expect(result.data.value).toBe(undefined);
|
|
259
|
+
expect(result.error.value).toBe(undefined);
|
|
260
|
+
expect(result.loading.value).toBe(true);
|
|
261
|
+
|
|
262
|
+
await waitForNextUpdate();
|
|
263
|
+
|
|
264
|
+
expect(result.loading.value).toBe(false);
|
|
265
|
+
expect(result.error.value).toBeUndefined();
|
|
266
|
+
expect(result.data.value).toEqual(CoolerArticle.fromJS(payload));
|
|
267
|
+
|
|
268
|
+
props.id = payload2.id;
|
|
269
|
+
await nextTick();
|
|
270
|
+
|
|
271
|
+
expect(result.data.value).toBe(undefined);
|
|
272
|
+
expect(result.error.value).toBe(undefined);
|
|
273
|
+
expect(result.loading.value).toBe(true);
|
|
274
|
+
|
|
275
|
+
await waitForNextUpdate();
|
|
276
|
+
|
|
277
|
+
expect(result.loading.value).toBe(false);
|
|
278
|
+
expect(result.error.value).toBeUndefined();
|
|
279
|
+
expect(result.data.value).toEqual(CoolerArticle.fromJS(payload2));
|
|
280
|
+
|
|
281
|
+
cleanup();
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
it('should not be loading with null params', async () => {
|
|
285
|
+
const { result, cleanup } = await renderDataCompose(() => {
|
|
286
|
+
return useDLE(CoolerArticleResource.get, null);
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
await nextTick();
|
|
290
|
+
expect(result.loading.value).toBe(false);
|
|
291
|
+
|
|
292
|
+
cleanup();
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
it('should maintain schema structure even with null params', async () => {
|
|
296
|
+
const { result, cleanup } = await renderDataCompose(() => {
|
|
297
|
+
return useDLE(PaginatedArticleResource.getList, null);
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
await nextTick();
|
|
301
|
+
expect(result.loading.value).toBe(false);
|
|
302
|
+
expect(result.data.value.results).toBeUndefined();
|
|
303
|
+
expect(result.data.value.nextPage).toBe('');
|
|
304
|
+
// ensure this isn't 'any'
|
|
305
|
+
// @ts-expect-error
|
|
306
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
307
|
+
const _typeCheck: PaginatedArticleResource[] = result.data.value.results;
|
|
308
|
+
|
|
309
|
+
cleanup();
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
it('should not select when results are stale and invalidIfStale is true', async () => {
|
|
313
|
+
const realDate = global.Date.now;
|
|
314
|
+
Date.now = jest.fn(() => 999999999);
|
|
315
|
+
|
|
316
|
+
const props = reactive({ id: payload.id as number | null });
|
|
317
|
+
const { result, waitForNextUpdate, cleanup } = await renderDataCompose(
|
|
318
|
+
(props: { id: number | null }) => {
|
|
319
|
+
return useDLE(
|
|
320
|
+
InvalidIfStaleArticleResource.get,
|
|
321
|
+
computed(() => (props.id !== null ? { id: props.id } : null)),
|
|
322
|
+
);
|
|
323
|
+
},
|
|
324
|
+
{ props },
|
|
325
|
+
);
|
|
326
|
+
|
|
327
|
+
await waitForNextUpdate();
|
|
328
|
+
expect(result.data.value).toBeDefined();
|
|
329
|
+
|
|
330
|
+
Date.now = jest.fn(() => 999999999 * 3);
|
|
331
|
+
|
|
332
|
+
props.id = null;
|
|
333
|
+
await nextTick();
|
|
334
|
+
expect(result.data.value).toBeUndefined();
|
|
335
|
+
|
|
336
|
+
props.id = payload.id;
|
|
337
|
+
await nextTick();
|
|
338
|
+
expect(result.data.value).toBeUndefined();
|
|
339
|
+
expect(result.loading.value).toBe(true);
|
|
340
|
+
|
|
341
|
+
await waitForNextUpdate();
|
|
342
|
+
expect(result.data.value).toBeDefined();
|
|
343
|
+
expect(result.loading.value).toBe(false);
|
|
344
|
+
|
|
345
|
+
global.Date.now = realDate;
|
|
346
|
+
cleanup();
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
it('should reactively update data when controller.setResponse() is called', async () => {
|
|
350
|
+
const { result, controller, waitForNextUpdate, cleanup } =
|
|
351
|
+
await renderDataCompose(() => {
|
|
352
|
+
return useDLE(CoolerArticleResource.get, { id: payload.id });
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
await waitForNextUpdate();
|
|
356
|
+
|
|
357
|
+
expect(result.data.value).toEqual(CoolerArticle.fromJS(payload));
|
|
358
|
+
expect(result.loading.value).toBe(false);
|
|
359
|
+
|
|
360
|
+
// Update the store using controller.setResponse
|
|
361
|
+
const newTitle = payload.title + ' updated';
|
|
362
|
+
const newContent = payload.content + ' v2';
|
|
363
|
+
controller.setResponse(
|
|
364
|
+
CoolerArticleResource.get,
|
|
365
|
+
{ id: payload.id },
|
|
366
|
+
{ ...payload, title: newTitle, content: newContent },
|
|
367
|
+
);
|
|
368
|
+
|
|
369
|
+
// Wait a tick for Vue reactivity to propagate
|
|
370
|
+
await nextTick();
|
|
371
|
+
|
|
372
|
+
// The ComputedRef should now have updated values (it's reactive!)
|
|
373
|
+
expect(result.data.value!.title).toBe(newTitle);
|
|
374
|
+
expect(result.data.value!.content).toBe(newContent);
|
|
375
|
+
expect(result.loading.value).toBe(false);
|
|
376
|
+
|
|
377
|
+
cleanup();
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
it('should be reactive with prop changes', async () => {
|
|
381
|
+
const props = reactive({ id: payload.id });
|
|
382
|
+
const { result, waitForNextUpdate, cleanup } = await renderDataCompose(
|
|
383
|
+
(props: { id: number }) => {
|
|
384
|
+
return useDLE(
|
|
385
|
+
CoolerArticleResource.get,
|
|
386
|
+
computed(() => ({ id: props.id })),
|
|
387
|
+
);
|
|
388
|
+
},
|
|
389
|
+
{
|
|
390
|
+
props,
|
|
391
|
+
initialFixtures: [
|
|
392
|
+
{
|
|
393
|
+
endpoint: CoolerArticleResource.get,
|
|
394
|
+
args: [{ id: payload.id }],
|
|
395
|
+
response: payload,
|
|
396
|
+
},
|
|
397
|
+
{
|
|
398
|
+
endpoint: CoolerArticleResource.get,
|
|
399
|
+
args: [{ id: payload2.id }],
|
|
400
|
+
response: payload2,
|
|
401
|
+
},
|
|
402
|
+
],
|
|
403
|
+
},
|
|
404
|
+
);
|
|
405
|
+
|
|
406
|
+
await waitForNextUpdate();
|
|
407
|
+
expect(result.data.value?.title).toBe(payload.title);
|
|
408
|
+
|
|
409
|
+
// Change props - result automatically updates
|
|
410
|
+
props.id = payload2.id;
|
|
411
|
+
await nextTick();
|
|
412
|
+
await waitForNextUpdate();
|
|
413
|
+
|
|
414
|
+
expect(result.data.value?.title).toBe(payload2.title);
|
|
415
|
+
|
|
416
|
+
cleanup();
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
it('should handle conditional null arguments', async () => {
|
|
420
|
+
const props = reactive({ id: payload.id as number | null });
|
|
421
|
+
const { result, waitForNextUpdate, cleanup } = await renderDataCompose(
|
|
422
|
+
(props: { id: number | null }) =>
|
|
423
|
+
useDLE(
|
|
424
|
+
CoolerArticleResource.get,
|
|
425
|
+
computed(() => (props.id !== null ? { id: props.id } : null)),
|
|
426
|
+
),
|
|
427
|
+
{
|
|
428
|
+
props,
|
|
429
|
+
initialFixtures: [
|
|
430
|
+
{
|
|
431
|
+
endpoint: CoolerArticleResource.get,
|
|
432
|
+
args: [{ id: payload.id }],
|
|
433
|
+
response: payload,
|
|
434
|
+
},
|
|
435
|
+
],
|
|
436
|
+
},
|
|
437
|
+
);
|
|
438
|
+
|
|
439
|
+
await waitForNextUpdate();
|
|
440
|
+
expect(result.data.value).toBeDefined();
|
|
441
|
+
expect(result.data.value?.title).toBe(payload.title);
|
|
442
|
+
|
|
443
|
+
// Set to null - becomes undefined
|
|
444
|
+
props.id = null;
|
|
445
|
+
await nextTick();
|
|
446
|
+
expect(result.data.value).toBeUndefined();
|
|
447
|
+
expect(result.loading.value).toBe(false);
|
|
448
|
+
|
|
449
|
+
cleanup();
|
|
450
|
+
});
|
|
451
|
+
|
|
452
|
+
it('should not be loading when data is valid and not expired', async () => {
|
|
453
|
+
const { result, cleanup } = await renderDataCompose(
|
|
454
|
+
() => {
|
|
455
|
+
return useDLE(CoolerArticleResource.get, { id: payload.id });
|
|
456
|
+
},
|
|
457
|
+
{
|
|
458
|
+
initialFixtures: [
|
|
459
|
+
{
|
|
460
|
+
endpoint: CoolerArticleResource.get,
|
|
461
|
+
args: [{ id: payload.id }],
|
|
462
|
+
response: payload,
|
|
463
|
+
},
|
|
464
|
+
],
|
|
465
|
+
},
|
|
466
|
+
);
|
|
467
|
+
|
|
468
|
+
// Should not be loading since data is valid (fixture provides fresh data)
|
|
469
|
+
expect(result.loading.value).toBe(false);
|
|
470
|
+
expect(result.data.value).toBeDefined();
|
|
471
|
+
expect(result.data.value?.title).toBe(payload.title);
|
|
472
|
+
|
|
473
|
+
cleanup();
|
|
474
|
+
});
|
|
475
|
+
|
|
476
|
+
it('should refetch when data becomes stale after time passes', async () => {
|
|
477
|
+
const realDate = global.Date.now;
|
|
478
|
+
Date.now = jest.fn(() => 1000000);
|
|
479
|
+
|
|
480
|
+
const props = reactive({ id: payload.id });
|
|
481
|
+
const { result, waitForNextUpdate, cleanup } = await renderDataCompose(
|
|
482
|
+
(props: { id: number }) => {
|
|
483
|
+
return useDLE(
|
|
484
|
+
CoolerArticleResource.get,
|
|
485
|
+
computed(() => ({ id: props.id })),
|
|
486
|
+
);
|
|
487
|
+
},
|
|
488
|
+
{ props },
|
|
489
|
+
);
|
|
490
|
+
|
|
491
|
+
await waitForNextUpdate();
|
|
492
|
+
expect(result.data.value).toBeDefined();
|
|
493
|
+
expect(result.loading.value).toBe(false);
|
|
494
|
+
|
|
495
|
+
// Advance time beyond expiry
|
|
496
|
+
Date.now = jest.fn(() => 1000000 + 999999999);
|
|
497
|
+
|
|
498
|
+
// Trigger re-evaluation by changing and reverting props
|
|
499
|
+
props.id = payload2.id;
|
|
500
|
+
await nextTick();
|
|
501
|
+
|
|
502
|
+
// Should trigger new fetch for payload2 (and it will be stale)
|
|
503
|
+
expect(result.loading.value).toBe(true);
|
|
504
|
+
|
|
505
|
+
global.Date.now = realDate;
|
|
506
|
+
cleanup();
|
|
507
|
+
});
|
|
508
|
+
|
|
509
|
+
it('should handle force fetch with invalidIfStale', async () => {
|
|
510
|
+
const realDate = global.Date.now;
|
|
511
|
+
Date.now = jest.fn(() => 1000000);
|
|
512
|
+
|
|
513
|
+
const { result, waitForNextUpdate, cleanup, controller } =
|
|
514
|
+
await renderDataCompose(() => {
|
|
515
|
+
return useDLE(InvalidIfStaleArticleResource.get, { id: payload.id });
|
|
516
|
+
});
|
|
517
|
+
|
|
518
|
+
await waitForNextUpdate();
|
|
519
|
+
expect(result.data.value).toBeDefined();
|
|
520
|
+
expect(result.loading.value).toBe(false);
|
|
521
|
+
|
|
522
|
+
// Invalidate to trigger forceFetch
|
|
523
|
+
controller.invalidate(InvalidIfStaleArticleResource.get, {
|
|
524
|
+
id: payload.id,
|
|
525
|
+
});
|
|
526
|
+
|
|
527
|
+
await nextTick();
|
|
528
|
+
|
|
529
|
+
// Should be loading due to invalidation (force fetch)
|
|
530
|
+
expect(result.loading.value).toBe(true);
|
|
531
|
+
|
|
532
|
+
await waitForNextUpdate();
|
|
533
|
+
|
|
534
|
+
expect(result.loading.value).toBe(false);
|
|
535
|
+
expect(result.data.value).toBeDefined();
|
|
536
|
+
|
|
537
|
+
global.Date.now = realDate;
|
|
538
|
+
cleanup();
|
|
539
|
+
});
|
|
540
|
+
|
|
541
|
+
it('should handle switching from null to valid args', async () => {
|
|
542
|
+
const props = reactive({ id: null as number | null });
|
|
543
|
+
const { result, waitForNextUpdate, cleanup } = await renderDataCompose(
|
|
544
|
+
(props: { id: number | null }) =>
|
|
545
|
+
useDLE(
|
|
546
|
+
CoolerArticleResource.get,
|
|
547
|
+
computed(() => (props.id !== null ? { id: props.id } : null)),
|
|
548
|
+
),
|
|
549
|
+
{ props },
|
|
550
|
+
);
|
|
551
|
+
|
|
552
|
+
// Initially with null args
|
|
553
|
+
expect(result.loading.value).toBe(false);
|
|
554
|
+
expect(result.data.value).toBeUndefined();
|
|
555
|
+
|
|
556
|
+
// Switch to valid args
|
|
557
|
+
props.id = payload.id;
|
|
558
|
+
await nextTick();
|
|
559
|
+
|
|
560
|
+
expect(result.loading.value).toBe(true);
|
|
561
|
+
|
|
562
|
+
await waitForNextUpdate();
|
|
563
|
+
|
|
564
|
+
expect(result.loading.value).toBe(false);
|
|
565
|
+
expect(result.data.value).toBeDefined();
|
|
566
|
+
expect(result.data.value?.title).toBe(payload.title);
|
|
567
|
+
|
|
568
|
+
cleanup();
|
|
569
|
+
});
|
|
570
|
+
|
|
571
|
+
it('should handle data when expires condition is met but not force fetch', async () => {
|
|
572
|
+
const realDate = global.Date.now;
|
|
573
|
+
// Set initial time
|
|
574
|
+
Date.now = jest.fn(() => 100000);
|
|
575
|
+
|
|
576
|
+
const { result, cleanup } = await renderDataCompose(
|
|
577
|
+
() => {
|
|
578
|
+
return useDLE(CoolerArticleResource.get, { id: payload.id });
|
|
579
|
+
},
|
|
580
|
+
{
|
|
581
|
+
initialFixtures: [
|
|
582
|
+
{
|
|
583
|
+
endpoint: CoolerArticleResource.get,
|
|
584
|
+
args: [{ id: payload.id }],
|
|
585
|
+
response: payload,
|
|
586
|
+
},
|
|
587
|
+
],
|
|
588
|
+
},
|
|
589
|
+
);
|
|
590
|
+
|
|
591
|
+
// Data should be available and not loading (fixture provides fresh data)
|
|
592
|
+
expect(result.loading.value).toBe(false);
|
|
593
|
+
expect(result.data.value?.title).toBe(payload.title);
|
|
594
|
+
|
|
595
|
+
global.Date.now = realDate;
|
|
596
|
+
cleanup();
|
|
597
|
+
});
|
|
598
|
+
|
|
599
|
+
it('should handle data when data is valid (not expired)', async () => {
|
|
600
|
+
const { result, cleanup } = await renderDataCompose(
|
|
601
|
+
() => {
|
|
602
|
+
return useDLE(CoolerArticleResource.get, { id: payload.id });
|
|
603
|
+
},
|
|
604
|
+
{
|
|
605
|
+
initialFixtures: [
|
|
606
|
+
{
|
|
607
|
+
endpoint: CoolerArticleResource.get,
|
|
608
|
+
args: [{ id: payload.id }],
|
|
609
|
+
response: payload,
|
|
610
|
+
},
|
|
611
|
+
],
|
|
612
|
+
},
|
|
613
|
+
);
|
|
614
|
+
|
|
615
|
+
// With valid cached data, should not be loading
|
|
616
|
+
expect(result.loading.value).toBe(false);
|
|
617
|
+
expect(result.data.value?.title).toBe(payload.title);
|
|
618
|
+
expect(result.error.value).toBeUndefined();
|
|
619
|
+
|
|
620
|
+
cleanup();
|
|
621
|
+
});
|
|
622
|
+
|
|
623
|
+
it('should cover forceFetch branch when data is not expired', async () => {
|
|
624
|
+
const realDate = global.Date.now;
|
|
625
|
+
Date.now = jest.fn(() => 1000000);
|
|
626
|
+
|
|
627
|
+
const { result, waitForNextUpdate, cleanup, controller } =
|
|
628
|
+
await renderDataCompose(() => {
|
|
629
|
+
return useDLE(InvalidIfStaleArticleResource.get, { id: payload.id });
|
|
630
|
+
});
|
|
631
|
+
|
|
632
|
+
await waitForNextUpdate();
|
|
633
|
+
expect(result.data.value).toBeDefined();
|
|
634
|
+
expect(result.loading.value).toBe(false);
|
|
635
|
+
|
|
636
|
+
// Don't advance time, but invalidate (forceFetch = true, but Date.now() <= expiresAt)
|
|
637
|
+
controller.invalidate(InvalidIfStaleArticleResource.get, {
|
|
638
|
+
id: payload.id,
|
|
639
|
+
});
|
|
640
|
+
await nextTick();
|
|
641
|
+
|
|
642
|
+
// Should trigger loading due to forceFetch even though not expired by time
|
|
643
|
+
expect(result.loading.value).toBe(true);
|
|
644
|
+
|
|
645
|
+
global.Date.now = realDate;
|
|
646
|
+
cleanup();
|
|
647
|
+
});
|
|
648
|
+
|
|
649
|
+
it('should handle argsKey being empty string with null args', async () => {
|
|
650
|
+
const { result, cleanup } = await renderDataCompose(() => {
|
|
651
|
+
return useDLE(CoolerArticleResource.get, null);
|
|
652
|
+
});
|
|
653
|
+
|
|
654
|
+
// With null args, argsKey is empty, so not loading
|
|
655
|
+
expect(result.loading.value).toBe(false);
|
|
656
|
+
expect(result.data.value).toBeUndefined();
|
|
657
|
+
|
|
658
|
+
cleanup();
|
|
659
|
+
});
|
|
660
|
+
});
|