@data-client/vue 0.18.1 → 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.
Files changed (112) hide show
  1. package/CHANGELOG.md +371 -0
  2. package/README.md +86 -41
  3. package/dist/index.js +223 -158
  4. package/dist/index.umd.min.js +1 -1
  5. package/lib/consumers/refetchTriggers.d.ts +12 -0
  6. package/lib/consumers/refetchTriggers.d.ts.map +1 -0
  7. package/lib/consumers/refetchTriggers.js +9 -0
  8. package/lib/consumers/useCache.d.ts +1 -1
  9. package/lib/consumers/useCache.d.ts.map +1 -1
  10. package/lib/consumers/useCache.js +3 -53
  11. package/lib/consumers/useCacheResponse.d.ts +20 -0
  12. package/lib/consumers/useCacheResponse.d.ts.map +1 -0
  13. package/lib/consumers/useCacheResponse.js +55 -0
  14. package/lib/consumers/useCountRef.d.ts +7 -0
  15. package/lib/consumers/useCountRef.d.ts.map +1 -0
  16. package/lib/consumers/useCountRef.js +10 -0
  17. package/lib/consumers/useDLE.d.ts +1 -1
  18. package/lib/consumers/useDLE.d.ts.map +1 -1
  19. package/lib/consumers/useDLE.js +16 -62
  20. package/lib/consumers/useDebounce.d.ts +1 -1
  21. package/lib/consumers/useDebounce.d.ts.map +1 -1
  22. package/lib/consumers/useDebounce.js +1 -2
  23. package/lib/consumers/useExpired.d.ts +12 -0
  24. package/lib/consumers/useExpired.d.ts.map +1 -0
  25. package/lib/consumers/useExpired.js +19 -0
  26. package/lib/consumers/useFetch.d.ts +8 -11
  27. package/lib/consumers/useFetch.d.ts.map +1 -1
  28. package/lib/consumers/useFetch.js +13 -24
  29. package/lib/consumers/useLive.js +1 -1
  30. package/lib/consumers/useLoading.d.ts +1 -1
  31. package/lib/consumers/useLoading.d.ts.map +1 -1
  32. package/lib/consumers/useLoading.js +1 -2
  33. package/lib/consumers/useQuery.d.ts +1 -1
  34. package/lib/consumers/useQuery.d.ts.map +1 -1
  35. package/lib/consumers/useQuery.js +6 -12
  36. package/lib/consumers/useResponseMeta.d.ts +23 -0
  37. package/lib/consumers/useResponseMeta.d.ts.map +1 -0
  38. package/lib/consumers/useResponseMeta.js +36 -0
  39. package/lib/consumers/useSubscription.js +4 -4
  40. package/lib/consumers/useSuspense.d.ts +1 -1
  41. package/lib/consumers/useSuspense.d.ts.map +1 -1
  42. package/lib/consumers/useSuspense.js +58 -34
  43. package/lib/context.d.ts +1 -1
  44. package/lib/context.d.ts.map +1 -1
  45. package/lib/context.js +3 -3
  46. package/lib/index.d.ts +2 -1
  47. package/lib/index.d.ts.map +1 -1
  48. package/lib/index.js +4 -2
  49. package/lib/internal.d.ts +6 -0
  50. package/lib/internal.d.ts.map +1 -0
  51. package/lib/internal.js +6 -0
  52. package/lib/providers/DataClientPlugin.d.ts +1 -1
  53. package/lib/providers/DataClientPlugin.d.ts.map +1 -1
  54. package/lib/providers/DataClientPlugin.js +16 -4
  55. package/lib/providers/createDataClient.d.ts +10 -4
  56. package/lib/providers/createDataClient.d.ts.map +1 -1
  57. package/lib/providers/createDataClient.js +11 -5
  58. package/lib/providers/getDefaultManagers.d.ts.map +1 -1
  59. package/lib/providers/getDefaultManagers.js +1 -1
  60. package/lib/providers/index.d.ts +2 -1
  61. package/lib/providers/index.d.ts.map +1 -1
  62. package/lib/providers/index.js +1 -1
  63. package/lib/test/MockPlugin.d.ts +1 -1
  64. package/lib/test/MockPlugin.d.ts.map +1 -1
  65. package/lib/test/MockPlugin.js +1 -1
  66. package/lib/test/index.d.ts +2 -1
  67. package/lib/test/index.d.ts.map +1 -1
  68. package/lib/test/index.js +1 -1
  69. package/lib/test/mountDataClient.d.ts +5 -3
  70. package/lib/test/mountDataClient.d.ts.map +1 -1
  71. package/lib/test/mountDataClient.js +15 -10
  72. package/lib/test/renderDataCompose.d.ts +10 -4
  73. package/lib/test/renderDataCompose.d.ts.map +1 -1
  74. package/lib/test/renderDataCompose.js +2 -3
  75. package/package.json +20 -5
  76. package/src/__tests__/__snapshots__/context.test.ts.snap +2 -2
  77. package/src/__tests__/context.test.ts +1 -1
  78. package/src/__tests__/integration-garbage-collection.web.ts +80 -4
  79. package/src/__tests__/useCache.web.ts +160 -93
  80. package/src/__tests__/useDLE.web.ts +204 -96
  81. package/src/__tests__/useFetch.web.ts +100 -37
  82. package/src/__tests__/useQuery.web.ts +60 -50
  83. package/src/__tests__/useSubscription.web.ts +74 -75
  84. package/src/__tests__/useSuspense.web.ts +455 -46
  85. package/src/consumers/refetchTriggers.ts +22 -0
  86. package/src/consumers/useCache.ts +7 -70
  87. package/src/consumers/useCacheResponse.ts +56 -0
  88. package/src/consumers/useCountRef.ts +15 -0
  89. package/src/consumers/useDLE.ts +24 -83
  90. package/src/consumers/useDebounce.ts +2 -1
  91. package/src/consumers/useExpired.ts +26 -0
  92. package/src/consumers/useFetch.ts +34 -49
  93. package/src/consumers/useLive.ts +1 -1
  94. package/src/consumers/useLoading.ts +2 -1
  95. package/src/consumers/useQuery.ts +10 -13
  96. package/src/consumers/useResponseMeta.ts +41 -0
  97. package/src/consumers/useSubscription.ts +3 -3
  98. package/src/consumers/useSuspense.ts +66 -58
  99. package/src/context.ts +4 -3
  100. package/src/index.ts +7 -1
  101. package/src/internal.ts +12 -0
  102. package/src/providers/DataClientPlugin.ts +17 -3
  103. package/src/providers/__tests__/DataClientPlugin.test.ts +40 -1
  104. package/src/providers/createDataClient.ts +15 -5
  105. package/src/providers/getDefaultManagers.ts +2 -6
  106. package/src/providers/index.ts +2 -5
  107. package/src/test/MockPlugin.ts +3 -3
  108. package/src/test/README.md +10 -304
  109. package/src/test/__tests__/mountDataClient.test.ts +38 -0
  110. package/src/test/index.ts +2 -1
  111. package/src/test/mountDataClient.ts +22 -17
  112. 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, waitForNextUpdate } = await renderDataCompose(
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 waitForNextUpdate();
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, waitForNextUpdate } = await renderDataCompose(
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 waitForNextUpdate();
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('should update result when Entity args change', async () => {
373
- const payload1 = {
374
- id: 1,
375
- title: 'First Article',
376
- slug: 'first-article',
377
- content: 'content 1',
378
- tags: ['tag1'],
379
- };
380
- const payload2 = {
381
- id: 2,
382
- title: 'Second Article',
383
- slug: 'second-article',
384
- content: 'content 2',
385
- tags: ['tag2'],
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
- const props = reactive({ id: 1 });
389
- const { result } = await renderDataCompose(
390
- () => {
391
- return useQuery(
392
- ArticleWithSlug,
393
- computed(() => ({ id: props.id })),
394
- );
395
- },
396
- {
397
- initialFixtures: [
398
- {
399
- endpoint: ArticleSlugResource.get,
400
- args: [{ id: 1 }],
401
- response: payload1,
402
- },
403
- {
404
- endpoint: ArticleSlugResource.get,
405
- args: [{ id: 2 }],
406
- response: payload2,
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
- expect(result.value).toEqual(ArticleWithSlug.fromJS(payload1));
413
- expect(result.value?.id).toBe(1);
414
- expect(result.value?.title).toBe('First Article');
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
- props.id = 2;
417
- expect(result.value).toEqual(ArticleWithSlug.fromJS(payload2));
418
- expect(result.value?.id).toBe(2);
419
- expect(result.value?.title).toBe('Second Article');
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, waitForNextUpdate, allSettled, cleanup } =
49
- await renderDataCompose(
50
- ({ active }: { active: boolean }) => {
51
- const args = computed(() => (active ? { id: payload.id } : null));
52
- useSubscription(PollingArticleResource.get, args);
53
- return useSuspense(PollingArticleResource.get, {
54
- id: payload.id,
55
- });
56
- },
57
- {
58
- props: propsRef,
59
- resolverFixtures: [
60
- {
61
- endpoint: PollingArticleResource.get,
62
- response: responseMock,
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('should not receive updates after unsubscribing by setting active to null', async () => {
100
- jest.useFakeTimers();
101
- const frequency = PollingArticleResource.get.pollFrequency as number;
102
- expect(frequency).toBe(5000);
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
- const responseMock = jest.fn(() => payload);
105
- const propsRef = reactive({ active: true });
110
+ const responseMock = jest.fn(() => payload);
111
+ const propsRef = reactive({ active: true });
106
112
 
107
- const { result, waitForNextUpdate, allSettled, cleanup } =
108
- await renderDataCompose(
113
+ const { result, allSettled, cleanup } = await renderDataCompose(
109
114
  (props: { active: boolean }) => {
110
- const args = computed(() =>
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
- // Wait for initial render
130
- jest.advanceTimersByTime(frequency);
131
- await allSettled();
132
- await waitForNextUpdate();
132
+ // Wait for initial render
133
+ jest.advanceTimersByTime(frequency);
134
+ await allSettled();
133
135
 
134
- // Verify initial values
135
- const initialArticleRef = await result;
136
- expect(initialArticleRef!.value!.title).toBe(payload.title);
137
- expect(responseMock).toHaveBeenCalledTimes(1);
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
- // Change the mock response
140
- responseMock.mockReturnValue({
141
- ...payload,
142
- title: 'after first poll',
143
- });
141
+ // Change the mock response
142
+ responseMock.mockReturnValue({
143
+ ...payload,
144
+ title: 'after first poll',
145
+ });
144
146
 
145
- // Advance time to trigger another poll
146
- jest.advanceTimersByTime(frequency);
147
- await allSettled();
148
- await nextTick();
147
+ // Advance time to trigger another poll
148
+ jest.advanceTimersByTime(frequency);
149
+ await allSettled();
149
150
 
150
- // Verify the article was updated
151
- const updatedArticleRef = await result;
152
- expect(updatedArticleRef!.value!.title).toBe('after first poll');
153
- expect(responseMock).toHaveBeenCalledTimes(2);
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
- // Now unsubscribe by setting active to false
156
- propsRef.active = false;
157
- await nextTick();
156
+ // Now unsubscribe by setting active to false
157
+ propsRef.active = false;
158
+ await nextTick();
158
159
 
159
- // Change the mock response again
160
- responseMock.mockReturnValue({
161
- ...payload,
162
- title: 'should not see this',
163
- });
160
+ // Change the mock response again
161
+ responseMock.mockReturnValue({
162
+ ...payload,
163
+ title: 'should not see this',
164
+ });
164
165
 
165
- // Advance time - subscription should not trigger
166
- jest.advanceTimersByTime(frequency);
167
- await allSettled();
168
- await nextTick();
166
+ // Advance time - subscription should not trigger
167
+ jest.advanceTimersByTime(frequency);
168
+ await allSettled();
169
169
 
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);
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
- jest.useRealTimers();
177
- cleanup();
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');