@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,195 @@
|
|
|
1
|
+
import { Controller, initialState } from '@data-client/core';
|
|
2
|
+
import type { State } from '@data-client/core';
|
|
3
|
+
import {
|
|
4
|
+
describe,
|
|
5
|
+
it,
|
|
6
|
+
expect,
|
|
7
|
+
beforeEach,
|
|
8
|
+
afterEach,
|
|
9
|
+
jest,
|
|
10
|
+
} from '@jest/globals';
|
|
11
|
+
import { shallowRef } from 'vue';
|
|
12
|
+
|
|
13
|
+
import {
|
|
14
|
+
StateKey,
|
|
15
|
+
ControllerKey,
|
|
16
|
+
FallbackStateRef,
|
|
17
|
+
useController,
|
|
18
|
+
injectState,
|
|
19
|
+
} from '../context';
|
|
20
|
+
|
|
21
|
+
// Mock Vue's inject function
|
|
22
|
+
const mockInjectMap: Map<symbol, any> = new Map();
|
|
23
|
+
|
|
24
|
+
jest.mock('vue', () => {
|
|
25
|
+
const actual = jest.requireActual('vue') as any;
|
|
26
|
+
return {
|
|
27
|
+
...(actual || {}),
|
|
28
|
+
inject: jest.fn((key: symbol, defaultValue: any) => {
|
|
29
|
+
return mockInjectMap.get(key) ?? defaultValue;
|
|
30
|
+
}),
|
|
31
|
+
};
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
describe('context', () => {
|
|
35
|
+
let consoleErrorSpy: jest.SpiedFunction<typeof console.error>;
|
|
36
|
+
const originalNodeEnv = process.env.NODE_ENV;
|
|
37
|
+
|
|
38
|
+
beforeEach(() => {
|
|
39
|
+
mockInjectMap.clear();
|
|
40
|
+
consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
afterEach(() => {
|
|
44
|
+
consoleErrorSpy.mockRestore();
|
|
45
|
+
process.env.NODE_ENV = originalNodeEnv;
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe('FallbackStateRef', () => {
|
|
49
|
+
it('should be a shallow ref with initial state', () => {
|
|
50
|
+
expect(FallbackStateRef.value).toEqual(initialState);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('should be reactive', () => {
|
|
54
|
+
const initialValue = FallbackStateRef.value;
|
|
55
|
+
const newState: State<unknown> = {
|
|
56
|
+
...initialState,
|
|
57
|
+
endpoints: { test: { data: 'value' } } as any,
|
|
58
|
+
};
|
|
59
|
+
FallbackStateRef.value = newState;
|
|
60
|
+
expect(FallbackStateRef.value).toEqual(newState);
|
|
61
|
+
// Restore original state
|
|
62
|
+
FallbackStateRef.value = initialValue;
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
describe('useController', () => {
|
|
67
|
+
it('should return the injected controller when provided', () => {
|
|
68
|
+
const mockController = new Controller();
|
|
69
|
+
mockInjectMap.set(ControllerKey, mockController);
|
|
70
|
+
|
|
71
|
+
const result = useController();
|
|
72
|
+
|
|
73
|
+
expect(result).toBe(mockController);
|
|
74
|
+
expect(consoleErrorSpy).not.toHaveBeenCalled();
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('should return a new Controller when not provided', () => {
|
|
78
|
+
mockInjectMap.set(ControllerKey, null);
|
|
79
|
+
|
|
80
|
+
const result = useController();
|
|
81
|
+
|
|
82
|
+
expect(result).toBeInstanceOf(Controller);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('should log error in development when controller is not provided', () => {
|
|
86
|
+
process.env.NODE_ENV = 'development';
|
|
87
|
+
mockInjectMap.set(ControllerKey, null);
|
|
88
|
+
|
|
89
|
+
useController();
|
|
90
|
+
|
|
91
|
+
expect(consoleErrorSpy).toHaveBeenCalledWith(
|
|
92
|
+
expect.stringContaining(
|
|
93
|
+
'It appears you are trying to use Reactive Data Client (Vue) without a provider.',
|
|
94
|
+
),
|
|
95
|
+
);
|
|
96
|
+
expect(consoleErrorSpy).toHaveBeenCalledWith(
|
|
97
|
+
expect.stringContaining(
|
|
98
|
+
'https://dataclient.io/docs/getting-started/installation',
|
|
99
|
+
),
|
|
100
|
+
);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('should not log error in production when controller is not provided', () => {
|
|
104
|
+
process.env.NODE_ENV = 'production';
|
|
105
|
+
mockInjectMap.set(ControllerKey, null);
|
|
106
|
+
|
|
107
|
+
useController();
|
|
108
|
+
|
|
109
|
+
expect(consoleErrorSpy).not.toHaveBeenCalled();
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('should handle undefined controller', () => {
|
|
113
|
+
mockInjectMap.set(ControllerKey, undefined);
|
|
114
|
+
|
|
115
|
+
const result = useController();
|
|
116
|
+
|
|
117
|
+
expect(result).toBeInstanceOf(Controller);
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
describe('injectState', () => {
|
|
122
|
+
it('should return the injected state when provided', () => {
|
|
123
|
+
const mockState = shallowRef<State<unknown>>({
|
|
124
|
+
...initialState,
|
|
125
|
+
endpoints: { test: { data: 'value' } } as any,
|
|
126
|
+
});
|
|
127
|
+
mockInjectMap.set(StateKey, mockState);
|
|
128
|
+
|
|
129
|
+
const result = injectState();
|
|
130
|
+
|
|
131
|
+
expect(result).toBe(mockState);
|
|
132
|
+
expect(consoleErrorSpy).not.toHaveBeenCalled();
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('should return FallbackStateRef when not provided', () => {
|
|
136
|
+
mockInjectMap.set(StateKey, null);
|
|
137
|
+
|
|
138
|
+
const result = injectState();
|
|
139
|
+
|
|
140
|
+
expect(result).toBe(FallbackStateRef);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it('should log error in development when state is not provided', () => {
|
|
144
|
+
process.env.NODE_ENV = 'development';
|
|
145
|
+
mockInjectMap.set(StateKey, null);
|
|
146
|
+
|
|
147
|
+
injectState();
|
|
148
|
+
|
|
149
|
+
expect(consoleErrorSpy).toHaveBeenCalledWith(
|
|
150
|
+
expect.stringContaining(
|
|
151
|
+
'It appears you are trying to use Reactive Data Client (Vue) without a provider.',
|
|
152
|
+
),
|
|
153
|
+
);
|
|
154
|
+
expect(consoleErrorSpy).toHaveBeenCalledWith(
|
|
155
|
+
expect.stringContaining(
|
|
156
|
+
'https://dataclient.io/docs/getting-started/installation',
|
|
157
|
+
),
|
|
158
|
+
);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it('should not log error in production when state is not provided', () => {
|
|
162
|
+
process.env.NODE_ENV = 'production';
|
|
163
|
+
mockInjectMap.set(StateKey, null);
|
|
164
|
+
|
|
165
|
+
injectState();
|
|
166
|
+
|
|
167
|
+
expect(consoleErrorSpy).not.toHaveBeenCalled();
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('should handle undefined state', () => {
|
|
171
|
+
mockInjectMap.set(StateKey, undefined);
|
|
172
|
+
|
|
173
|
+
const result = injectState();
|
|
174
|
+
|
|
175
|
+
expect(result).toBe(FallbackStateRef);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it('should return state ref that is reactive', () => {
|
|
179
|
+
const mockState = shallowRef<State<unknown>>(initialState);
|
|
180
|
+
mockInjectMap.set(StateKey, mockState);
|
|
181
|
+
|
|
182
|
+
const result = injectState();
|
|
183
|
+
|
|
184
|
+
expect(result.value).toEqual(initialState);
|
|
185
|
+
|
|
186
|
+
const newState: State<unknown> = {
|
|
187
|
+
...initialState,
|
|
188
|
+
endpoints: { test: { data: 'updated' } } as any,
|
|
189
|
+
};
|
|
190
|
+
mockState.value = newState;
|
|
191
|
+
|
|
192
|
+
expect(result.value).toEqual(newState);
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
});
|
|
@@ -4,10 +4,7 @@ import { defineComponent, h } from 'vue';
|
|
|
4
4
|
import { Article, ArticleResource } from '../../../../__tests__/new';
|
|
5
5
|
import useQuery from '../consumers/useQuery';
|
|
6
6
|
import useSuspense from '../consumers/useSuspense';
|
|
7
|
-
import {
|
|
8
|
-
renderDataComposable,
|
|
9
|
-
renderDataClient,
|
|
10
|
-
} from '../test/renderDataClient';
|
|
7
|
+
import { renderDataCompose, mountDataClient } from '../test';
|
|
11
8
|
|
|
12
9
|
const GC_INTERVAL = 100; // Use short interval for faster tests
|
|
13
10
|
|
|
@@ -20,7 +17,7 @@ describe('Integration Garbage Collection Web (Vue)', () => {
|
|
|
20
17
|
expect(gcPolicy).toBeDefined();
|
|
21
18
|
});
|
|
22
19
|
|
|
23
|
-
it('should accept gcPolicy option in
|
|
20
|
+
it('should accept gcPolicy option in mountDataClient', () => {
|
|
24
21
|
const TestComp = defineComponent({
|
|
25
22
|
name: 'TestComp',
|
|
26
23
|
setup() {
|
|
@@ -33,7 +30,7 @@ describe('Integration Garbage Collection Web (Vue)', () => {
|
|
|
33
30
|
expiryMultiplier: 2,
|
|
34
31
|
});
|
|
35
32
|
|
|
36
|
-
const { wrapper, cleanup } =
|
|
33
|
+
const { wrapper, cleanup } = mountDataClient(TestComp, {
|
|
37
34
|
gcPolicy,
|
|
38
35
|
});
|
|
39
36
|
|
|
@@ -48,7 +45,7 @@ describe('Integration Garbage Collection Web (Vue)', () => {
|
|
|
48
45
|
content: 'Test Content',
|
|
49
46
|
};
|
|
50
47
|
|
|
51
|
-
const { result, cleanup, waitForNextUpdate } =
|
|
48
|
+
const { result, cleanup, waitForNextUpdate } = await renderDataCompose(
|
|
52
49
|
() => useSuspense(ArticleResource.get, { id: 1 }),
|
|
53
50
|
{
|
|
54
51
|
initialFixtures: [
|
|
@@ -67,20 +64,20 @@ describe('Integration Garbage Collection Web (Vue)', () => {
|
|
|
67
64
|
|
|
68
65
|
await waitForNextUpdate();
|
|
69
66
|
|
|
70
|
-
const articleRef = await result
|
|
67
|
+
const articleRef = await result;
|
|
71
68
|
expect(articleRef?.value.title).toBe(articleData.title);
|
|
72
69
|
expect(articleRef?.value.content).toBe(articleData.content);
|
|
73
70
|
|
|
74
71
|
cleanup();
|
|
75
72
|
});
|
|
76
73
|
|
|
77
|
-
it('should work with useQuery and GCPolicy', () => {
|
|
74
|
+
it('should work with useQuery and GCPolicy', async () => {
|
|
78
75
|
const articleListData = [
|
|
79
76
|
{ id: 1, title: 'Article 1', content: 'Content 1' },
|
|
80
77
|
{ id: 2, title: 'Article 2', content: 'Content 2' },
|
|
81
78
|
];
|
|
82
79
|
|
|
83
|
-
const { result, cleanup } =
|
|
80
|
+
const { result, cleanup } = await renderDataCompose(
|
|
84
81
|
() => useQuery(ArticleResource.getList.schema),
|
|
85
82
|
{
|
|
86
83
|
initialFixtures: [
|
|
@@ -97,9 +94,9 @@ describe('Integration Garbage Collection Web (Vue)', () => {
|
|
|
97
94
|
},
|
|
98
95
|
);
|
|
99
96
|
|
|
100
|
-
expect(result.
|
|
101
|
-
expect(result.
|
|
102
|
-
expect(result.
|
|
97
|
+
expect(result.value).toBeDefined();
|
|
98
|
+
expect(result.value?.length).toBe(2);
|
|
99
|
+
expect(result.value?.[0]).toBeInstanceOf(Article);
|
|
103
100
|
|
|
104
101
|
cleanup();
|
|
105
102
|
});
|
|
@@ -119,7 +116,7 @@ describe('Integration Garbage Collection Web (Vue)', () => {
|
|
|
119
116
|
});
|
|
120
117
|
|
|
121
118
|
// Test with expiryMultiplier of 4
|
|
122
|
-
const { cleanup: cleanup1 } =
|
|
119
|
+
const { cleanup: cleanup1 } = mountDataClient(TestComp, {
|
|
123
120
|
initialFixtures: [
|
|
124
121
|
{
|
|
125
122
|
endpoint: ArticleResource.get,
|
|
@@ -136,7 +133,7 @@ describe('Integration Garbage Collection Web (Vue)', () => {
|
|
|
136
133
|
cleanup1();
|
|
137
134
|
|
|
138
135
|
// Test with expiryMultiplier of 2 (default in tests)
|
|
139
|
-
const { cleanup: cleanup2 } =
|
|
136
|
+
const { cleanup: cleanup2 } = mountDataClient(TestComp, {
|
|
140
137
|
initialFixtures: [
|
|
141
138
|
{
|
|
142
139
|
endpoint: ArticleResource.get,
|
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
import nock from 'nock';
|
|
2
|
+
import { computed, defineComponent, h, nextTick, reactive } from 'vue';
|
|
3
|
+
|
|
4
|
+
import { CoolerArticleResource } from '../../../../__tests__/new';
|
|
5
|
+
import useCache from '../consumers/useCache';
|
|
6
|
+
import { renderDataCompose, mountDataClient } from '../test';
|
|
7
|
+
|
|
8
|
+
// Minimal shared fixture (copied from React test fixtures)
|
|
9
|
+
const payload = {
|
|
10
|
+
id: 5,
|
|
11
|
+
title: 'hi ho',
|
|
12
|
+
content: 'whatever',
|
|
13
|
+
tags: ['a', 'best', 'react'],
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const payload2 = {
|
|
17
|
+
id: 6,
|
|
18
|
+
title: 'next',
|
|
19
|
+
content: 'my best content yet',
|
|
20
|
+
tags: ['b'],
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
describe('vue useCache()', () => {
|
|
24
|
+
async function flushUntil(
|
|
25
|
+
wrapper: any,
|
|
26
|
+
predicate: () => boolean,
|
|
27
|
+
tries = 100,
|
|
28
|
+
) {
|
|
29
|
+
for (let i = 0; i < tries; i++) {
|
|
30
|
+
if (predicate()) return;
|
|
31
|
+
await Promise.resolve();
|
|
32
|
+
await nextTick();
|
|
33
|
+
await new Promise(resolve => setTimeout(resolve, 0));
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
beforeAll(() => {
|
|
38
|
+
nock(/.*/)
|
|
39
|
+
.persist()
|
|
40
|
+
.defaultReplyHeaders({
|
|
41
|
+
'Access-Control-Allow-Origin': '*',
|
|
42
|
+
'Access-Control-Allow-Headers': 'Access-Token',
|
|
43
|
+
'Content-Type': 'application/json',
|
|
44
|
+
})
|
|
45
|
+
.options(/.*/)
|
|
46
|
+
.reply(200)
|
|
47
|
+
.get(`/article-cooler/${payload.id}`)
|
|
48
|
+
.reply(200, payload)
|
|
49
|
+
.get(`/article-cooler/${payload2.id}`)
|
|
50
|
+
.reply(200, payload2)
|
|
51
|
+
.put(`/article-cooler/${payload.id}`)
|
|
52
|
+
.reply(200, (uri, requestBody: any) => ({
|
|
53
|
+
...payload,
|
|
54
|
+
...requestBody,
|
|
55
|
+
}));
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
afterAll(() => {
|
|
59
|
+
nock.cleanAll();
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('returns undefined on empty store (no fetch)', async () => {
|
|
63
|
+
const { result, cleanup } = await renderDataCompose(() =>
|
|
64
|
+
useCache(CoolerArticleResource.get, { id: payload.id }),
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
// The value should be undefined since we haven't fetched yet
|
|
68
|
+
expect(result.value).toBeUndefined();
|
|
69
|
+
|
|
70
|
+
cleanup();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('returns data when already in cache', async () => {
|
|
74
|
+
const { result, waitForNextUpdate, cleanup } = await renderDataCompose(
|
|
75
|
+
() => useCache(CoolerArticleResource.get, { id: payload.id }),
|
|
76
|
+
{
|
|
77
|
+
initialFixtures: [
|
|
78
|
+
{
|
|
79
|
+
endpoint: CoolerArticleResource.get,
|
|
80
|
+
args: [{ id: payload.id }],
|
|
81
|
+
response: payload,
|
|
82
|
+
},
|
|
83
|
+
],
|
|
84
|
+
},
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
// Wait for initial render
|
|
88
|
+
await waitForNextUpdate();
|
|
89
|
+
|
|
90
|
+
// The value should be the cached data
|
|
91
|
+
expect(result.value).toBeDefined();
|
|
92
|
+
expect(result.value?.title).toBe(payload.title);
|
|
93
|
+
expect(result.value?.content).toBe(payload.content);
|
|
94
|
+
|
|
95
|
+
cleanup();
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('re-renders when controller.setResponse() updates data', async () => {
|
|
99
|
+
const { result, controller, waitForNextUpdate, cleanup } =
|
|
100
|
+
await renderDataCompose(
|
|
101
|
+
() => useCache(CoolerArticleResource.get, { id: payload.id }),
|
|
102
|
+
{
|
|
103
|
+
initialFixtures: [
|
|
104
|
+
{
|
|
105
|
+
endpoint: CoolerArticleResource.get,
|
|
106
|
+
args: [{ id: payload.id }],
|
|
107
|
+
response: payload,
|
|
108
|
+
},
|
|
109
|
+
],
|
|
110
|
+
},
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
// Wait for initial render
|
|
114
|
+
await waitForNextUpdate();
|
|
115
|
+
|
|
116
|
+
// Verify initial values
|
|
117
|
+
expect(result.value?.title).toBe(payload.title);
|
|
118
|
+
expect(result.value?.content).toBe(payload.content);
|
|
119
|
+
|
|
120
|
+
// Update the store using controller.setResponse
|
|
121
|
+
const newTitle = payload.title + ' updated';
|
|
122
|
+
const newContent = payload.content + ' v2';
|
|
123
|
+
controller.setResponse(
|
|
124
|
+
CoolerArticleResource.get,
|
|
125
|
+
{ id: payload.id },
|
|
126
|
+
{ ...payload, title: newTitle, content: newContent },
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
// Wait a tick for Vue reactivity to propagate
|
|
130
|
+
await nextTick();
|
|
131
|
+
|
|
132
|
+
// The ComputedRef should now have updated values (it's reactive!)
|
|
133
|
+
expect(result.value?.title).toBe(newTitle);
|
|
134
|
+
expect(result.value?.content).toBe(newContent);
|
|
135
|
+
|
|
136
|
+
cleanup();
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('re-renders when controller.fetch() mutates data', async () => {
|
|
140
|
+
const { result, controller, waitForNextUpdate, cleanup } =
|
|
141
|
+
await renderDataCompose(
|
|
142
|
+
() => useCache(CoolerArticleResource.get, { id: payload.id }),
|
|
143
|
+
{
|
|
144
|
+
initialFixtures: [
|
|
145
|
+
{
|
|
146
|
+
endpoint: CoolerArticleResource.get,
|
|
147
|
+
args: [{ id: payload.id }],
|
|
148
|
+
response: payload,
|
|
149
|
+
},
|
|
150
|
+
],
|
|
151
|
+
},
|
|
152
|
+
);
|
|
153
|
+
|
|
154
|
+
// Wait for initial render
|
|
155
|
+
await waitForNextUpdate();
|
|
156
|
+
|
|
157
|
+
// Verify initial values
|
|
158
|
+
expect(result.value?.title).toBe(payload.title);
|
|
159
|
+
expect(result.value?.content).toBe(payload.content);
|
|
160
|
+
|
|
161
|
+
// Mutate the data using controller.fetch with update endpoint
|
|
162
|
+
const updatedTitle = payload.title + ' mutated';
|
|
163
|
+
const updatedContent = payload.content + ' mutated';
|
|
164
|
+
|
|
165
|
+
await controller.fetch(
|
|
166
|
+
CoolerArticleResource.update,
|
|
167
|
+
{ id: payload.id },
|
|
168
|
+
{ title: updatedTitle, content: updatedContent },
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
// Wait a tick for Vue reactivity to propagate
|
|
172
|
+
await nextTick();
|
|
173
|
+
|
|
174
|
+
// The ComputedRef should now have updated values (it's reactive!)
|
|
175
|
+
expect(result.value?.title).toBe(updatedTitle);
|
|
176
|
+
expect(result.value?.content).toBe(updatedContent);
|
|
177
|
+
|
|
178
|
+
cleanup();
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it('should update when props change', async () => {
|
|
182
|
+
nock.cleanAll();
|
|
183
|
+
|
|
184
|
+
const fetchMock1 = jest.fn(() => payload);
|
|
185
|
+
const fetchMock2 = jest.fn(() => payload2);
|
|
186
|
+
|
|
187
|
+
nock(/.*/)
|
|
188
|
+
.persist()
|
|
189
|
+
.defaultReplyHeaders({
|
|
190
|
+
'Access-Control-Allow-Origin': '*',
|
|
191
|
+
'Access-Control-Allow-Headers': 'Access-Token',
|
|
192
|
+
'Content-Type': 'application/json',
|
|
193
|
+
})
|
|
194
|
+
.options(/.*/)
|
|
195
|
+
.reply(200)
|
|
196
|
+
.get(`/article-cooler/${payload.id}`)
|
|
197
|
+
.reply(200, fetchMock1)
|
|
198
|
+
.get(`/article-cooler/${payload2.id}`)
|
|
199
|
+
.reply(200, fetchMock2);
|
|
200
|
+
|
|
201
|
+
const ArticleWithProps = defineComponent({
|
|
202
|
+
name: 'ArticleWithProps',
|
|
203
|
+
props: {
|
|
204
|
+
id: {
|
|
205
|
+
type: Number,
|
|
206
|
+
required: true,
|
|
207
|
+
},
|
|
208
|
+
},
|
|
209
|
+
setup(props: { id: number }) {
|
|
210
|
+
// Pass the reactive value - useCache will track changes via its internal computed
|
|
211
|
+
const article = useCache(CoolerArticleResource.get, props);
|
|
212
|
+
|
|
213
|
+
return () =>
|
|
214
|
+
h('div', [
|
|
215
|
+
h('h3', (article as any).value?.title || 'Loading...'),
|
|
216
|
+
h('p', (article as any).value?.content || ''),
|
|
217
|
+
]);
|
|
218
|
+
},
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
// Use mountDataClient to properly set up the test environment
|
|
222
|
+
const props = reactive({ id: payload.id });
|
|
223
|
+
const { wrapper, cleanup } = mountDataClient(ArticleWithProps, {
|
|
224
|
+
props,
|
|
225
|
+
initialFixtures: [
|
|
226
|
+
{
|
|
227
|
+
endpoint: CoolerArticleResource.get,
|
|
228
|
+
args: [{ id: payload.id }],
|
|
229
|
+
response: payload,
|
|
230
|
+
},
|
|
231
|
+
{
|
|
232
|
+
endpoint: CoolerArticleResource.get,
|
|
233
|
+
args: [{ id: payload2.id }],
|
|
234
|
+
response: payload2,
|
|
235
|
+
},
|
|
236
|
+
],
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
// Wait for data to render
|
|
240
|
+
await flushUntil(
|
|
241
|
+
wrapper,
|
|
242
|
+
() => wrapper.find('h3').text() === payload.title,
|
|
243
|
+
);
|
|
244
|
+
expect(wrapper.find('h3').text()).toBe(payload.title);
|
|
245
|
+
expect(wrapper.find('p').text()).toBe(payload.content);
|
|
246
|
+
|
|
247
|
+
// Change the id prop
|
|
248
|
+
props.id = payload2.id;
|
|
249
|
+
await nextTick();
|
|
250
|
+
|
|
251
|
+
// Wait for the new data to render
|
|
252
|
+
await flushUntil(
|
|
253
|
+
wrapper,
|
|
254
|
+
() =>
|
|
255
|
+
wrapper.find('h3').exists() &&
|
|
256
|
+
wrapper.find('h3').text() === payload2.title,
|
|
257
|
+
);
|
|
258
|
+
|
|
259
|
+
expect(wrapper.find('h3').text()).toBe(payload2.title);
|
|
260
|
+
expect(wrapper.find('p').text()).toBe(payload2.content);
|
|
261
|
+
|
|
262
|
+
cleanup();
|
|
263
|
+
|
|
264
|
+
// Restore the original nock interceptors from beforeAll
|
|
265
|
+
nock.cleanAll();
|
|
266
|
+
nock(/.*/)
|
|
267
|
+
.persist()
|
|
268
|
+
.defaultReplyHeaders({
|
|
269
|
+
'Access-Control-Allow-Origin': '*',
|
|
270
|
+
'Access-Control-Allow-Headers': 'Access-Token',
|
|
271
|
+
'Content-Type': 'application/json',
|
|
272
|
+
})
|
|
273
|
+
.options(/.*/)
|
|
274
|
+
.reply(200)
|
|
275
|
+
.get(`/article-cooler/${payload.id}`)
|
|
276
|
+
.reply(200, payload)
|
|
277
|
+
.get(`/article-cooler/${payload2.id}`)
|
|
278
|
+
.reply(200, payload2)
|
|
279
|
+
.put(`/article-cooler/${payload.id}`)
|
|
280
|
+
.reply(200, (uri, requestBody: any) => ({
|
|
281
|
+
...payload,
|
|
282
|
+
...requestBody,
|
|
283
|
+
}));
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
it('should handle null args by returning undefined', async () => {
|
|
287
|
+
const props = reactive({ id: payload.id as number | null });
|
|
288
|
+
const { result, waitForNextUpdate, cleanup } = await renderDataCompose(
|
|
289
|
+
(props: { id: number | null }) =>
|
|
290
|
+
useCache(
|
|
291
|
+
CoolerArticleResource.get,
|
|
292
|
+
computed(() => (props.id !== null ? { id: props.id } : null)),
|
|
293
|
+
),
|
|
294
|
+
{
|
|
295
|
+
props,
|
|
296
|
+
initialFixtures: [
|
|
297
|
+
{
|
|
298
|
+
endpoint: CoolerArticleResource.get,
|
|
299
|
+
args: [{ id: payload.id }],
|
|
300
|
+
response: payload,
|
|
301
|
+
},
|
|
302
|
+
{
|
|
303
|
+
endpoint: CoolerArticleResource.get,
|
|
304
|
+
args: [{ id: payload2.id }],
|
|
305
|
+
response: payload2,
|
|
306
|
+
},
|
|
307
|
+
],
|
|
308
|
+
},
|
|
309
|
+
);
|
|
310
|
+
|
|
311
|
+
// Wait for initial render
|
|
312
|
+
await waitForNextUpdate();
|
|
313
|
+
|
|
314
|
+
expect(result.value).toBeDefined();
|
|
315
|
+
|
|
316
|
+
// Verify initial values
|
|
317
|
+
expect(result.value?.title).toBe(payload.title);
|
|
318
|
+
expect(result.value?.content).toBe(payload.content);
|
|
319
|
+
|
|
320
|
+
// Change to null - the ComputedRef should reactively become undefined
|
|
321
|
+
props.id = null;
|
|
322
|
+
await nextTick();
|
|
323
|
+
|
|
324
|
+
// The same ComputedRef should now have undefined value
|
|
325
|
+
expect(result.value).toBeUndefined();
|
|
326
|
+
|
|
327
|
+
// Change back to valid id - should get cached data
|
|
328
|
+
props.id = payload2.id;
|
|
329
|
+
await nextTick();
|
|
330
|
+
|
|
331
|
+
// The ComputedRef should now have the new article data
|
|
332
|
+
expect(result.value).toBeDefined();
|
|
333
|
+
expect(result.value?.title).toBe(payload2.title);
|
|
334
|
+
expect(result.value?.content).toBe(payload2.content);
|
|
335
|
+
|
|
336
|
+
cleanup();
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
it('returns undefined for stale data when invalidIfStale is true', async () => {
|
|
340
|
+
const { result, controller, waitForNextUpdate, cleanup } =
|
|
341
|
+
await renderDataCompose(
|
|
342
|
+
() => useCache(CoolerArticleResource.get, { id: payload.id }),
|
|
343
|
+
{
|
|
344
|
+
initialFixtures: [
|
|
345
|
+
{
|
|
346
|
+
endpoint: CoolerArticleResource.get,
|
|
347
|
+
args: [{ id: payload.id }],
|
|
348
|
+
response: payload,
|
|
349
|
+
},
|
|
350
|
+
],
|
|
351
|
+
},
|
|
352
|
+
);
|
|
353
|
+
|
|
354
|
+
// Wait for initial render
|
|
355
|
+
await waitForNextUpdate();
|
|
356
|
+
|
|
357
|
+
// Invalidate the data
|
|
358
|
+
controller.invalidate(CoolerArticleResource.get, { id: payload.id });
|
|
359
|
+
|
|
360
|
+
await nextTick();
|
|
361
|
+
|
|
362
|
+
// The value should be undefined since the data is invalid
|
|
363
|
+
expect(result.value).toBeUndefined();
|
|
364
|
+
|
|
365
|
+
cleanup();
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
it('returns cached data even if expired when expiryStatus is Valid', async () => {
|
|
369
|
+
const { result, waitForNextUpdate, cleanup } = await renderDataCompose(
|
|
370
|
+
() => useCache(CoolerArticleResource.get, { id: payload.id }),
|
|
371
|
+
{
|
|
372
|
+
initialFixtures: [
|
|
373
|
+
{
|
|
374
|
+
endpoint: CoolerArticleResource.get,
|
|
375
|
+
args: [{ id: payload.id }],
|
|
376
|
+
response: payload,
|
|
377
|
+
},
|
|
378
|
+
],
|
|
379
|
+
},
|
|
380
|
+
);
|
|
381
|
+
|
|
382
|
+
// Wait for initial render
|
|
383
|
+
await waitForNextUpdate();
|
|
384
|
+
|
|
385
|
+
// Even though data might be expired, if it's Valid it should be returned
|
|
386
|
+
expect(result.value).toBeDefined();
|
|
387
|
+
expect(result.value?.title).toBe(payload.title);
|
|
388
|
+
|
|
389
|
+
cleanup();
|
|
390
|
+
});
|
|
391
|
+
});
|