@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
|
@@ -0,0 +1,586 @@
|
|
|
1
|
+
import { ref, computed, watch, reactive, nextTick } from 'vue';
|
|
2
|
+
|
|
3
|
+
import { CoolerArticleResource } from '../../../../../__tests__/new';
|
|
4
|
+
import { renderDataCompose } from '../renderDataCompose';
|
|
5
|
+
|
|
6
|
+
describe('renderDataCompose', () => {
|
|
7
|
+
const payload = {
|
|
8
|
+
id: 5,
|
|
9
|
+
title: 'hi ho',
|
|
10
|
+
content: 'whatever',
|
|
11
|
+
tags: ['a', 'best', 'react'],
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
// Helper to ensure reactivity has settled
|
|
15
|
+
async function waitForResult() {
|
|
16
|
+
await nextTick();
|
|
17
|
+
await nextTick(); // One more tick to ensure reactivity is settled
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
describe('basic behaviors', () => {
|
|
21
|
+
it('should execute a composable and return its result', async () => {
|
|
22
|
+
const composable = () => {
|
|
23
|
+
const count = ref(0);
|
|
24
|
+
return { count };
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const { result, cleanup } = await renderDataCompose(composable);
|
|
28
|
+
|
|
29
|
+
// Wait for composable to execute and result to be available
|
|
30
|
+
await waitForResult();
|
|
31
|
+
|
|
32
|
+
expect(result).toBeDefined();
|
|
33
|
+
// Access the ref's value directly
|
|
34
|
+
expect(result.count.value).toBe(0);
|
|
35
|
+
|
|
36
|
+
cleanup();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('should provide a controller', async () => {
|
|
40
|
+
const composable = () => {
|
|
41
|
+
return { value: 'test' };
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const { controller, cleanup } = await renderDataCompose(composable);
|
|
45
|
+
|
|
46
|
+
expect(controller).toBeDefined();
|
|
47
|
+
expect(controller.fetch).toBeDefined();
|
|
48
|
+
expect(controller.setResponse).toBeDefined();
|
|
49
|
+
|
|
50
|
+
cleanup();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('should pass props to composable', async () => {
|
|
54
|
+
const composable = (props: { message: string; count: number }) => {
|
|
55
|
+
const computed_message = computed(
|
|
56
|
+
() => `${props.message}: ${props.count}`,
|
|
57
|
+
);
|
|
58
|
+
return { message: computed_message };
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const props = reactive({ message: 'Hello', count: 42 });
|
|
62
|
+
const { result, cleanup } = await renderDataCompose(composable, {
|
|
63
|
+
props,
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
// Wait for composable to execute and result to be available
|
|
67
|
+
await waitForResult();
|
|
68
|
+
|
|
69
|
+
expect(result).toBeDefined();
|
|
70
|
+
expect(result.message.value).toBe('Hello: 42');
|
|
71
|
+
|
|
72
|
+
cleanup();
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('should handle computed values in composables', async () => {
|
|
76
|
+
const composable = () => {
|
|
77
|
+
const count = ref(10);
|
|
78
|
+
const doubled = computed(() => count.value * 2);
|
|
79
|
+
return { count, doubled };
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const { result, cleanup } = await renderDataCompose(composable);
|
|
83
|
+
|
|
84
|
+
// Wait for composable to execute and result to be available
|
|
85
|
+
await waitForResult();
|
|
86
|
+
|
|
87
|
+
expect(result.count.value).toBe(10);
|
|
88
|
+
expect(result.doubled.value).toBe(20);
|
|
89
|
+
|
|
90
|
+
cleanup();
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('should provide initial fixtures to the store', async () => {
|
|
94
|
+
const composable = () => {
|
|
95
|
+
return { test: 'value' };
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const { controller, cleanup } = await renderDataCompose(composable, {
|
|
99
|
+
initialFixtures: [
|
|
100
|
+
{
|
|
101
|
+
endpoint: CoolerArticleResource.get,
|
|
102
|
+
args: [{ id: payload.id }],
|
|
103
|
+
response: payload,
|
|
104
|
+
},
|
|
105
|
+
],
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
// Just verify that we can render with fixtures
|
|
109
|
+
expect(controller).toBeDefined();
|
|
110
|
+
|
|
111
|
+
cleanup();
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('should cleanup properly', async () => {
|
|
115
|
+
const composable = () => {
|
|
116
|
+
return { value: 'test' };
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
const { wrapper, cleanup } = await renderDataCompose(composable);
|
|
120
|
+
|
|
121
|
+
expect(wrapper.find('[data-testid="composable-result"]').exists()).toBe(
|
|
122
|
+
true,
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
// Cleanup should not throw
|
|
126
|
+
expect(() => cleanup()).not.toThrow();
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('should handle allSettled()', async () => {
|
|
130
|
+
const composable = () => {
|
|
131
|
+
return { value: 'test' };
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
const { allSettled, cleanup } = await renderDataCompose(composable);
|
|
135
|
+
|
|
136
|
+
const result = await allSettled();
|
|
137
|
+
|
|
138
|
+
expect(Array.isArray(result)).toBe(true);
|
|
139
|
+
|
|
140
|
+
cleanup();
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
describe('reactive prop changes', () => {
|
|
145
|
+
it('should trigger computed values when props change', async () => {
|
|
146
|
+
const composable = (props: { value: number }) => {
|
|
147
|
+
const doubled = computed(() => props.value * 2);
|
|
148
|
+
return { doubled };
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
const props = reactive({ value: 5 });
|
|
152
|
+
const { result, cleanup } = await renderDataCompose(composable, {
|
|
153
|
+
props,
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
// Wait for composable to execute and result to be available
|
|
157
|
+
await waitForResult();
|
|
158
|
+
|
|
159
|
+
// Initial computed value
|
|
160
|
+
expect(result.doubled.value).toBe(10);
|
|
161
|
+
|
|
162
|
+
// Change prop - computed should update
|
|
163
|
+
props.value = 10;
|
|
164
|
+
await nextTick();
|
|
165
|
+
|
|
166
|
+
expect(result.doubled.value).toBe(20);
|
|
167
|
+
|
|
168
|
+
cleanup();
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('should trigger watchers when props change', async () => {
|
|
172
|
+
const watchSpy = jest.fn();
|
|
173
|
+
|
|
174
|
+
const composable = (props: { count: number }) => {
|
|
175
|
+
watch(
|
|
176
|
+
() => props.count,
|
|
177
|
+
(newVal, oldVal) => {
|
|
178
|
+
watchSpy(newVal, oldVal);
|
|
179
|
+
},
|
|
180
|
+
);
|
|
181
|
+
|
|
182
|
+
return { count: computed(() => props.count) };
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
const props = reactive({ count: 1 });
|
|
186
|
+
const { result, cleanup } = await renderDataCompose(composable, {
|
|
187
|
+
props,
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
// Wait for composable to execute and result to be available
|
|
191
|
+
await waitForResult();
|
|
192
|
+
|
|
193
|
+
// Initial value
|
|
194
|
+
expect(result.count.value).toBe(1);
|
|
195
|
+
expect(watchSpy).not.toHaveBeenCalled(); // watch doesn't fire on initial setup
|
|
196
|
+
|
|
197
|
+
// Change prop - watcher should fire
|
|
198
|
+
props.count = 2;
|
|
199
|
+
await nextTick();
|
|
200
|
+
|
|
201
|
+
expect(result.count.value).toBe(2);
|
|
202
|
+
expect(watchSpy).toHaveBeenCalledWith(2, 1);
|
|
203
|
+
|
|
204
|
+
// Change again
|
|
205
|
+
props.count = 5;
|
|
206
|
+
await nextTick();
|
|
207
|
+
|
|
208
|
+
expect(result.count.value).toBe(5);
|
|
209
|
+
expect(watchSpy).toHaveBeenCalledWith(5, 2);
|
|
210
|
+
|
|
211
|
+
cleanup();
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it('should handle multiple computed dependencies on props', async () => {
|
|
215
|
+
const composable = (props: { firstName: string; lastName: string }) => {
|
|
216
|
+
const fullName = computed(() => `${props.firstName} ${props.lastName}`);
|
|
217
|
+
const initials = computed(
|
|
218
|
+
() => `${props.firstName[0]}${props.lastName[0]}`,
|
|
219
|
+
);
|
|
220
|
+
|
|
221
|
+
return { fullName, initials };
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
const props = reactive({
|
|
225
|
+
firstName: 'John',
|
|
226
|
+
lastName: 'Doe',
|
|
227
|
+
});
|
|
228
|
+
const { result, cleanup } = await renderDataCompose(composable, {
|
|
229
|
+
props,
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
// Wait for composable to execute and result to be available
|
|
233
|
+
await waitForResult();
|
|
234
|
+
|
|
235
|
+
// Initial computed values
|
|
236
|
+
expect(result.fullName.value).toBe('John Doe');
|
|
237
|
+
expect(result.initials.value).toBe('JD');
|
|
238
|
+
|
|
239
|
+
// Change first name
|
|
240
|
+
props.firstName = 'Jane';
|
|
241
|
+
await nextTick();
|
|
242
|
+
|
|
243
|
+
expect(result.fullName.value).toBe('Jane Doe');
|
|
244
|
+
expect(result.initials.value).toBe('JD');
|
|
245
|
+
|
|
246
|
+
// Change last name
|
|
247
|
+
props.lastName = 'Smith';
|
|
248
|
+
await nextTick();
|
|
249
|
+
|
|
250
|
+
expect(result.fullName.value).toBe('Jane Smith');
|
|
251
|
+
expect(result.initials.value).toBe('JS');
|
|
252
|
+
|
|
253
|
+
cleanup();
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
it('should update nested computed values when props change', async () => {
|
|
257
|
+
const composable = (props: { value: number }) => {
|
|
258
|
+
const doubled = computed(() => props.value * 2);
|
|
259
|
+
const quadrupled = computed(() => doubled.value * 2);
|
|
260
|
+
const octupled = computed(() => quadrupled.value * 2);
|
|
261
|
+
|
|
262
|
+
return { doubled, quadrupled, octupled };
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
const props = reactive({ value: 1 });
|
|
266
|
+
const { result, cleanup } = await renderDataCompose(composable, {
|
|
267
|
+
props,
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
// Wait for composable to execute and result to be available
|
|
271
|
+
await waitForResult();
|
|
272
|
+
|
|
273
|
+
// Initial values
|
|
274
|
+
expect(result.doubled.value).toBe(2);
|
|
275
|
+
expect(result.quadrupled.value).toBe(4);
|
|
276
|
+
expect(result.octupled.value).toBe(8);
|
|
277
|
+
|
|
278
|
+
// Change prop - all computed values should update
|
|
279
|
+
props.value = 5;
|
|
280
|
+
await nextTick();
|
|
281
|
+
|
|
282
|
+
expect(result.doubled.value).toBe(10);
|
|
283
|
+
expect(result.quadrupled.value).toBe(20);
|
|
284
|
+
expect(result.octupled.value).toBe(40);
|
|
285
|
+
|
|
286
|
+
cleanup();
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
it('should handle complex object prop changes', async () => {
|
|
290
|
+
const composable = (props: { user: { name: string; age: number } }) => {
|
|
291
|
+
const description = computed(
|
|
292
|
+
() => `${props.user.name} is ${props.user.age} years old`,
|
|
293
|
+
);
|
|
294
|
+
const isAdult = computed(() => props.user.age >= 18);
|
|
295
|
+
|
|
296
|
+
return { description, isAdult };
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
const props = reactive({
|
|
300
|
+
user: { name: 'Alice', age: 16 },
|
|
301
|
+
});
|
|
302
|
+
const { result, cleanup } = await renderDataCompose(composable, {
|
|
303
|
+
props,
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
// Wait for composable to execute and result to be available
|
|
307
|
+
await waitForResult();
|
|
308
|
+
|
|
309
|
+
// Initial values
|
|
310
|
+
expect(result.description.value).toBe('Alice is 16 years old');
|
|
311
|
+
expect(result.isAdult.value).toBe(false);
|
|
312
|
+
|
|
313
|
+
// Update nested object
|
|
314
|
+
props.user = { name: 'Bob', age: 25 };
|
|
315
|
+
await nextTick();
|
|
316
|
+
|
|
317
|
+
expect(result.description.value).toBe('Bob is 25 years old');
|
|
318
|
+
expect(result.isAdult.value).toBe(true);
|
|
319
|
+
|
|
320
|
+
cleanup();
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
it('should handle array prop changes in computed values', async () => {
|
|
324
|
+
const composable = (props: { items: string[] }) => {
|
|
325
|
+
const itemCount = computed(() => props.items.length);
|
|
326
|
+
const firstItem = computed(() => props.items[0] || 'none');
|
|
327
|
+
const allItems = computed(() => props.items.join(', '));
|
|
328
|
+
|
|
329
|
+
return { itemCount, firstItem, allItems };
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
const props = reactive({
|
|
333
|
+
items: ['apple', 'banana'],
|
|
334
|
+
});
|
|
335
|
+
const { result, cleanup } = await renderDataCompose(composable, {
|
|
336
|
+
props,
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
// Wait for composable to execute and result to be available
|
|
340
|
+
await waitForResult();
|
|
341
|
+
|
|
342
|
+
// Initial values
|
|
343
|
+
expect(result.itemCount.value).toBe(2);
|
|
344
|
+
expect(result.firstItem.value).toBe('apple');
|
|
345
|
+
expect(result.allItems.value).toBe('apple, banana');
|
|
346
|
+
|
|
347
|
+
// Update array
|
|
348
|
+
props.items = ['orange', 'grape', 'melon'];
|
|
349
|
+
await nextTick();
|
|
350
|
+
|
|
351
|
+
expect(result.itemCount.value).toBe(3);
|
|
352
|
+
expect(result.firstItem.value).toBe('orange');
|
|
353
|
+
expect(result.allItems.value).toBe('orange, grape, melon');
|
|
354
|
+
|
|
355
|
+
cleanup();
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
it('should trigger watchers with immediate option on prop changes', async () => {
|
|
359
|
+
const watchSpy = jest.fn();
|
|
360
|
+
|
|
361
|
+
const composable = (props: { value: number }) => {
|
|
362
|
+
watch(
|
|
363
|
+
() => props.value,
|
|
364
|
+
newVal => {
|
|
365
|
+
watchSpy(newVal);
|
|
366
|
+
},
|
|
367
|
+
{ immediate: true },
|
|
368
|
+
);
|
|
369
|
+
|
|
370
|
+
return { value: computed(() => props.value) };
|
|
371
|
+
};
|
|
372
|
+
|
|
373
|
+
const props = reactive({ value: 10 });
|
|
374
|
+
const { result, cleanup } = await renderDataCompose(composable, {
|
|
375
|
+
props,
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
// Wait for composable to execute and result to be available
|
|
379
|
+
await waitForResult();
|
|
380
|
+
|
|
381
|
+
// Immediate watcher should have fired on setup
|
|
382
|
+
expect(watchSpy).toHaveBeenCalledWith(10);
|
|
383
|
+
expect(watchSpy).toHaveBeenCalledTimes(1);
|
|
384
|
+
|
|
385
|
+
// Change prop
|
|
386
|
+
props.value = 20;
|
|
387
|
+
await nextTick();
|
|
388
|
+
|
|
389
|
+
expect(result.value.value).toBe(20);
|
|
390
|
+
expect(watchSpy).toHaveBeenCalledWith(20);
|
|
391
|
+
expect(watchSpy).toHaveBeenCalledTimes(2);
|
|
392
|
+
|
|
393
|
+
cleanup();
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
it('should handle multiple watchers triggered by same prop change', async () => {
|
|
397
|
+
const watcher1Spy = jest.fn();
|
|
398
|
+
const watcher2Spy = jest.fn();
|
|
399
|
+
|
|
400
|
+
const composable = (props: { value: number }) => {
|
|
401
|
+
watch(
|
|
402
|
+
() => props.value,
|
|
403
|
+
newVal => {
|
|
404
|
+
watcher1Spy(newVal);
|
|
405
|
+
},
|
|
406
|
+
);
|
|
407
|
+
|
|
408
|
+
watch(
|
|
409
|
+
() => props.value * 2,
|
|
410
|
+
newVal => {
|
|
411
|
+
watcher2Spy(newVal);
|
|
412
|
+
},
|
|
413
|
+
);
|
|
414
|
+
|
|
415
|
+
return { value: computed(() => props.value) };
|
|
416
|
+
};
|
|
417
|
+
|
|
418
|
+
const props = reactive({ value: 5 });
|
|
419
|
+
const { result, cleanup } = await renderDataCompose(composable, {
|
|
420
|
+
props,
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
// Wait for composable to execute and result to be available
|
|
424
|
+
await waitForResult();
|
|
425
|
+
|
|
426
|
+
expect(watcher1Spy).not.toHaveBeenCalled();
|
|
427
|
+
expect(watcher2Spy).not.toHaveBeenCalled();
|
|
428
|
+
|
|
429
|
+
// Change prop - both watchers should fire
|
|
430
|
+
props.value = 10;
|
|
431
|
+
await nextTick();
|
|
432
|
+
|
|
433
|
+
expect(result.value.value).toBe(10);
|
|
434
|
+
expect(watcher1Spy).toHaveBeenCalledWith(10);
|
|
435
|
+
expect(watcher2Spy).toHaveBeenCalledWith(20);
|
|
436
|
+
|
|
437
|
+
cleanup();
|
|
438
|
+
});
|
|
439
|
+
|
|
440
|
+
it('should handle conditional computed values based on props', async () => {
|
|
441
|
+
const composable = (props: { enabled: boolean; value: number }) => {
|
|
442
|
+
const result = computed(() => {
|
|
443
|
+
if (props.enabled) {
|
|
444
|
+
return props.value * 2;
|
|
445
|
+
}
|
|
446
|
+
return 0;
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
return { result };
|
|
450
|
+
};
|
|
451
|
+
|
|
452
|
+
const props = reactive({ enabled: true, value: 5 });
|
|
453
|
+
const { result, cleanup } = await renderDataCompose(composable, {
|
|
454
|
+
props,
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
// Wait for composable to execute and result to be available
|
|
458
|
+
await waitForResult();
|
|
459
|
+
|
|
460
|
+
// Initially enabled
|
|
461
|
+
expect(result.result.value).toBe(10);
|
|
462
|
+
|
|
463
|
+
// Disable
|
|
464
|
+
props.enabled = false;
|
|
465
|
+
await nextTick();
|
|
466
|
+
|
|
467
|
+
expect(result.result.value).toBe(0);
|
|
468
|
+
|
|
469
|
+
// Change value while disabled
|
|
470
|
+
props.value = 100;
|
|
471
|
+
await nextTick();
|
|
472
|
+
|
|
473
|
+
expect(result.result.value).toBe(0);
|
|
474
|
+
|
|
475
|
+
// Re-enable
|
|
476
|
+
props.enabled = true;
|
|
477
|
+
await nextTick();
|
|
478
|
+
|
|
479
|
+
expect(result.result.value).toBe(200);
|
|
480
|
+
|
|
481
|
+
cleanup();
|
|
482
|
+
});
|
|
483
|
+
});
|
|
484
|
+
|
|
485
|
+
describe('advanced reactivity patterns', () => {
|
|
486
|
+
it('should handle ref mutations inside composable that respond to prop changes', async () => {
|
|
487
|
+
const composable = (props: { multiplier: number }) => {
|
|
488
|
+
const base = ref(10);
|
|
489
|
+
const result = computed(() => base.value * props.multiplier);
|
|
490
|
+
|
|
491
|
+
const updateBase = (newValue: number) => {
|
|
492
|
+
base.value = newValue;
|
|
493
|
+
};
|
|
494
|
+
|
|
495
|
+
return { result, updateBase, base };
|
|
496
|
+
};
|
|
497
|
+
|
|
498
|
+
const props = reactive({ multiplier: 2 });
|
|
499
|
+
const { result, cleanup } = await renderDataCompose(composable, {
|
|
500
|
+
props,
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
// Wait for composable to execute and result to be available
|
|
504
|
+
await waitForResult();
|
|
505
|
+
|
|
506
|
+
// Initial
|
|
507
|
+
expect(result.result.value).toBe(20); // 10 * 2
|
|
508
|
+
|
|
509
|
+
// Change prop
|
|
510
|
+
props.multiplier = 3;
|
|
511
|
+
await nextTick();
|
|
512
|
+
|
|
513
|
+
expect(result.result.value).toBe(30); // 10 * 3
|
|
514
|
+
|
|
515
|
+
// Mutate internal ref
|
|
516
|
+
result.updateBase(5);
|
|
517
|
+
await nextTick();
|
|
518
|
+
|
|
519
|
+
expect(result.result.value).toBe(15); // 5 * 3
|
|
520
|
+
|
|
521
|
+
// Change prop again
|
|
522
|
+
props.multiplier = 4;
|
|
523
|
+
await nextTick();
|
|
524
|
+
|
|
525
|
+
expect(result.result.value).toBe(20); // 5 * 4
|
|
526
|
+
|
|
527
|
+
cleanup();
|
|
528
|
+
});
|
|
529
|
+
|
|
530
|
+
it('should handle side effects in watchers triggered by prop changes', async () => {
|
|
531
|
+
const sideEffectSpy = jest.fn();
|
|
532
|
+
|
|
533
|
+
const composable = (props: { trigger: boolean }) => {
|
|
534
|
+
const counter = ref(0);
|
|
535
|
+
|
|
536
|
+
watch(
|
|
537
|
+
() => props.trigger,
|
|
538
|
+
trigger => {
|
|
539
|
+
if (trigger) {
|
|
540
|
+
counter.value++;
|
|
541
|
+
sideEffectSpy(counter.value);
|
|
542
|
+
}
|
|
543
|
+
},
|
|
544
|
+
);
|
|
545
|
+
|
|
546
|
+
return { counter };
|
|
547
|
+
};
|
|
548
|
+
|
|
549
|
+
const props = reactive({ trigger: false });
|
|
550
|
+
const { result, cleanup } = await renderDataCompose(composable, {
|
|
551
|
+
props,
|
|
552
|
+
});
|
|
553
|
+
|
|
554
|
+
// Wait for composable to execute and result to be available
|
|
555
|
+
await waitForResult();
|
|
556
|
+
|
|
557
|
+
// Initial state
|
|
558
|
+
expect(result.counter.value).toBe(0);
|
|
559
|
+
expect(sideEffectSpy).not.toHaveBeenCalled();
|
|
560
|
+
|
|
561
|
+
// Trigger side effect
|
|
562
|
+
props.trigger = true;
|
|
563
|
+
await nextTick();
|
|
564
|
+
|
|
565
|
+
expect(result.counter.value).toBe(1);
|
|
566
|
+
expect(sideEffectSpy).toHaveBeenCalledWith(1);
|
|
567
|
+
|
|
568
|
+
// Toggle back (no increment)
|
|
569
|
+
props.trigger = false;
|
|
570
|
+
await nextTick();
|
|
571
|
+
|
|
572
|
+
expect(result.counter.value).toBe(1);
|
|
573
|
+
expect(sideEffectSpy).toHaveBeenCalledTimes(1);
|
|
574
|
+
|
|
575
|
+
// Trigger again
|
|
576
|
+
props.trigger = true;
|
|
577
|
+
await nextTick();
|
|
578
|
+
|
|
579
|
+
expect(result.counter.value).toBe(2);
|
|
580
|
+
expect(sideEffectSpy).toHaveBeenCalledWith(2);
|
|
581
|
+
expect(sideEffectSpy).toHaveBeenCalledTimes(2);
|
|
582
|
+
|
|
583
|
+
cleanup();
|
|
584
|
+
});
|
|
585
|
+
});
|
|
586
|
+
});
|
package/src/test/index.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
export { default as MockController } from './MockController.js';
|
|
2
|
-
export {
|
|
2
|
+
export { mountDataClient } from './mountDataClient.js';
|
|
3
|
+
export { renderDataCompose } from './renderDataCompose.js';
|
|
3
4
|
export type {
|
|
4
5
|
RenderDataClientOptions,
|
|
5
6
|
RenderDataClientResult,
|
|
6
|
-
} from './
|
|
7
|
+
} from './mountDataClient.js';
|
|
7
8
|
export type {
|
|
8
9
|
FixtureEndpoint,
|
|
9
10
|
SuccessFixtureEndpoint,
|
|
@@ -14,10 +15,3 @@ export type {
|
|
|
14
15
|
Interceptor,
|
|
15
16
|
} from './fixtureTypes.js';
|
|
16
17
|
export { default as mockInitialState } from './mockState.js';
|
|
17
|
-
|
|
18
|
-
// Re-export DataClientPlugin and createDataClient for testing
|
|
19
|
-
export { DataClientPlugin, createDataClient } from '../providers/index.js';
|
|
20
|
-
export type {
|
|
21
|
-
ProvideOptions,
|
|
22
|
-
ProvidedDataClient,
|
|
23
|
-
} from '../providers/createDataClient.js';
|