@data-client/vue 0.2.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -25
- package/dist/index.js +200 -42
- package/dist/index.umd.min.js +1 -1
- package/lib/consumers/index.d.ts +2 -0
- package/lib/consumers/index.d.ts.map +1 -1
- package/lib/consumers/index.js +3 -1
- package/lib/consumers/useCache.d.ts +12 -0
- package/lib/consumers/useCache.d.ts.map +1 -0
- package/lib/consumers/useCache.js +63 -0
- package/lib/consumers/useDLE.d.ts +18 -0
- package/lib/consumers/useDLE.d.ts.map +1 -0
- package/lib/consumers/useDLE.js +88 -0
- package/lib/consumers/useFetch.d.ts +3 -4
- package/lib/consumers/useFetch.d.ts.map +1 -1
- package/lib/consumers/useFetch.js +10 -10
- package/lib/consumers/useLive.d.ts +3 -2
- package/lib/consumers/useLive.d.ts.map +1 -1
- package/lib/consumers/useLive.js +1 -1
- package/lib/consumers/useQuery.d.ts +5 -6
- package/lib/consumers/useQuery.d.ts.map +1 -1
- package/lib/consumers/useQuery.js +9 -7
- package/lib/consumers/useSubscription.d.ts +2 -1
- package/lib/consumers/useSubscription.d.ts.map +1 -1
- package/lib/consumers/useSubscription.js +1 -2
- package/lib/consumers/useSuspense.d.ts +11 -2
- package/lib/consumers/useSuspense.d.ts.map +1 -1
- package/lib/consumers/useSuspense.js +25 -20
- package/lib/index.d.ts +1 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -1
- package/lib/providers/DataClientPlugin.d.ts +6 -0
- package/lib/providers/DataClientPlugin.d.ts.map +1 -1
- package/lib/providers/DataClientPlugin.js +15 -11
- package/lib/providers/getDefaultManagers.d.ts +4 -1
- package/lib/providers/getDefaultManagers.d.ts.map +1 -1
- package/lib/providers/getDefaultManagers.js +5 -2
- package/lib/providers/index.d.ts +1 -1
- package/lib/providers/index.d.ts.map +1 -1
- package/lib/providers/index.js +1 -1
- package/lib/test/index.d.ts +3 -4
- package/lib/test/index.d.ts.map +1 -1
- package/lib/test/index.js +3 -5
- package/lib/test/mountDataClient.d.ts +29 -0
- package/lib/test/mountDataClient.d.ts.map +1 -0
- package/lib/test/mountDataClient.js +114 -0
- package/lib/test/renderDataCompose.d.ts +19 -0
- package/lib/test/renderDataCompose.d.ts.map +1 -0
- package/lib/test/renderDataCompose.js +135 -0
- package/lib/types.d.ts +10 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/types.js +2 -0
- package/package.json +11 -9
- package/src/__tests__/context.test.ts +195 -0
- package/src/__tests__/integration-garbage-collection.web.ts +12 -15
- package/src/__tests__/useCache.web.ts +391 -0
- package/src/__tests__/useDLE.web.ts +660 -0
- package/src/__tests__/useQuery.web.ts +332 -50
- package/src/__tests__/useSubscription.web.ts +233 -119
- package/src/__tests__/useSuspense.web.ts +176 -50
- package/src/consumers/index.ts +2 -0
- package/src/consumers/useCache.ts +122 -0
- package/src/consumers/useDLE.ts +160 -0
- package/src/consumers/useFetch.ts +24 -15
- package/src/consumers/useLive.ts +6 -2
- package/src/consumers/useQuery.ts +16 -9
- package/src/consumers/useSubscription.ts +5 -1
- package/src/consumers/useSuspense.ts +37 -17
- package/src/index.ts +4 -0
- package/src/providers/DataClientPlugin.ts +25 -10
- package/src/providers/__tests__/DataClientPlugin.test.ts +204 -4
- package/src/providers/__tests__/createDataClient.test.ts +234 -0
- package/src/providers/__tests__/getDefaultManagers.test.ts +180 -0
- package/src/providers/getDefaultManagers.ts +4 -1
- package/src/providers/index.ts +5 -1
- package/src/test/README.md +77 -16
- package/src/test/__tests__/mountDataClient.test.ts +426 -0
- package/src/test/__tests__/renderDataCompose.test.ts +586 -0
- package/src/test/index.ts +3 -9
- package/src/test/mountDataClient.ts +169 -0
- package/src/test/renderDataCompose.ts +150 -0
- package/src/types.ts +11 -0
- package/lib/test/renderDataClient.d.ts +0 -40
- package/lib/test/renderDataClient.d.ts.map +0 -1
- package/lib/test/renderDataClient.js +0 -264
- package/src/__tests__/__snapshots__/useSubscription.web.ts.snap +0 -7
- package/src/test/renderDataClient.ts +0 -316
- package/ts3.4/consumers/index.d.ts +0 -9
- package/ts3.4/consumers/useDebounce.d.ts +0 -19
- package/ts3.4/consumers/useFetch.d.ts +0 -16
- package/ts3.4/consumers/useLive.d.ts +0 -19
- package/ts3.4/consumers/useLoading.d.ts +0 -22
- package/ts3.4/consumers/useQuery.d.ts +0 -12
- package/ts3.4/consumers/useSubscription.d.ts +0 -12
- package/ts3.4/consumers/useSuspense.d.ts +0 -11
- package/ts3.4/context.d.ts +0 -10
- package/ts3.4/index.d.ts +0 -6
- package/ts3.4/managers/index.d.ts +0 -2
- package/ts3.4/providers/DataClientPlugin.d.ts +0 -22
- package/ts3.4/providers/createDataClient.d.ts +0 -25
- package/ts3.4/providers/getDefaultManagers.d.ts +0 -14
- package/ts3.4/providers/index.d.ts +0 -4
- package/ts3.4/test/MockController.d.ts +0 -8
- package/ts3.4/test/collapseFixture.d.ts +0 -6
- package/ts3.4/test/createFixtureMap.d.ts +0 -6
- package/ts3.4/test/fixtureTypes.d.ts +0 -99
- package/ts3.4/test/index.d.ts +0 -8
- package/ts3.4/test/mockState.d.ts +0 -4
- package/ts3.4/test/renderDataClient.d.ts +0 -40
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Controller,
|
|
3
|
+
NetworkManager,
|
|
4
|
+
PollingSubscription,
|
|
5
|
+
SubscriptionManager,
|
|
6
|
+
actionTypes,
|
|
7
|
+
} from '@data-client/core';
|
|
8
|
+
import type { State, Manager, Middleware } from '@data-client/core';
|
|
9
|
+
import { describe, it, expect, jest, beforeEach } from '@jest/globals';
|
|
10
|
+
|
|
11
|
+
import { ControllerKey, StateKey } from '../../context';
|
|
12
|
+
import { createDataClient } from '../createDataClient';
|
|
13
|
+
|
|
14
|
+
// Mock Vue's provide for unit tests since we're testing createDataClient in isolation
|
|
15
|
+
// The actual provide functionality is tested in tests that use mockApp
|
|
16
|
+
jest.mock('vue', () => ({
|
|
17
|
+
...(jest.requireActual('vue') as any),
|
|
18
|
+
provide: jest.fn(),
|
|
19
|
+
}));
|
|
20
|
+
|
|
21
|
+
describe('createDataClient', () => {
|
|
22
|
+
beforeEach(() => {
|
|
23
|
+
jest.clearAllMocks();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('should create data client with default options', () => {
|
|
27
|
+
const provider = createDataClient();
|
|
28
|
+
|
|
29
|
+
expect(provider.controller).toBeInstanceOf(Controller);
|
|
30
|
+
expect(provider.stateRef).toBeDefined();
|
|
31
|
+
expect(typeof provider.start).toBe('function');
|
|
32
|
+
expect(typeof provider.stop).toBe('function');
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('should create data client with custom Controller', () => {
|
|
36
|
+
const provider = createDataClient({ Controller: Controller });
|
|
37
|
+
|
|
38
|
+
expect(provider.controller).toBeInstanceOf(Controller);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('should create data client with custom managers', () => {
|
|
42
|
+
const customNetworkManager = new NetworkManager();
|
|
43
|
+
const customSubscriptionManager = new SubscriptionManager(
|
|
44
|
+
PollingSubscription,
|
|
45
|
+
);
|
|
46
|
+
const customManagers = [customNetworkManager, customSubscriptionManager];
|
|
47
|
+
|
|
48
|
+
const provider = createDataClient({ managers: customManagers });
|
|
49
|
+
|
|
50
|
+
expect(provider.controller).toBeInstanceOf(Controller);
|
|
51
|
+
expect(provider.stateRef).toBeDefined();
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('should create data client with custom initialState', () => {
|
|
55
|
+
const customState: State<unknown> = {
|
|
56
|
+
entities: {},
|
|
57
|
+
indexes: {},
|
|
58
|
+
endpoints: {},
|
|
59
|
+
meta: {},
|
|
60
|
+
entitiesMeta: {},
|
|
61
|
+
optimistic: [],
|
|
62
|
+
lastReset: 12345,
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const provider = createDataClient({ initialState: customState });
|
|
66
|
+
|
|
67
|
+
expect(provider.stateRef.value.lastReset).toBe(12345);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('should handle actions through middleware chain', async () => {
|
|
71
|
+
// This test covers line 85: dispatch in middleware chain
|
|
72
|
+
const mockApp = {
|
|
73
|
+
provide: jest.fn(),
|
|
74
|
+
} as any;
|
|
75
|
+
|
|
76
|
+
const provider = createDataClient({ app: mockApp });
|
|
77
|
+
provider.start();
|
|
78
|
+
|
|
79
|
+
// Create a mock action to dispatch through the middleware
|
|
80
|
+
const testAction = {
|
|
81
|
+
type: 'test/SET',
|
|
82
|
+
payload: { id: '1', value: 'test' },
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
// Dispatch through controller which goes through middleware chain
|
|
86
|
+
await provider.controller.dispatch(testAction as any);
|
|
87
|
+
|
|
88
|
+
// Verify the action was processed
|
|
89
|
+
expect(provider.stateRef.value).toBeDefined();
|
|
90
|
+
|
|
91
|
+
provider.stop();
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('should call start and stop lifecycle methods', () => {
|
|
95
|
+
const provider = createDataClient();
|
|
96
|
+
|
|
97
|
+
// Start should initialize managers
|
|
98
|
+
expect(() => provider.start()).not.toThrow();
|
|
99
|
+
|
|
100
|
+
// Stop should cleanup managers
|
|
101
|
+
expect(() => provider.stop()).not.toThrow();
|
|
102
|
+
|
|
103
|
+
// Stop should be safe to call even without cleanup
|
|
104
|
+
const provider2 = createDataClient();
|
|
105
|
+
expect(() => provider2.stop()).not.toThrow();
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it('should handle middleware dispatch chain', async () => {
|
|
109
|
+
// Create a custom manager that dispatches from middleware (covers line 85)
|
|
110
|
+
class CustomManager implements Manager {
|
|
111
|
+
middleware: Middleware;
|
|
112
|
+
|
|
113
|
+
constructor() {
|
|
114
|
+
this.middleware =
|
|
115
|
+
({ dispatch, getState }) =>
|
|
116
|
+
next =>
|
|
117
|
+
async action => {
|
|
118
|
+
// This dispatch call covers line 85 - middleware calling dispatch
|
|
119
|
+
if (action.type === actionTypes.SET) {
|
|
120
|
+
// Dispatch a RESET action from within middleware
|
|
121
|
+
await dispatch({ type: actionTypes.RESET } as any);
|
|
122
|
+
}
|
|
123
|
+
return next(action);
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
cleanup() {}
|
|
128
|
+
init() {
|
|
129
|
+
return () => {};
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const mockApp = {
|
|
134
|
+
provide: jest.fn(),
|
|
135
|
+
} as any;
|
|
136
|
+
|
|
137
|
+
// Capture console.warn since we're not including NetworkManager
|
|
138
|
+
const consoleWarnSpy = jest
|
|
139
|
+
.spyOn(console, 'warn')
|
|
140
|
+
.mockImplementation(() => {});
|
|
141
|
+
|
|
142
|
+
const customManager = new CustomManager();
|
|
143
|
+
const provider = createDataClient({
|
|
144
|
+
managers: [customManager],
|
|
145
|
+
app: mockApp,
|
|
146
|
+
});
|
|
147
|
+
provider.start();
|
|
148
|
+
|
|
149
|
+
// Verify the NetworkManager warning was triggered
|
|
150
|
+
expect(consoleWarnSpy).toHaveBeenCalledWith(
|
|
151
|
+
'NetworkManager not found; this is a required manager.',
|
|
152
|
+
);
|
|
153
|
+
expect(consoleWarnSpy).toHaveBeenCalledWith(
|
|
154
|
+
'See https://dataclient.io/docs/guides/redux for hooking up redux',
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
// Dispatch a SET action that triggers middleware to dispatch a RESET action
|
|
158
|
+
await provider.controller.dispatch({ type: actionTypes.SET } as any);
|
|
159
|
+
|
|
160
|
+
provider.stop();
|
|
161
|
+
|
|
162
|
+
consoleWarnSpy.mockRestore();
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it('should compute optimistic state correctly', async () => {
|
|
166
|
+
const provider = createDataClient();
|
|
167
|
+
provider.start();
|
|
168
|
+
|
|
169
|
+
// Initial state should have no optimistic updates
|
|
170
|
+
expect(provider.stateRef.value.optimistic).toEqual([]);
|
|
171
|
+
|
|
172
|
+
// The state should be reactive
|
|
173
|
+
const initialState = provider.stateRef.value;
|
|
174
|
+
expect(initialState).toBeDefined();
|
|
175
|
+
|
|
176
|
+
provider.stop();
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it('should use app.provide when app option is provided', () => {
|
|
180
|
+
const mockApp = {
|
|
181
|
+
provide: jest.fn(),
|
|
182
|
+
} as any;
|
|
183
|
+
|
|
184
|
+
const provider = createDataClient({ app: mockApp });
|
|
185
|
+
|
|
186
|
+
// Should use app.provide instead of provide()
|
|
187
|
+
expect(mockApp.provide).toHaveBeenCalledTimes(2);
|
|
188
|
+
expect(mockApp.provide).toHaveBeenCalledWith(StateKey, provider.stateRef);
|
|
189
|
+
expect(mockApp.provide).toHaveBeenCalledWith(
|
|
190
|
+
ControllerKey,
|
|
191
|
+
provider.controller,
|
|
192
|
+
);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it('should accept custom gcPolicy', () => {
|
|
196
|
+
const customGcPolicy = {
|
|
197
|
+
expireAt: Date.now() + 10000,
|
|
198
|
+
} as any;
|
|
199
|
+
|
|
200
|
+
const provider = createDataClient({ gcPolicy: customGcPolicy });
|
|
201
|
+
|
|
202
|
+
expect(provider.controller).toBeInstanceOf(Controller);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it('should maintain stable controller and manager instances', () => {
|
|
206
|
+
const provider1 = createDataClient();
|
|
207
|
+
const provider2 = createDataClient();
|
|
208
|
+
|
|
209
|
+
// Different providers should have different controller instances
|
|
210
|
+
expect(provider1.controller).not.toBe(provider2.controller);
|
|
211
|
+
|
|
212
|
+
// Same provider should maintain stable controller
|
|
213
|
+
const controller1 = provider1.controller;
|
|
214
|
+
expect(provider1.controller).toBe(controller1);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it('should handle sequential dispatches correctly', async () => {
|
|
218
|
+
const mockApp = {
|
|
219
|
+
provide: jest.fn(),
|
|
220
|
+
} as any;
|
|
221
|
+
|
|
222
|
+
const provider = createDataClient({ app: mockApp });
|
|
223
|
+
provider.start();
|
|
224
|
+
|
|
225
|
+
// Dispatch multiple actions sequentially to test both branches of resolveNext check
|
|
226
|
+
await provider.controller.dispatch({ type: actionTypes.RESET } as any);
|
|
227
|
+
await provider.controller.dispatch({ type: actionTypes.RESET } as any);
|
|
228
|
+
|
|
229
|
+
// Verify state is updated
|
|
230
|
+
expect(provider.stateRef.value).toBeDefined();
|
|
231
|
+
|
|
232
|
+
provider.stop();
|
|
233
|
+
});
|
|
234
|
+
});
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import {
|
|
2
|
+
NetworkManager,
|
|
3
|
+
SubscriptionManager,
|
|
4
|
+
PollingSubscription,
|
|
5
|
+
DevToolsManager,
|
|
6
|
+
} from '@data-client/core';
|
|
7
|
+
import { describe, it, expect, jest, beforeEach } from '@jest/globals';
|
|
8
|
+
|
|
9
|
+
import { getDefaultManagers } from '../getDefaultManagers';
|
|
10
|
+
|
|
11
|
+
describe('getDefaultManagers', () => {
|
|
12
|
+
const originalNodeEnv = process.env.NODE_ENV;
|
|
13
|
+
let consoleErrorSpy: jest.SpiedFunction<typeof console.error>;
|
|
14
|
+
|
|
15
|
+
beforeEach(() => {
|
|
16
|
+
jest.clearAllMocks();
|
|
17
|
+
consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
afterEach(() => {
|
|
21
|
+
process.env.NODE_ENV = originalNodeEnv;
|
|
22
|
+
consoleErrorSpy.mockRestore();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('should return default managers', () => {
|
|
26
|
+
const managers = getDefaultManagers();
|
|
27
|
+
|
|
28
|
+
expect(managers.length).toBeGreaterThan(0);
|
|
29
|
+
expect(managers.some(m => m instanceof NetworkManager)).toBe(true);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('should return only NetworkManager when subscriptionManager is null', () => {
|
|
33
|
+
const managers = getDefaultManagers({ subscriptionManager: null });
|
|
34
|
+
|
|
35
|
+
expect(managers.length).toBeGreaterThanOrEqual(1);
|
|
36
|
+
expect(managers.some(m => m instanceof NetworkManager)).toBe(true);
|
|
37
|
+
expect(managers.some(m => m instanceof SubscriptionManager)).toBe(false);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('should accept custom NetworkManager instance', () => {
|
|
41
|
+
const customNetworkManager = new NetworkManager();
|
|
42
|
+
const managers = getDefaultManagers({
|
|
43
|
+
networkManager: customNetworkManager,
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// Should return the instance directly (covers line 62)
|
|
47
|
+
expect(managers.some(m => m === customNetworkManager)).toBe(true);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('should accept custom SubscriptionManager instance', () => {
|
|
51
|
+
const customSubscriptionManager = new SubscriptionManager(
|
|
52
|
+
PollingSubscription,
|
|
53
|
+
);
|
|
54
|
+
const managers = getDefaultManagers({
|
|
55
|
+
subscriptionManager: customSubscriptionManager,
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// Should return the instance directly (covers line 62)
|
|
59
|
+
expect(managers.some(m => m === customSubscriptionManager)).toBe(true);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('should accept NetworkManager options', () => {
|
|
63
|
+
const managers = getDefaultManagers({
|
|
64
|
+
networkManager: { dataExpiryLength: 60000 },
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
expect(managers.some(m => m instanceof NetworkManager)).toBe(true);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('should accept SubscriptionManager options', () => {
|
|
71
|
+
const managers = getDefaultManagers({
|
|
72
|
+
subscriptionManager: PollingSubscription,
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
expect(managers.some(m => m instanceof SubscriptionManager)).toBe(true);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
describe('development mode', () => {
|
|
79
|
+
beforeEach(() => {
|
|
80
|
+
// Ensure we're in development mode
|
|
81
|
+
process.env.NODE_ENV = 'development';
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('should log error when networkManager is null in dev mode', () => {
|
|
85
|
+
const managers = getDefaultManagers({
|
|
86
|
+
networkManager: null,
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
// Should log error (covers line 32)
|
|
90
|
+
expect(consoleErrorSpy).toHaveBeenCalledWith(
|
|
91
|
+
'Disabling NetworkManager is not allowed.',
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
// Should still return NetworkManager (fallback to default)
|
|
95
|
+
expect(managers.some(m => m instanceof NetworkManager)).toBe(true);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('should include DevToolsManager by default in dev mode', () => {
|
|
99
|
+
const managers = getDefaultManagers();
|
|
100
|
+
|
|
101
|
+
expect(managers.some(m => m instanceof DevToolsManager)).toBe(true);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('should accept DevToolsManager instance', () => {
|
|
105
|
+
const customDevToolsManager = new DevToolsManager({}, () => false);
|
|
106
|
+
|
|
107
|
+
const managers = getDefaultManagers({
|
|
108
|
+
devToolsManager: customDevToolsManager,
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
// Should return the instance directly (covers line 43)
|
|
112
|
+
expect(managers.some(m => m === customDevToolsManager)).toBe(true);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('should accept DevToolsManager config', () => {
|
|
116
|
+
const managers = getDefaultManagers({
|
|
117
|
+
devToolsManager: {},
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
expect(managers.some(m => m instanceof DevToolsManager)).toBe(true);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('should exclude DevToolsManager when explicitly set to null', () => {
|
|
124
|
+
const managers = getDefaultManagers({
|
|
125
|
+
devToolsManager: null,
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
expect(managers.some(m => m instanceof DevToolsManager)).toBe(false);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('should place DevToolsManager first in the array', () => {
|
|
132
|
+
const managers = getDefaultManagers({
|
|
133
|
+
devToolsManager: {},
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
expect(managers[0]).toBeInstanceOf(DevToolsManager);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('should work with all custom options combined', () => {
|
|
140
|
+
const customNetworkManager = new NetworkManager();
|
|
141
|
+
const customSubscriptionManager = new SubscriptionManager(
|
|
142
|
+
PollingSubscription,
|
|
143
|
+
);
|
|
144
|
+
const customDevToolsManager = new DevToolsManager({}, () => false);
|
|
145
|
+
|
|
146
|
+
const managers = getDefaultManagers({
|
|
147
|
+
networkManager: customNetworkManager,
|
|
148
|
+
subscriptionManager: customSubscriptionManager,
|
|
149
|
+
devToolsManager: customDevToolsManager,
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
expect(managers.some(m => m === customDevToolsManager)).toBe(true);
|
|
153
|
+
expect(managers.some(m => m === customNetworkManager)).toBe(true);
|
|
154
|
+
expect(managers.some(m => m === customSubscriptionManager)).toBe(true);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it('should exclude subscription manager when null in dev mode', () => {
|
|
158
|
+
const managers = getDefaultManagers({
|
|
159
|
+
subscriptionManager: null,
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
expect(managers.some(m => m instanceof SubscriptionManager)).toBe(false);
|
|
163
|
+
expect(managers.some(m => m instanceof NetworkManager)).toBe(true);
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
describe('production mode', () => {
|
|
168
|
+
beforeEach(() => {
|
|
169
|
+
process.env.NODE_ENV = 'production';
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it('should not include DevToolsManager in production', () => {
|
|
173
|
+
const managers = getDefaultManagers();
|
|
174
|
+
|
|
175
|
+
// In production, DevToolsManager should not be included by default
|
|
176
|
+
// Note: This test may vary based on the actual implementation
|
|
177
|
+
expect(managers.some(m => m instanceof NetworkManager)).toBe(true);
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
});
|
|
@@ -7,7 +7,10 @@ import {
|
|
|
7
7
|
} from '@data-client/core';
|
|
8
8
|
|
|
9
9
|
/* istanbul ignore next */
|
|
10
|
-
/** Returns the default Managers used by DataProvider.
|
|
10
|
+
/** Returns the default Managers used by DataProvider.
|
|
11
|
+
*
|
|
12
|
+
* @see https://dataclient.io/docs/api/getDefaultManagers
|
|
13
|
+
*/
|
|
11
14
|
let getDefaultManagers: (options?: GetManagersOptions) => Manager[] = (
|
|
12
15
|
options = {},
|
|
13
16
|
) => {
|
package/src/providers/index.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
1
|
export { getDefaultManagers } from './getDefaultManagers.js';
|
|
2
|
-
export {
|
|
2
|
+
export {
|
|
3
|
+
createDataClient,
|
|
4
|
+
type ProvideOptions,
|
|
5
|
+
type ProvidedDataClient,
|
|
6
|
+
} from './createDataClient.js';
|
|
3
7
|
export { DataClientPlugin } from './DataClientPlugin.js';
|
package/src/test/README.md
CHANGED
|
@@ -13,7 +13,7 @@ npm install @data-client/vue
|
|
|
13
13
|
### Basic Component Testing
|
|
14
14
|
|
|
15
15
|
```typescript
|
|
16
|
-
import {
|
|
16
|
+
import { mountDataClient } from '@data-client/vue/test';
|
|
17
17
|
import { defineComponent, h } from 'vue';
|
|
18
18
|
import { MyResource } from './resources';
|
|
19
19
|
|
|
@@ -23,7 +23,7 @@ const TestComponent = defineComponent({
|
|
|
23
23
|
},
|
|
24
24
|
});
|
|
25
25
|
|
|
26
|
-
const { wrapper, controller, cleanup } =
|
|
26
|
+
const { wrapper, controller, cleanup } = mountDataClient(TestComponent, {
|
|
27
27
|
initialFixtures: [
|
|
28
28
|
{
|
|
29
29
|
endpoint: MyResource.get,
|
|
@@ -42,10 +42,10 @@ cleanup();
|
|
|
42
42
|
|
|
43
43
|
### Testing with Suspense (Automatic)
|
|
44
44
|
|
|
45
|
-
Suspense is automatically integrated into `
|
|
45
|
+
Suspense is automatically integrated into `mountDataClient`. Your components will automatically suspend and show a fallback while data is loading:
|
|
46
46
|
|
|
47
47
|
```typescript
|
|
48
|
-
import {
|
|
48
|
+
import { mountDataClient } from '@data-client/vue/test';
|
|
49
49
|
import { defineComponent, h } from 'vue';
|
|
50
50
|
import { MyResource } from './resources';
|
|
51
51
|
|
|
@@ -56,7 +56,7 @@ const AsyncComponent = defineComponent({
|
|
|
56
56
|
},
|
|
57
57
|
});
|
|
58
58
|
|
|
59
|
-
const { wrapper, controller, cleanup } =
|
|
59
|
+
const { wrapper, controller, cleanup } = mountDataClient(AsyncComponent);
|
|
60
60
|
|
|
61
61
|
// Initially shows suspense fallback
|
|
62
62
|
expect(wrapper.find('[data-testid="suspense-fallback"]').exists()).toBe(true);
|
|
@@ -73,15 +73,17 @@ cleanup();
|
|
|
73
73
|
### Composable Testing
|
|
74
74
|
|
|
75
75
|
```typescript
|
|
76
|
-
import {
|
|
76
|
+
import { renderDataCompose } from '@data-client/vue/test';
|
|
77
|
+
import { reactive } from 'vue';
|
|
77
78
|
|
|
78
|
-
const useMyComposable = (id: number) => {
|
|
79
|
-
const data = useSuspense(MyResource.get, { id });
|
|
79
|
+
const useMyComposable = (props: { id: number }) => {
|
|
80
|
+
const data = useSuspense(MyResource.get, { id: props.id });
|
|
80
81
|
return { data, isLoading: false };
|
|
81
82
|
};
|
|
82
83
|
|
|
83
|
-
const
|
|
84
|
-
|
|
84
|
+
const props = reactive({ id: 1 });
|
|
85
|
+
const { result, controller, cleanup, waitForNextUpdate } = renderDataCompose(useMyComposable, {
|
|
86
|
+
props,
|
|
85
87
|
initialFixtures: [
|
|
86
88
|
{
|
|
87
89
|
endpoint: MyResource.get,
|
|
@@ -104,13 +106,70 @@ expect(result.current).toBeInstanceOf(Promise);
|
|
|
104
106
|
// Access the actual data
|
|
105
107
|
const dataRef = await result.current;
|
|
106
108
|
expect(dataRef.value).toBeDefined();
|
|
109
|
+
|
|
110
|
+
// Update props reactively
|
|
111
|
+
props.id = 2;
|
|
112
|
+
|
|
113
|
+
cleanup();
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### Testing with Reactive Props
|
|
117
|
+
|
|
118
|
+
Components can receive reactive props that can be updated during tests:
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
import { mountDataClient } from '@data-client/vue/test';
|
|
122
|
+
import { defineComponent, h, reactive } from 'vue';
|
|
123
|
+
import { MyResource } from './resources';
|
|
124
|
+
|
|
125
|
+
const ArticleComponent = defineComponent({
|
|
126
|
+
props: {
|
|
127
|
+
id: {
|
|
128
|
+
type: Number,
|
|
129
|
+
required: true,
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
async setup(props) {
|
|
133
|
+
const article = await useSuspense(MyResource.get, { id: props.id });
|
|
134
|
+
return () => h('div', article.value.title);
|
|
135
|
+
},
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
// Create a reactive props ref
|
|
139
|
+
const props = reactive({ id: 1 });
|
|
140
|
+
|
|
141
|
+
const { wrapper, cleanup } = mountDataClient(ArticleComponent, {
|
|
142
|
+
props,
|
|
143
|
+
resolverFixtures: [
|
|
144
|
+
{
|
|
145
|
+
endpoint: MyResource.get,
|
|
146
|
+
response: (request) => ({
|
|
147
|
+
id: request.args[0].id,
|
|
148
|
+
title: `Article ${request.args[0].id}`,
|
|
149
|
+
}),
|
|
150
|
+
},
|
|
151
|
+
],
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
// Wait for initial render
|
|
155
|
+
await flushUntil(wrapper, () => wrapper.find('div').exists());
|
|
156
|
+
expect(wrapper.text()).toBe('Article 1');
|
|
157
|
+
|
|
158
|
+
// Update props reactively - component will re-render with new data
|
|
159
|
+
props.id = 2;
|
|
160
|
+
await nextTick();
|
|
161
|
+
|
|
162
|
+
// Wait for new data to load
|
|
163
|
+
await flushUntil(wrapper, () => wrapper.text() === 'Article 2');
|
|
164
|
+
expect(wrapper.text()).toBe('Article 2');
|
|
165
|
+
|
|
107
166
|
cleanup();
|
|
108
167
|
```
|
|
109
168
|
|
|
110
169
|
### Using Resolver Fixtures
|
|
111
170
|
|
|
112
171
|
```typescript
|
|
113
|
-
const { wrapper, controller, cleanup } =
|
|
172
|
+
const { wrapper, controller, cleanup } = mountDataClient(TestComponent, {
|
|
114
173
|
resolverFixtures: [
|
|
115
174
|
{
|
|
116
175
|
endpoint: MyResource.get,
|
|
@@ -131,7 +190,7 @@ expect(result.name).toBe('Dynamic 123');
|
|
|
131
190
|
|
|
132
191
|
## API Reference
|
|
133
192
|
|
|
134
|
-
### `
|
|
193
|
+
### `mountDataClient(component, options)`
|
|
135
194
|
|
|
136
195
|
Renders a Vue component with DataClient provider for testing.
|
|
137
196
|
|
|
@@ -146,7 +205,7 @@ Renders a Vue component with DataClient provider for testing.
|
|
|
146
205
|
- `cleanup`: Function to clean up resources
|
|
147
206
|
- `allSettled`: Function to wait for all pending promises
|
|
148
207
|
|
|
149
|
-
### `
|
|
208
|
+
### `renderDataCompose(composable, options)`
|
|
150
209
|
|
|
151
210
|
Renders a Vue composable with DataClient provider for testing.
|
|
152
211
|
|
|
@@ -166,12 +225,13 @@ Renders a Vue composable with DataClient provider for testing.
|
|
|
166
225
|
|
|
167
226
|
```typescript
|
|
168
227
|
interface RenderDataClientOptions<P = any> {
|
|
169
|
-
|
|
228
|
+
props?: Reactive<P>; // Reactive props ref to pass to component
|
|
170
229
|
initialFixtures?: readonly Fixture[]; // Initial data fixtures
|
|
171
230
|
resolverFixtures?: readonly (Fixture | Interceptor)[]; // Dynamic response fixtures
|
|
172
231
|
getInitialInterceptorData?: () => any; // Initial data for interceptors
|
|
173
232
|
managers?: Manager[]; // Custom managers
|
|
174
233
|
initialState?: State<unknown>; // Custom initial state
|
|
234
|
+
gcPolicy?: GCInterface; // Custom garbage collection policy
|
|
175
235
|
wrapper?: any; // Custom wrapper component
|
|
176
236
|
}
|
|
177
237
|
```
|
|
@@ -207,8 +267,9 @@ interface Interceptor {
|
|
|
207
267
|
4. **Use resolver fixtures** for dynamic responses based on request parameters
|
|
208
268
|
5. **Leverage the controller** for testing mutations and state updates
|
|
209
269
|
6. **Suspense is automatic** - no need to manually wrap components in Suspense
|
|
210
|
-
7. **Use
|
|
270
|
+
7. **Use reactive props** - Pass a `reactive` in the `props` option and set its members to change component props
|
|
211
271
|
8. **Vue Suspense behavior** - Vue's `useSuspense` returns a Promise that suspends when data is missing, then resolves to a ComputedRef
|
|
272
|
+
9. **Reactive props with async setup** - Components using async setup with `useSuspense` that depend on props should use `useFetch` + `watchEffect` for reactive behavior, or rely on non-async setup patterns. Async setup only runs once per component instance.
|
|
212
273
|
|
|
213
274
|
## Migration from Manual Setup
|
|
214
275
|
|
|
@@ -231,7 +292,7 @@ const wrapper = mount(MyComponent, {
|
|
|
231
292
|
});
|
|
232
293
|
|
|
233
294
|
// After
|
|
234
|
-
const { wrapper, controller, cleanup } =
|
|
295
|
+
const { wrapper, controller, cleanup } = mountDataClient(MyComponent, {
|
|
235
296
|
initialFixtures: [
|
|
236
297
|
{
|
|
237
298
|
endpoint: MyResource.get,
|