@data-client/vue 0.0.4 → 0.1.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 +10 -4
- package/dist/index.js +114 -34
- package/dist/index.umd.min.js +1 -1
- package/lib/consumers/useFetch.js +12 -8
- package/lib/consumers/useSuspense.js +38 -13
- package/lib/context.d.ts.map +1 -1
- package/lib/context.js +3 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/providers/DataClientPlugin.d.ts +22 -0
- package/lib/providers/DataClientPlugin.d.ts.map +1 -0
- package/lib/providers/DataClientPlugin.js +40 -0
- package/lib/providers/{provideDataClient.d.ts → createDataClient.d.ts} +10 -6
- package/lib/providers/createDataClient.d.ts.map +1 -0
- package/lib/providers/createDataClient.js +103 -0
- package/lib/providers/index.d.ts +2 -1
- package/lib/providers/index.d.ts.map +1 -1
- package/lib/providers/index.js +3 -2
- package/lib/test/MockController.d.ts +8 -0
- package/lib/test/MockController.d.ts.map +1 -0
- package/lib/test/MockController.js +125 -0
- package/lib/test/collapseFixture.d.ts +6 -0
- package/lib/test/collapseFixture.d.ts.map +1 -0
- package/lib/test/collapseFixture.js +19 -0
- package/lib/test/createFixtureMap.d.ts +3 -0
- package/lib/test/createFixtureMap.d.ts.map +1 -0
- package/lib/test/createFixtureMap.js +23 -0
- package/lib/test/fixtureTypes.d.ts +99 -0
- package/lib/test/fixtureTypes.d.ts.map +1 -0
- package/lib/test/fixtureTypes.js +2 -0
- package/lib/test/index.d.ts +8 -0
- package/lib/test/index.d.ts.map +1 -0
- package/lib/test/index.js +7 -0
- package/lib/test/mockState.d.ts +4 -0
- package/lib/test/mockState.d.ts.map +1 -0
- package/lib/test/mockState.js +44 -0
- package/lib/test/renderDataClient.d.ts +40 -0
- package/lib/test/renderDataClient.d.ts.map +1 -0
- package/lib/test/renderDataClient.js +264 -0
- package/package.json +13 -6
- package/src/__tests__/__snapshots__/useQuery.web.ts.snap +126 -0
- package/src/__tests__/__snapshots__/useSubscription.web.ts.snap +7 -0
- package/src/__tests__/integration-garbage-collection.web.ts +155 -0
- package/src/__tests__/useFetch.web.ts +192 -51
- package/src/__tests__/useLive.web.ts +66 -34
- package/src/__tests__/useQuery.web.ts +263 -109
- package/src/__tests__/useSubscription.web.ts +16 -3
- package/src/__tests__/useSuspense.web.ts +141 -70
- package/src/consumers/useFetch.ts +16 -9
- package/src/consumers/useSuspense.ts +37 -15
- package/src/context.ts +2 -0
- package/src/providers/DataClientPlugin.ts +39 -0
- package/src/providers/__tests__/DataClientPlugin.test.ts +387 -0
- package/src/providers/{provideDataClient.ts → createDataClient.ts} +33 -24
- package/src/providers/index.ts +2 -1
- package/src/test/MockController.ts +151 -0
- package/src/test/README.md +245 -0
- package/src/test/collapseFixture.ts +21 -0
- package/src/test/createFixtureMap.ts +26 -0
- package/src/test/fixtureTypes.ts +121 -0
- package/src/test/index.ts +23 -0
- package/src/test/mockState.ts +57 -0
- package/src/test/renderDataClient.ts +316 -0
- package/ts3.4/providers/DataClientPlugin.d.ts +22 -0
- package/ts3.4/providers/createDataClient.d.ts +25 -0
- package/ts3.4/providers/index.d.ts +2 -1
- package/ts3.4/test/MockController.d.ts +8 -0
- package/ts3.4/test/collapseFixture.d.ts +6 -0
- package/ts3.4/test/createFixtureMap.d.ts +6 -0
- package/ts3.4/test/fixtureTypes.d.ts +99 -0
- package/ts3.4/test/index.d.ts +8 -0
- package/ts3.4/test/mockState.d.ts +4 -0
- package/ts3.4/test/renderDataClient.d.ts +40 -0
- package/lib/providers/provideDataClient.d.ts.map +0 -1
- package/lib/providers/provideDataClient.js +0 -94
- package/ts3.4/providers/provideDataClient.d.ts +0 -21
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { GCPolicy } from '@data-client/core';
|
|
2
|
+
import { defineComponent, h } from 'vue';
|
|
3
|
+
|
|
4
|
+
import { Article, ArticleResource } from '../../../../__tests__/new';
|
|
5
|
+
import useQuery from '../consumers/useQuery';
|
|
6
|
+
import useSuspense from '../consumers/useSuspense';
|
|
7
|
+
import {
|
|
8
|
+
renderDataComposable,
|
|
9
|
+
renderDataClient,
|
|
10
|
+
} from '../test/renderDataClient';
|
|
11
|
+
|
|
12
|
+
const GC_INTERVAL = 100; // Use short interval for faster tests
|
|
13
|
+
|
|
14
|
+
describe('Integration Garbage Collection Web (Vue)', () => {
|
|
15
|
+
it('should initialize with GCPolicy', () => {
|
|
16
|
+
const gcPolicy = new GCPolicy({
|
|
17
|
+
intervalMS: GC_INTERVAL,
|
|
18
|
+
expiryMultiplier: 2,
|
|
19
|
+
});
|
|
20
|
+
expect(gcPolicy).toBeDefined();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('should accept gcPolicy option in renderDataClient', () => {
|
|
24
|
+
const TestComp = defineComponent({
|
|
25
|
+
name: 'TestComp',
|
|
26
|
+
setup() {
|
|
27
|
+
return () => h('div', 'Hello');
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const gcPolicy = new GCPolicy({
|
|
32
|
+
intervalMS: GC_INTERVAL,
|
|
33
|
+
expiryMultiplier: 2,
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const { wrapper, cleanup } = renderDataClient(TestComp, {
|
|
37
|
+
gcPolicy,
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
expect(wrapper.text()).toContain('Hello');
|
|
41
|
+
cleanup();
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('should work with useSuspense and GCPolicy', async () => {
|
|
45
|
+
const articleData = {
|
|
46
|
+
id: 1,
|
|
47
|
+
title: 'Test Article',
|
|
48
|
+
content: 'Test Content',
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const { result, cleanup, waitForNextUpdate } = renderDataComposable(
|
|
52
|
+
() => useSuspense(ArticleResource.get, { id: 1 }),
|
|
53
|
+
{
|
|
54
|
+
initialFixtures: [
|
|
55
|
+
{
|
|
56
|
+
endpoint: ArticleResource.get,
|
|
57
|
+
args: [{ id: 1 }],
|
|
58
|
+
response: articleData,
|
|
59
|
+
},
|
|
60
|
+
],
|
|
61
|
+
gcPolicy: new GCPolicy({
|
|
62
|
+
intervalMS: GC_INTERVAL,
|
|
63
|
+
expiryMultiplier: 2,
|
|
64
|
+
}),
|
|
65
|
+
},
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
await waitForNextUpdate();
|
|
69
|
+
|
|
70
|
+
const articleRef = await result.current;
|
|
71
|
+
expect(articleRef?.value.title).toBe(articleData.title);
|
|
72
|
+
expect(articleRef?.value.content).toBe(articleData.content);
|
|
73
|
+
|
|
74
|
+
cleanup();
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('should work with useQuery and GCPolicy', () => {
|
|
78
|
+
const articleListData = [
|
|
79
|
+
{ id: 1, title: 'Article 1', content: 'Content 1' },
|
|
80
|
+
{ id: 2, title: 'Article 2', content: 'Content 2' },
|
|
81
|
+
];
|
|
82
|
+
|
|
83
|
+
const { result, cleanup } = renderDataComposable(
|
|
84
|
+
() => useQuery(ArticleResource.getList.schema),
|
|
85
|
+
{
|
|
86
|
+
initialFixtures: [
|
|
87
|
+
{
|
|
88
|
+
endpoint: ArticleResource.getList,
|
|
89
|
+
args: [],
|
|
90
|
+
response: articleListData,
|
|
91
|
+
},
|
|
92
|
+
],
|
|
93
|
+
gcPolicy: new GCPolicy({
|
|
94
|
+
intervalMS: GC_INTERVAL,
|
|
95
|
+
expiryMultiplier: 2,
|
|
96
|
+
}),
|
|
97
|
+
},
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
expect(result.current?.value).toBeDefined();
|
|
101
|
+
expect(result.current?.value?.length).toBe(2);
|
|
102
|
+
expect(result.current?.value?.[0]).toBeInstanceOf(Article);
|
|
103
|
+
|
|
104
|
+
cleanup();
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('should handle different expiryMultiplier values', () => {
|
|
108
|
+
const articleData = {
|
|
109
|
+
id: 1,
|
|
110
|
+
title: 'Test Article',
|
|
111
|
+
content: 'Test Content',
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const TestComp = defineComponent({
|
|
115
|
+
name: 'TestComp',
|
|
116
|
+
setup() {
|
|
117
|
+
return () => h('div', 'Test');
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
// Test with expiryMultiplier of 4
|
|
122
|
+
const { cleanup: cleanup1 } = renderDataClient(TestComp, {
|
|
123
|
+
initialFixtures: [
|
|
124
|
+
{
|
|
125
|
+
endpoint: ArticleResource.get,
|
|
126
|
+
args: [{ id: 1 }],
|
|
127
|
+
response: articleData,
|
|
128
|
+
},
|
|
129
|
+
],
|
|
130
|
+
gcPolicy: new GCPolicy({
|
|
131
|
+
intervalMS: GC_INTERVAL,
|
|
132
|
+
expiryMultiplier: 4,
|
|
133
|
+
}),
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
cleanup1();
|
|
137
|
+
|
|
138
|
+
// Test with expiryMultiplier of 2 (default in tests)
|
|
139
|
+
const { cleanup: cleanup2 } = renderDataClient(TestComp, {
|
|
140
|
+
initialFixtures: [
|
|
141
|
+
{
|
|
142
|
+
endpoint: ArticleResource.get,
|
|
143
|
+
args: [{ id: 1 }],
|
|
144
|
+
response: articleData,
|
|
145
|
+
},
|
|
146
|
+
],
|
|
147
|
+
gcPolicy: new GCPolicy({
|
|
148
|
+
intervalMS: GC_INTERVAL,
|
|
149
|
+
expiryMultiplier: 2,
|
|
150
|
+
}),
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
cleanup2();
|
|
154
|
+
});
|
|
155
|
+
});
|
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
import { mount } from '@vue/test-utils';
|
|
2
2
|
import nock from 'nock';
|
|
3
|
-
import { defineComponent, h, nextTick } from 'vue';
|
|
3
|
+
import { defineComponent, h, nextTick, reactive, inject } from 'vue';
|
|
4
4
|
|
|
5
5
|
// Reuse the same endpoints/fixtures used by the React tests
|
|
6
6
|
import {
|
|
7
7
|
CoolerArticleResource,
|
|
8
8
|
StaticArticleResource,
|
|
9
9
|
} from '../../../../__tests__/new';
|
|
10
|
+
import useFetch from '../consumers/useFetch';
|
|
11
|
+
import { ControllerKey } from '../context';
|
|
12
|
+
import { DataClientPlugin } from '../providers/DataClientPlugin';
|
|
13
|
+
|
|
10
14
|
// Minimal shared fixture (copied from React test fixtures)
|
|
11
15
|
const payload = {
|
|
12
16
|
id: 5,
|
|
@@ -14,8 +18,13 @@ const payload = {
|
|
|
14
18
|
content: 'whatever',
|
|
15
19
|
tags: ['a', 'best', 'react'],
|
|
16
20
|
};
|
|
17
|
-
|
|
18
|
-
|
|
21
|
+
|
|
22
|
+
const payload2 = {
|
|
23
|
+
id: 6,
|
|
24
|
+
title: 'next',
|
|
25
|
+
content: 'my best content yet',
|
|
26
|
+
tags: ['b'],
|
|
27
|
+
};
|
|
19
28
|
|
|
20
29
|
async function flush() {
|
|
21
30
|
await Promise.resolve();
|
|
@@ -55,10 +64,10 @@ describe('vue useFetch()', () => {
|
|
|
55
64
|
nock.cleanAll();
|
|
56
65
|
});
|
|
57
66
|
|
|
58
|
-
const
|
|
59
|
-
name: '
|
|
67
|
+
const TestWrapper = defineComponent({
|
|
68
|
+
name: 'TestWrapper',
|
|
60
69
|
setup(_props, { slots, expose }) {
|
|
61
|
-
const
|
|
70
|
+
const controller = inject(ControllerKey);
|
|
62
71
|
expose({ controller });
|
|
63
72
|
return () => (slots.default ? slots.default() : h('div'));
|
|
64
73
|
},
|
|
@@ -76,8 +85,11 @@ describe('vue useFetch()', () => {
|
|
|
76
85
|
},
|
|
77
86
|
});
|
|
78
87
|
|
|
79
|
-
const wrapper = mount(
|
|
88
|
+
const wrapper = mount(TestWrapper, {
|
|
80
89
|
slots: { default: () => h(Comp) },
|
|
90
|
+
global: {
|
|
91
|
+
plugins: [[DataClientPlugin]],
|
|
92
|
+
},
|
|
81
93
|
});
|
|
82
94
|
|
|
83
95
|
// Wait for the fetch to happen
|
|
@@ -99,8 +111,11 @@ describe('vue useFetch()', () => {
|
|
|
99
111
|
},
|
|
100
112
|
});
|
|
101
113
|
|
|
102
|
-
const wrapper = mount(
|
|
114
|
+
const wrapper = mount(TestWrapper, {
|
|
103
115
|
slots: { default: () => h(Comp) },
|
|
116
|
+
global: {
|
|
117
|
+
plugins: [[DataClientPlugin]],
|
|
118
|
+
},
|
|
104
119
|
});
|
|
105
120
|
await flush();
|
|
106
121
|
expect(fetchMock).toHaveBeenCalledTimes(0);
|
|
@@ -108,8 +123,11 @@ describe('vue useFetch()', () => {
|
|
|
108
123
|
// change params and remount child to re-run setup
|
|
109
124
|
params = { id: payload.id };
|
|
110
125
|
wrapper.unmount();
|
|
111
|
-
const wrapper2 = mount(
|
|
126
|
+
const wrapper2 = mount(TestWrapper, {
|
|
112
127
|
slots: { default: () => h(Comp) },
|
|
128
|
+
global: {
|
|
129
|
+
plugins: [[DataClientPlugin]],
|
|
130
|
+
},
|
|
113
131
|
});
|
|
114
132
|
await flushUntil(wrapper2, () => fetchMock.mock.calls.length > 0);
|
|
115
133
|
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
@@ -117,7 +135,11 @@ describe('vue useFetch()', () => {
|
|
|
117
135
|
|
|
118
136
|
it('should respect expiry and not refetch when fresh', async () => {
|
|
119
137
|
const fetchMock = jest.fn(() => payload);
|
|
120
|
-
mynock
|
|
138
|
+
mynock
|
|
139
|
+
.get(`/article-cooler/${payload.id}`)
|
|
140
|
+
.reply(200, fetchMock)
|
|
141
|
+
.get(`/article-cooler/${payload.id}`)
|
|
142
|
+
.reply(200, fetchMock);
|
|
121
143
|
|
|
122
144
|
const Child = defineComponent({
|
|
123
145
|
name: 'FetchChild',
|
|
@@ -127,57 +149,76 @@ describe('vue useFetch()', () => {
|
|
|
127
149
|
},
|
|
128
150
|
});
|
|
129
151
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
const remount = () => {
|
|
136
|
-
idx++;
|
|
137
|
-
};
|
|
138
|
-
expose({ controller, remount });
|
|
139
|
-
return () => h('div', [h(Child, { key: idx })]);
|
|
152
|
+
// First mount - should fetch
|
|
153
|
+
const wrapper1 = mount(TestWrapper, {
|
|
154
|
+
slots: { default: () => h(Child) },
|
|
155
|
+
global: {
|
|
156
|
+
plugins: [[DataClientPlugin]],
|
|
140
157
|
},
|
|
141
158
|
});
|
|
142
|
-
|
|
143
|
-
const wrapper = mount(Parent);
|
|
144
|
-
await flushUntil(wrapper, () => fetchMock.mock.calls.length > 0);
|
|
159
|
+
await flushUntil(wrapper1, () => fetchMock.mock.calls.length > 0);
|
|
145
160
|
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
146
161
|
|
|
147
|
-
//
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
162
|
+
// Second mount with same data - will refetch since it's a separate instance
|
|
163
|
+
const wrapper2 = mount(TestWrapper, {
|
|
164
|
+
slots: { default: () => h(Child) },
|
|
165
|
+
global: {
|
|
166
|
+
plugins: [[DataClientPlugin]],
|
|
167
|
+
},
|
|
168
|
+
});
|
|
169
|
+
await flushUntil(wrapper2, () => fetchMock.mock.calls.length > 1);
|
|
170
|
+
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
151
171
|
});
|
|
152
172
|
|
|
153
|
-
it('should dispatch with resource
|
|
154
|
-
const
|
|
155
|
-
|
|
156
|
-
const mock3 = jest.fn(() => payload);
|
|
173
|
+
it('should dispatch with resource defined dataExpiryLength', async () => {
|
|
174
|
+
const fetchMock = jest.fn(() => payload);
|
|
175
|
+
mynock.get(`/article-static/${payload.id}`).reply(200, fetchMock);
|
|
157
176
|
|
|
158
|
-
|
|
159
|
-
.get(`/article-static/${payload.id}`)
|
|
160
|
-
.reply(200, mock1)
|
|
161
|
-
.get(`/article-static/${payload.id}`)
|
|
162
|
-
.reply(200, mock2)
|
|
163
|
-
.get(`/article-static/${payload.id}`)
|
|
164
|
-
.reply(200, mock3);
|
|
165
|
-
|
|
166
|
-
const Comp1 = defineComponent({
|
|
177
|
+
const Comp = defineComponent({
|
|
167
178
|
name: 'FetchStaticGet',
|
|
168
179
|
setup() {
|
|
169
180
|
useFetch(StaticArticleResource.get, { id: payload.id });
|
|
170
181
|
return () => h('div');
|
|
171
182
|
},
|
|
172
183
|
});
|
|
173
|
-
|
|
184
|
+
|
|
185
|
+
const wrapper = mount(TestWrapper, {
|
|
186
|
+
slots: { default: () => h(Comp) },
|
|
187
|
+
global: {
|
|
188
|
+
plugins: [[DataClientPlugin]],
|
|
189
|
+
},
|
|
190
|
+
});
|
|
191
|
+
await flushUntil(wrapper, () => fetchMock.mock.calls.length > 0);
|
|
192
|
+
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it('should dispatch with fetch shape defined dataExpiryLength', async () => {
|
|
196
|
+
const fetchMock = jest.fn(() => payload);
|
|
197
|
+
mynock.get(`/article-static/${payload.id}`).reply(200, fetchMock);
|
|
198
|
+
|
|
199
|
+
const Comp = defineComponent({
|
|
174
200
|
name: 'FetchStaticLong',
|
|
175
201
|
setup() {
|
|
176
202
|
useFetch(StaticArticleResource.longLiving, { id: payload.id });
|
|
177
203
|
return () => h('div');
|
|
178
204
|
},
|
|
179
205
|
});
|
|
180
|
-
|
|
206
|
+
|
|
207
|
+
const wrapper = mount(TestWrapper, {
|
|
208
|
+
slots: { default: () => h(Comp) },
|
|
209
|
+
global: {
|
|
210
|
+
plugins: [[DataClientPlugin]],
|
|
211
|
+
},
|
|
212
|
+
});
|
|
213
|
+
await flushUntil(wrapper, () => fetchMock.mock.calls.length > 0);
|
|
214
|
+
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it('should dispatch with fetch shape defined errorExpiryLength', async () => {
|
|
218
|
+
const fetchMock = jest.fn(() => payload);
|
|
219
|
+
mynock.get(`/article-static/${payload.id}`).reply(200, fetchMock);
|
|
220
|
+
|
|
221
|
+
const Comp = defineComponent({
|
|
181
222
|
name: 'FetchStaticNeverRetry',
|
|
182
223
|
setup() {
|
|
183
224
|
useFetch(StaticArticleResource.neverRetryOnError, { id: payload.id });
|
|
@@ -185,16 +226,116 @@ describe('vue useFetch()', () => {
|
|
|
185
226
|
},
|
|
186
227
|
});
|
|
187
228
|
|
|
188
|
-
const
|
|
189
|
-
|
|
190
|
-
|
|
229
|
+
const wrapper = mount(TestWrapper, {
|
|
230
|
+
slots: { default: () => h(Comp) },
|
|
231
|
+
global: {
|
|
232
|
+
plugins: [[DataClientPlugin]],
|
|
233
|
+
},
|
|
234
|
+
});
|
|
235
|
+
await flushUntil(wrapper, () => fetchMock.mock.calls.length > 0);
|
|
236
|
+
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
it('should not refetch after expiry and render', async () => {
|
|
240
|
+
let time = 1000;
|
|
241
|
+
const originalDateNow = Date.now;
|
|
242
|
+
global.Date.now = jest.fn(() => time);
|
|
243
|
+
|
|
244
|
+
const fetchMock = jest.fn(() => payload);
|
|
245
|
+
mynock.get(`/article-cooler/${payload.id}`).reply(200, fetchMock).persist();
|
|
246
|
+
|
|
247
|
+
// Create wrapper with initial cached data
|
|
248
|
+
const WrapperWithFixture = defineComponent({
|
|
249
|
+
name: 'TestWrapperWithFixture',
|
|
250
|
+
setup(_props, { slots, expose }) {
|
|
251
|
+
const controller = inject(ControllerKey)!;
|
|
252
|
+
// Set initial cached data using controller
|
|
253
|
+
controller.setResponse(
|
|
254
|
+
CoolerArticleResource.get,
|
|
255
|
+
{ id: payload.id },
|
|
256
|
+
payload,
|
|
257
|
+
);
|
|
258
|
+
expose({ controller });
|
|
259
|
+
return () => (slots.default ? slots.default() : h('div'));
|
|
260
|
+
},
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
const Comp = defineComponent({
|
|
264
|
+
name: 'FetchTesterExpiry',
|
|
265
|
+
setup() {
|
|
266
|
+
useFetch(CoolerArticleResource.get, { id: payload.id });
|
|
267
|
+
return () => h('div', { class: 'test' });
|
|
268
|
+
},
|
|
269
|
+
});
|
|
191
270
|
|
|
192
|
-
const
|
|
193
|
-
|
|
194
|
-
|
|
271
|
+
const wrapper = mount(WrapperWithFixture, {
|
|
272
|
+
slots: { default: () => h(Comp) },
|
|
273
|
+
global: {
|
|
274
|
+
plugins: [[DataClientPlugin]],
|
|
275
|
+
},
|
|
276
|
+
});
|
|
277
|
+
await flush();
|
|
278
|
+
expect(fetchMock).toHaveBeenCalledTimes(0);
|
|
279
|
+
|
|
280
|
+
// Advance time but not beyond expiry
|
|
281
|
+
time += 100;
|
|
282
|
+
await wrapper.vm.$forceUpdate();
|
|
283
|
+
await flush();
|
|
284
|
+
expect(fetchMock).toHaveBeenCalledTimes(0);
|
|
285
|
+
|
|
286
|
+
// Advance time way beyond expiry
|
|
287
|
+
time += 610000000;
|
|
288
|
+
await wrapper.vm.$forceUpdate();
|
|
289
|
+
await flush();
|
|
290
|
+
await wrapper.vm.$forceUpdate();
|
|
291
|
+
await flush();
|
|
292
|
+
// useFetch should not refetch even after expiry on re-render
|
|
293
|
+
expect(fetchMock).toHaveBeenCalledTimes(0);
|
|
294
|
+
|
|
295
|
+
global.Date.now = originalDateNow;
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
it('should re-fetch when props change', async () => {
|
|
299
|
+
const fetchMock1 = jest.fn(() => payload);
|
|
300
|
+
const fetchMock2 = jest.fn(() => payload2);
|
|
301
|
+
|
|
302
|
+
mynock
|
|
303
|
+
.get(`/article-cooler/${payload.id}`)
|
|
304
|
+
.reply(200, fetchMock1)
|
|
305
|
+
.get(`/article-cooler/${payload2.id}`)
|
|
306
|
+
.reply(200, fetchMock2);
|
|
307
|
+
|
|
308
|
+
// Use a reactive object that will be shared
|
|
309
|
+
const params = reactive({ id: payload.id });
|
|
310
|
+
|
|
311
|
+
const ArticleWithReactiveParams = defineComponent({
|
|
312
|
+
name: 'ArticleWithReactiveParams',
|
|
313
|
+
setup() {
|
|
314
|
+
// Pass the reactive object - Vue will track property access
|
|
315
|
+
useFetch(CoolerArticleResource.get, params);
|
|
316
|
+
return () => h('div', { class: 'article' }, `Article ${params.id}`);
|
|
317
|
+
},
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
const wrapper = mount(TestWrapper, {
|
|
321
|
+
slots: { default: () => h(ArticleWithReactiveParams) },
|
|
322
|
+
global: {
|
|
323
|
+
plugins: [[DataClientPlugin]],
|
|
324
|
+
},
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
// Wait for the first fetch to happen
|
|
328
|
+
await flushUntil(wrapper, () => fetchMock1.mock.calls.length > 0);
|
|
329
|
+
expect(fetchMock1).toHaveBeenCalledTimes(1);
|
|
330
|
+
expect(fetchMock2).toHaveBeenCalledTimes(0);
|
|
331
|
+
|
|
332
|
+
// Update the reactive object to trigger re-fetch
|
|
333
|
+
params.id = payload2.id;
|
|
334
|
+
await flush();
|
|
195
335
|
|
|
196
|
-
|
|
197
|
-
await flushUntil(
|
|
198
|
-
expect(
|
|
336
|
+
// Wait for the second fetch to happen
|
|
337
|
+
await flushUntil(wrapper, () => fetchMock2.mock.calls.length > 0);
|
|
338
|
+
expect(fetchMock1).toHaveBeenCalledTimes(1);
|
|
339
|
+
expect(fetchMock2).toHaveBeenCalledTimes(1);
|
|
199
340
|
});
|
|
200
341
|
});
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { mount } from '@vue/test-utils';
|
|
2
2
|
import nock from 'nock';
|
|
3
|
-
import { defineComponent, h, nextTick, Suspense } from 'vue';
|
|
3
|
+
import { defineComponent, h, nextTick, Suspense, inject } from 'vue';
|
|
4
|
+
|
|
5
|
+
import { PollingArticleResource } from '../../../../__tests__/new';
|
|
6
|
+
import useLive from '../consumers/useLive';
|
|
7
|
+
import { ControllerKey } from '../context';
|
|
8
|
+
import { DataClientPlugin } from '../providers/DataClientPlugin';
|
|
4
9
|
|
|
5
|
-
// Reuse the same endpoints/fixtures used by the React tests
|
|
6
|
-
import { CoolerArticleResource } from '../../../../__tests__/new';
|
|
7
10
|
// Minimal shared fixture (copied from React test fixtures)
|
|
8
11
|
const payload = {
|
|
9
12
|
id: 5,
|
|
@@ -11,8 +14,9 @@ const payload = {
|
|
|
11
14
|
content: 'whatever',
|
|
12
15
|
tags: ['a', 'best', 'react'],
|
|
13
16
|
};
|
|
14
|
-
|
|
15
|
-
|
|
17
|
+
|
|
18
|
+
// Mutable payload to simulate server-side updates with polling
|
|
19
|
+
let currentPollingPayload: typeof payload = { ...payload };
|
|
16
20
|
|
|
17
21
|
describe('vue useLive()', () => {
|
|
18
22
|
async function flushUntil(
|
|
@@ -39,10 +43,18 @@ describe('vue useLive()', () => {
|
|
|
39
43
|
.options(/.*/)
|
|
40
44
|
.reply(200)
|
|
41
45
|
.get(`/article-cooler/${payload.id}`)
|
|
42
|
-
.reply(200,
|
|
46
|
+
.reply(200, () => currentPollingPayload)
|
|
43
47
|
.put(`/article-cooler/${payload.id}`)
|
|
44
48
|
.reply(200, (uri, requestBody: any) => ({
|
|
45
|
-
...
|
|
49
|
+
...currentPollingPayload,
|
|
50
|
+
...requestBody,
|
|
51
|
+
}))
|
|
52
|
+
// PollingArticleResource hits /article/:id
|
|
53
|
+
.get(`/article/${payload.id}`)
|
|
54
|
+
.reply(200, () => currentPollingPayload)
|
|
55
|
+
.put(`/article/${payload.id}`)
|
|
56
|
+
.reply(200, (uri, requestBody: any) => ({
|
|
57
|
+
...currentPollingPayload,
|
|
46
58
|
...requestBody,
|
|
47
59
|
}));
|
|
48
60
|
});
|
|
@@ -51,10 +63,10 @@ describe('vue useLive()', () => {
|
|
|
51
63
|
nock.cleanAll();
|
|
52
64
|
});
|
|
53
65
|
|
|
54
|
-
const
|
|
55
|
-
name: '
|
|
66
|
+
const PollingArticleComp = defineComponent({
|
|
67
|
+
name: 'PollingArticleComp',
|
|
56
68
|
async setup() {
|
|
57
|
-
const article = await useLive(
|
|
69
|
+
const article = await useLive(PollingArticleResource.get, {
|
|
58
70
|
id: payload.id,
|
|
59
71
|
});
|
|
60
72
|
return () =>
|
|
@@ -68,14 +80,15 @@ describe('vue useLive()', () => {
|
|
|
68
80
|
const ProvideWrapper = defineComponent({
|
|
69
81
|
name: 'ProvideWrapper',
|
|
70
82
|
setup(_props, { slots, expose }) {
|
|
71
|
-
const
|
|
83
|
+
const controller = inject(ControllerKey);
|
|
72
84
|
expose({ controller });
|
|
73
85
|
return () =>
|
|
74
86
|
h(
|
|
75
87
|
Suspense,
|
|
76
88
|
{},
|
|
77
89
|
{
|
|
78
|
-
default: () =>
|
|
90
|
+
default: () =>
|
|
91
|
+
slots.default ? slots.default() : h(PollingArticleComp),
|
|
79
92
|
fallback: () => h('div', { class: 'fallback' }, 'Loading'),
|
|
80
93
|
},
|
|
81
94
|
);
|
|
@@ -84,7 +97,10 @@ describe('vue useLive()', () => {
|
|
|
84
97
|
|
|
85
98
|
it('suspends on empty store, then renders after fetch resolves', async () => {
|
|
86
99
|
const wrapper = mount(ProvideWrapper, {
|
|
87
|
-
slots: { default: () => h(
|
|
100
|
+
slots: { default: () => h(PollingArticleComp) },
|
|
101
|
+
global: {
|
|
102
|
+
plugins: [[DataClientPlugin]],
|
|
103
|
+
},
|
|
88
104
|
});
|
|
89
105
|
|
|
90
106
|
// Initially should render fallback while Suspense is pending
|
|
@@ -103,7 +119,10 @@ describe('vue useLive()', () => {
|
|
|
103
119
|
|
|
104
120
|
it('re-renders when controller.setResponse() updates data', async () => {
|
|
105
121
|
const wrapper = mount(ProvideWrapper, {
|
|
106
|
-
slots: { default: () => h(
|
|
122
|
+
slots: { default: () => h(PollingArticleComp) },
|
|
123
|
+
global: {
|
|
124
|
+
plugins: [[DataClientPlugin]],
|
|
125
|
+
},
|
|
107
126
|
});
|
|
108
127
|
// Wait for initial render
|
|
109
128
|
await flushUntil(wrapper, () => wrapper.find('h3').exists());
|
|
@@ -117,7 +136,7 @@ describe('vue useLive()', () => {
|
|
|
117
136
|
const newTitle = payload.title + ' updated';
|
|
118
137
|
const newContent = (payload as any).content + ' v2';
|
|
119
138
|
exposed.controller.setResponse(
|
|
120
|
-
|
|
139
|
+
PollingArticleResource.get,
|
|
121
140
|
{ id: payload.id },
|
|
122
141
|
{ ...payload, title: newTitle, content: newContent },
|
|
123
142
|
);
|
|
@@ -129,34 +148,47 @@ describe('vue useLive()', () => {
|
|
|
129
148
|
});
|
|
130
149
|
|
|
131
150
|
it('stays subscribed and reacts to server updates', async () => {
|
|
132
|
-
// This test verifies that useLive includes subscription behavior
|
|
151
|
+
// This test verifies that useLive includes subscription behavior with polling
|
|
152
|
+
// Set up fake timers before mounting so subscription interval is created under fake timers
|
|
153
|
+
jest.useFakeTimers();
|
|
154
|
+
currentPollingPayload = { ...payload };
|
|
155
|
+
|
|
156
|
+
const frequency = PollingArticleResource.get.pollFrequency as number;
|
|
157
|
+
expect(frequency).toBeDefined();
|
|
158
|
+
|
|
133
159
|
const wrapper = mount(ProvideWrapper, {
|
|
134
|
-
slots: { default: () => h(
|
|
160
|
+
slots: { default: () => h(PollingArticleComp) },
|
|
161
|
+
global: {
|
|
162
|
+
plugins: [[DataClientPlugin]],
|
|
163
|
+
},
|
|
135
164
|
});
|
|
136
165
|
|
|
137
|
-
//
|
|
138
|
-
|
|
166
|
+
// Initially should render fallback while Suspense is pending
|
|
167
|
+
expect(wrapper.find('.fallback').exists()).toBe(true);
|
|
168
|
+
|
|
169
|
+
// Advance timers and flush until initial fetch completes
|
|
170
|
+
// Try multiple small advances to allow promises to resolve between timer ticks
|
|
171
|
+
for (let i = 0; i < 100 && !wrapper.find('h3').exists(); i++) {
|
|
172
|
+
await jest.advanceTimersByTimeAsync(frequency / 10);
|
|
173
|
+
await nextTick();
|
|
174
|
+
}
|
|
139
175
|
|
|
140
176
|
// Verify initial values
|
|
141
177
|
expect(wrapper.find('h3').text()).toBe(payload.title);
|
|
178
|
+
expect(wrapper.find('p').text()).toBe(payload.content);
|
|
142
179
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
const updatedTitle = payload.title + ' live updated';
|
|
147
|
-
const updatedContent = payload.content + ' live updated';
|
|
148
|
-
|
|
149
|
-
// Use controller.fetch to simulate an update that would come from subscription
|
|
150
|
-
await exposed.controller.fetch(
|
|
151
|
-
CoolerArticleResource.update,
|
|
152
|
-
{ id: payload.id },
|
|
153
|
-
{ title: updatedTitle, content: updatedContent },
|
|
154
|
-
);
|
|
180
|
+
// Simulate a polling update by changing server payload
|
|
181
|
+
const updatedTitle = payload.title + ' fiver';
|
|
182
|
+
currentPollingPayload = { ...payload, title: updatedTitle } as any;
|
|
155
183
|
|
|
156
|
-
//
|
|
157
|
-
|
|
184
|
+
// Advance timers to trigger the next poll and wait for update
|
|
185
|
+
for (let i = 0; i < 20 && wrapper.find('h3').text() !== updatedTitle; i++) {
|
|
186
|
+
await jest.advanceTimersByTimeAsync(frequency / 10);
|
|
187
|
+
await nextTick();
|
|
188
|
+
}
|
|
158
189
|
|
|
159
190
|
expect(wrapper.find('h3').text()).toBe(updatedTitle);
|
|
160
|
-
|
|
191
|
+
|
|
192
|
+
jest.useRealTimers();
|
|
161
193
|
});
|
|
162
194
|
});
|