@data-client/vue 0.0.5 → 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/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,245 @@
|
|
|
1
|
+
# Vue Data Client Testing Utilities
|
|
2
|
+
|
|
3
|
+
This package provides testing utilities for Vue applications using `@data-client/vue`, similar to `@data-client/test` for React.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @data-client/vue
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Basic Component Testing
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { renderDataClient } from '@data-client/vue/test';
|
|
17
|
+
import { defineComponent, h } from 'vue';
|
|
18
|
+
import { MyResource } from './resources';
|
|
19
|
+
|
|
20
|
+
const TestComponent = defineComponent({
|
|
21
|
+
setup() {
|
|
22
|
+
return () => h('div', 'Hello World');
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
const { wrapper, controller, cleanup } = renderDataClient(TestComponent, {
|
|
27
|
+
initialFixtures: [
|
|
28
|
+
{
|
|
29
|
+
endpoint: MyResource.get,
|
|
30
|
+
args: [{ id: 1 }],
|
|
31
|
+
response: { id: 1, name: 'Test' },
|
|
32
|
+
},
|
|
33
|
+
],
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// Your tests here
|
|
37
|
+
expect(wrapper.text()).toBe('Hello World');
|
|
38
|
+
|
|
39
|
+
// Clean up after test
|
|
40
|
+
cleanup();
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Testing with Suspense (Automatic)
|
|
44
|
+
|
|
45
|
+
Suspense is automatically integrated into `renderDataClient`. Your components will automatically suspend and show a fallback while data is loading:
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
import { renderDataClient } from '@data-client/vue/test';
|
|
49
|
+
import { defineComponent, h } from 'vue';
|
|
50
|
+
import { MyResource } from './resources';
|
|
51
|
+
|
|
52
|
+
const AsyncComponent = defineComponent({
|
|
53
|
+
async setup() {
|
|
54
|
+
const data = await useSuspense(MyResource.get, { id: 1 });
|
|
55
|
+
return () => h('div', data.value.name);
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
const { wrapper, controller, cleanup } = renderDataClient(AsyncComponent);
|
|
60
|
+
|
|
61
|
+
// Initially shows suspense fallback
|
|
62
|
+
expect(wrapper.find('[data-testid="suspense-fallback"]').exists()).toBe(true);
|
|
63
|
+
|
|
64
|
+
// Wait for data to load
|
|
65
|
+
await flushUntil(wrapper, () => wrapper.find('div').exists());
|
|
66
|
+
|
|
67
|
+
// Now shows the actual content
|
|
68
|
+
expect(wrapper.text()).toBe('Test');
|
|
69
|
+
|
|
70
|
+
cleanup();
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Composable Testing
|
|
74
|
+
|
|
75
|
+
```typescript
|
|
76
|
+
import { renderDataComposable } from '@data-client/vue/test';
|
|
77
|
+
|
|
78
|
+
const useMyComposable = (id: number) => {
|
|
79
|
+
const data = useSuspense(MyResource.get, { id });
|
|
80
|
+
return { data, isLoading: false };
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const { result, controller, cleanup, waitForNextUpdate } = renderDataComposable(useMyComposable, {
|
|
84
|
+
initialProps: { id: 1 },
|
|
85
|
+
initialFixtures: [
|
|
86
|
+
{
|
|
87
|
+
endpoint: MyResource.get,
|
|
88
|
+
args: [{ id: 1 }],
|
|
89
|
+
response: { id: 1, name: 'Test' },
|
|
90
|
+
},
|
|
91
|
+
],
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
// Initially suspended
|
|
95
|
+
expect(result.current).toBeUndefined();
|
|
96
|
+
|
|
97
|
+
// Wait for the composable to resolve
|
|
98
|
+
await waitForNextUpdate();
|
|
99
|
+
|
|
100
|
+
// Now should have the actual data (useSuspense returns a Promise that resolves to ComputedRef)
|
|
101
|
+
expect(result.current).toBeDefined();
|
|
102
|
+
expect(result.current).toBeInstanceOf(Promise);
|
|
103
|
+
|
|
104
|
+
// Access the actual data
|
|
105
|
+
const dataRef = await result.current;
|
|
106
|
+
expect(dataRef.value).toBeDefined();
|
|
107
|
+
cleanup();
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Using Resolver Fixtures
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
const { wrapper, controller, cleanup } = renderDataClient(TestComponent, {
|
|
114
|
+
resolverFixtures: [
|
|
115
|
+
{
|
|
116
|
+
endpoint: MyResource.get,
|
|
117
|
+
response: (request) => {
|
|
118
|
+
return {
|
|
119
|
+
id: request.args[0].id,
|
|
120
|
+
name: `Dynamic ${request.args[0].id}`,
|
|
121
|
+
};
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
],
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
// Test dynamic responses
|
|
128
|
+
const result = await controller.fetch(MyResource.get, { id: 123 });
|
|
129
|
+
expect(result.name).toBe('Dynamic 123');
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## API Reference
|
|
133
|
+
|
|
134
|
+
### `renderDataClient(component, options)`
|
|
135
|
+
|
|
136
|
+
Renders a Vue component with DataClient provider for testing.
|
|
137
|
+
|
|
138
|
+
**Parameters:**
|
|
139
|
+
- `component`: Vue component to render
|
|
140
|
+
- `options`: Configuration options
|
|
141
|
+
|
|
142
|
+
**Returns:**
|
|
143
|
+
- `wrapper`: Vue Test Utils wrapper
|
|
144
|
+
- `controller`: DataClient controller instance
|
|
145
|
+
- `app`: Vue app instance
|
|
146
|
+
- `cleanup`: Function to clean up resources
|
|
147
|
+
- `allSettled`: Function to wait for all pending promises
|
|
148
|
+
|
|
149
|
+
### `renderDataComposable(composable, options)`
|
|
150
|
+
|
|
151
|
+
Renders a Vue composable with DataClient provider for testing.
|
|
152
|
+
|
|
153
|
+
**Parameters:**
|
|
154
|
+
- `composable`: Vue composable function
|
|
155
|
+
- `options`: Configuration options
|
|
156
|
+
|
|
157
|
+
**Returns:**
|
|
158
|
+
- `result`: Object with `current` property containing the composable's return value (`undefined` when suspended, Promise when resolved)
|
|
159
|
+
- `wrapper`: Vue Test Utils wrapper
|
|
160
|
+
- `controller`: DataClient controller instance
|
|
161
|
+
- `cleanup`: Function to clean up resources
|
|
162
|
+
- `allSettled`: Function to wait for all pending promises
|
|
163
|
+
- `waitForNextUpdate`: Function to wait for the composable to resolve from suspended state
|
|
164
|
+
|
|
165
|
+
### Options
|
|
166
|
+
|
|
167
|
+
```typescript
|
|
168
|
+
interface RenderDataClientOptions<P = any> {
|
|
169
|
+
initialProps?: P; // Initial props to pass to component
|
|
170
|
+
initialFixtures?: readonly Fixture[]; // Initial data fixtures
|
|
171
|
+
resolverFixtures?: readonly (Fixture | Interceptor)[]; // Dynamic response fixtures
|
|
172
|
+
getInitialInterceptorData?: () => any; // Initial data for interceptors
|
|
173
|
+
managers?: Manager[]; // Custom managers
|
|
174
|
+
initialState?: State<unknown>; // Custom initial state
|
|
175
|
+
wrapper?: any; // Custom wrapper component
|
|
176
|
+
}
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### Fixture Types
|
|
180
|
+
|
|
181
|
+
```typescript
|
|
182
|
+
interface Fixture {
|
|
183
|
+
endpoint: FixtureEndpoint;
|
|
184
|
+
args: any[];
|
|
185
|
+
response: any;
|
|
186
|
+
error?: boolean;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
interface Interceptor {
|
|
190
|
+
endpoint: FixtureEndpoint;
|
|
191
|
+
response: (request: {
|
|
192
|
+
body?: any;
|
|
193
|
+
headers?: Record<string, string>;
|
|
194
|
+
url: string;
|
|
195
|
+
method: string;
|
|
196
|
+
args: any[];
|
|
197
|
+
}) => any | Promise<any>;
|
|
198
|
+
error?: boolean;
|
|
199
|
+
}
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
## Best Practices
|
|
203
|
+
|
|
204
|
+
1. **Always call cleanup()** after your tests to prevent memory leaks
|
|
205
|
+
2. **Use fixtures** instead of mocking network requests when possible
|
|
206
|
+
3. **Test both success and error cases** using the `error` property in fixtures
|
|
207
|
+
4. **Use resolver fixtures** for dynamic responses based on request parameters
|
|
208
|
+
5. **Leverage the controller** for testing mutations and state updates
|
|
209
|
+
6. **Suspense is automatic** - no need to manually wrap components in Suspense
|
|
210
|
+
7. **Use initialProps** to pass props to your components during testing
|
|
211
|
+
8. **Vue Suspense behavior** - Vue's `useSuspense` returns a Promise that suspends when data is missing, then resolves to a ComputedRef
|
|
212
|
+
|
|
213
|
+
## Migration from Manual Setup
|
|
214
|
+
|
|
215
|
+
If you were previously setting up DataClient manually in tests:
|
|
216
|
+
|
|
217
|
+
```typescript
|
|
218
|
+
// Before
|
|
219
|
+
const wrapper = mount(MyComponent, {
|
|
220
|
+
global: {
|
|
221
|
+
plugins: [
|
|
222
|
+
[
|
|
223
|
+
DataClientPlugin,
|
|
224
|
+
{
|
|
225
|
+
managers: [new NetworkManager()],
|
|
226
|
+
initialState: mockState,
|
|
227
|
+
},
|
|
228
|
+
],
|
|
229
|
+
],
|
|
230
|
+
},
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
// After
|
|
234
|
+
const { wrapper, controller, cleanup } = renderDataClient(MyComponent, {
|
|
235
|
+
initialFixtures: [
|
|
236
|
+
{
|
|
237
|
+
endpoint: MyResource.get,
|
|
238
|
+
args: [{ id: 1 }],
|
|
239
|
+
response: { id: 1, name: 'Test' },
|
|
240
|
+
},
|
|
241
|
+
],
|
|
242
|
+
});
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
This approach is more declarative, easier to maintain, and provides better test isolation.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Fixture, ResponseInterceptor } from './fixtureTypes';
|
|
2
|
+
|
|
3
|
+
export async function collapseFixture(
|
|
4
|
+
fixture: Fixture | ResponseInterceptor,
|
|
5
|
+
args: any[],
|
|
6
|
+
interceptorData: any,
|
|
7
|
+
) {
|
|
8
|
+
let error = 'error' in fixture ? fixture.error : false;
|
|
9
|
+
let response = fixture.response;
|
|
10
|
+
if (typeof fixture.response === 'function') {
|
|
11
|
+
try {
|
|
12
|
+
response = await fixture.response.apply(interceptorData, args);
|
|
13
|
+
// dispatch goes through user-code that can sometimes fail.
|
|
14
|
+
// let's ensure we always handle errors
|
|
15
|
+
} catch (e: any) {
|
|
16
|
+
response = e;
|
|
17
|
+
error = true;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return { response, error };
|
|
21
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Fixture, Interceptor } from './fixtureTypes';
|
|
2
|
+
|
|
3
|
+
export function createFixtureMap(fixtures: (Fixture | Interceptor)[] = []) {
|
|
4
|
+
const map: Map<string, Fixture> = new Map();
|
|
5
|
+
const computed: Interceptor[] = [];
|
|
6
|
+
for (const fixture of fixtures) {
|
|
7
|
+
if ('args' in fixture) {
|
|
8
|
+
if (typeof fixture.response !== 'function') {
|
|
9
|
+
const key = fixture.endpoint.key(...fixture.args);
|
|
10
|
+
map.set(key, fixture);
|
|
11
|
+
} else {
|
|
12
|
+
// this has to be a typo. probably needs to remove args
|
|
13
|
+
console.warn(
|
|
14
|
+
`Fixture found with function response, and explicit args. Interceptors should not specify args.
|
|
15
|
+
${fixture.endpoint.name}: ${JSON.stringify(fixture.args)}
|
|
16
|
+
|
|
17
|
+
Treating as Interceptor`,
|
|
18
|
+
);
|
|
19
|
+
computed.push(fixture as any);
|
|
20
|
+
}
|
|
21
|
+
} else {
|
|
22
|
+
computed.push(fixture);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return [map, computed] as const;
|
|
26
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import type { EndpointInterface, ResolveType } from '@data-client/core';
|
|
2
|
+
|
|
3
|
+
type Updater = (
|
|
4
|
+
result: any,
|
|
5
|
+
...args: any
|
|
6
|
+
) => Record<string, (...args: any) => any>;
|
|
7
|
+
|
|
8
|
+
export interface SuccessFixtureEndpoint<
|
|
9
|
+
E extends EndpointInterface & { update?: Updater } = EndpointInterface,
|
|
10
|
+
> {
|
|
11
|
+
readonly endpoint: E;
|
|
12
|
+
readonly args: Parameters<E>;
|
|
13
|
+
readonly response:
|
|
14
|
+
| ResolveType<E>
|
|
15
|
+
| ((...args: Parameters<E>) => ResolveType<E>);
|
|
16
|
+
readonly error?: false;
|
|
17
|
+
/** Number of miliseconds to wait before resolving */
|
|
18
|
+
readonly delay?: number;
|
|
19
|
+
/** Waits to run `response()` after `delay` time */
|
|
20
|
+
readonly delayCollapse?: boolean;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface ResponseInterceptor<
|
|
24
|
+
T = any,
|
|
25
|
+
E extends EndpointInterface & {
|
|
26
|
+
update?: Updater;
|
|
27
|
+
testKey(key: string): boolean;
|
|
28
|
+
} = EndpointInterface & { testKey(key: string): boolean },
|
|
29
|
+
> {
|
|
30
|
+
readonly endpoint: E;
|
|
31
|
+
response(this: T, ...args: Parameters<E>): ResolveType<E>;
|
|
32
|
+
/** Number of miliseconds (or function that returns) to wait before resolving */
|
|
33
|
+
readonly delay?: number | ((...args: Parameters<E>) => number);
|
|
34
|
+
/** Waits to run `response()` after `delay` time */
|
|
35
|
+
readonly delayCollapse?: boolean;
|
|
36
|
+
}
|
|
37
|
+
export interface FetchInterceptor<
|
|
38
|
+
T = any,
|
|
39
|
+
E extends EndpointInterface & {
|
|
40
|
+
update?: Updater;
|
|
41
|
+
testKey(key: string): boolean;
|
|
42
|
+
fetchResponse(input: RequestInfo, init: RequestInit): Promise<Response>;
|
|
43
|
+
extend(options: any): any;
|
|
44
|
+
} = EndpointInterface & {
|
|
45
|
+
testKey(key: string): boolean;
|
|
46
|
+
fetchResponse(input: RequestInfo, init: RequestInit): Promise<Response>;
|
|
47
|
+
extend(options: any): any;
|
|
48
|
+
},
|
|
49
|
+
> {
|
|
50
|
+
readonly endpoint: E;
|
|
51
|
+
fetchResponse(this: T, input: RequestInfo, init: RequestInit): ResolveType<E>;
|
|
52
|
+
/** Number of miliseconds (or function that returns) to wait before resolving */
|
|
53
|
+
readonly delay?: number | ((...args: Parameters<E>) => number);
|
|
54
|
+
/** Waits to run `response()` after `delay` time */
|
|
55
|
+
readonly delayCollapse?: boolean;
|
|
56
|
+
}
|
|
57
|
+
/** Interceptors match and compute dynamic responses based on args
|
|
58
|
+
*
|
|
59
|
+
* @see https://dataclient.io/docs/api/Fixtures#interceptor
|
|
60
|
+
*/
|
|
61
|
+
export type Interceptor<
|
|
62
|
+
T = any,
|
|
63
|
+
E extends EndpointInterface & {
|
|
64
|
+
update?: Updater;
|
|
65
|
+
testKey(key: string): boolean;
|
|
66
|
+
fetchResponse?(input: RequestInfo, init: RequestInit): Promise<Response>;
|
|
67
|
+
extend?(options: any): any;
|
|
68
|
+
} = EndpointInterface & {
|
|
69
|
+
testKey(key: string): boolean;
|
|
70
|
+
fetchResponse(input: RequestInfo, init: RequestInit): Promise<Response>;
|
|
71
|
+
extend(options: any): any;
|
|
72
|
+
},
|
|
73
|
+
> =
|
|
74
|
+
| ResponseInterceptor<T, E>
|
|
75
|
+
| (E extends (
|
|
76
|
+
{
|
|
77
|
+
fetchResponse(input: RequestInfo, init: RequestInit): Promise<Response>;
|
|
78
|
+
extend(options: any): any;
|
|
79
|
+
}
|
|
80
|
+
) ?
|
|
81
|
+
FetchInterceptor<T, E>
|
|
82
|
+
: never);
|
|
83
|
+
|
|
84
|
+
export interface ErrorFixtureEndpoint<
|
|
85
|
+
E extends EndpointInterface & { update?: Updater } = EndpointInterface,
|
|
86
|
+
> {
|
|
87
|
+
readonly endpoint: E;
|
|
88
|
+
readonly args: Parameters<E>;
|
|
89
|
+
readonly response: any;
|
|
90
|
+
readonly error: true;
|
|
91
|
+
/** Number of miliseconds to wait before resolving */
|
|
92
|
+
readonly delay?: number;
|
|
93
|
+
/** Waits to run `response()` after `delay` time */
|
|
94
|
+
readonly delayCollapse?: boolean;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export type FixtureEndpoint<
|
|
98
|
+
E extends EndpointInterface & { update?: Updater } = EndpointInterface,
|
|
99
|
+
> = SuccessFixtureEndpoint<E> | ErrorFixtureEndpoint<E>;
|
|
100
|
+
|
|
101
|
+
/** Represents a successful response
|
|
102
|
+
*
|
|
103
|
+
* @see https://dataclient.io/docs/api/Fixtures#successfixture
|
|
104
|
+
*/
|
|
105
|
+
export type SuccessFixture<
|
|
106
|
+
E extends EndpointInterface & { update?: Updater } = EndpointInterface,
|
|
107
|
+
> = SuccessFixtureEndpoint<E>;
|
|
108
|
+
/** Represents a failed/errored response
|
|
109
|
+
*
|
|
110
|
+
* @see https://dataclient.io/docs/api/Fixtures#errorfixtures
|
|
111
|
+
*/
|
|
112
|
+
export type ErrorFixture<
|
|
113
|
+
E extends EndpointInterface & { update?: Updater } = EndpointInterface,
|
|
114
|
+
> = ErrorFixtureEndpoint<E>;
|
|
115
|
+
/** Represents a static response
|
|
116
|
+
*
|
|
117
|
+
* @see https://dataclient.io/docs/api/Fixtures
|
|
118
|
+
*/
|
|
119
|
+
export type Fixture<
|
|
120
|
+
E extends EndpointInterface & { update?: Updater } = EndpointInterface,
|
|
121
|
+
> = FixtureEndpoint<E>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export { default as MockController } from './MockController.js';
|
|
2
|
+
export { renderDataClient, renderDataComposable } from './renderDataClient.js';
|
|
3
|
+
export type {
|
|
4
|
+
RenderDataClientOptions,
|
|
5
|
+
RenderDataClientResult,
|
|
6
|
+
} from './renderDataClient.js';
|
|
7
|
+
export type {
|
|
8
|
+
FixtureEndpoint,
|
|
9
|
+
SuccessFixtureEndpoint,
|
|
10
|
+
ErrorFixtureEndpoint,
|
|
11
|
+
Fixture,
|
|
12
|
+
SuccessFixture,
|
|
13
|
+
ErrorFixture,
|
|
14
|
+
Interceptor,
|
|
15
|
+
} from './fixtureTypes.js';
|
|
16
|
+
export { default as mockInitialState } from './mockState.js';
|
|
17
|
+
|
|
18
|
+
// Re-export DataClientPlugin and createDataClient for testing
|
|
19
|
+
export { DataClientPlugin, createDataClient } from '../providers/index.js';
|
|
20
|
+
export type {
|
|
21
|
+
ProvideOptions,
|
|
22
|
+
ProvidedDataClient,
|
|
23
|
+
} from '../providers/createDataClient.js';
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SetAction,
|
|
3
|
+
State,
|
|
4
|
+
ActionTypes,
|
|
5
|
+
Controller,
|
|
6
|
+
createReducer,
|
|
7
|
+
__INTERNAL__,
|
|
8
|
+
} from '@data-client/core';
|
|
9
|
+
|
|
10
|
+
import type { Fixture } from './fixtureTypes.js';
|
|
11
|
+
|
|
12
|
+
const { initialState } = __INTERNAL__;
|
|
13
|
+
|
|
14
|
+
export default function mockInitialState(
|
|
15
|
+
fixtures: Fixture[] = [],
|
|
16
|
+
): State<unknown> {
|
|
17
|
+
const actions: SetAction[] = [];
|
|
18
|
+
const dispatch = (action: any) => {
|
|
19
|
+
actions.push(action);
|
|
20
|
+
return Promise.resolve();
|
|
21
|
+
};
|
|
22
|
+
const controller = new Controller({ dispatch });
|
|
23
|
+
const reducer: (
|
|
24
|
+
state: State<unknown> | undefined,
|
|
25
|
+
action: ActionTypes,
|
|
26
|
+
) => State<unknown> = createReducer(controller);
|
|
27
|
+
|
|
28
|
+
fixtures.forEach(fixture => {
|
|
29
|
+
dispatchFixture(fixture, fixture.args, controller);
|
|
30
|
+
});
|
|
31
|
+
return actions.reduce(reducer, initialState);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function dispatchFixture(
|
|
35
|
+
fixture: Fixture,
|
|
36
|
+
args: any[],
|
|
37
|
+
controller: Controller,
|
|
38
|
+
fetchedAt?: number,
|
|
39
|
+
) {
|
|
40
|
+
// eslint-disable-next-line prefer-const
|
|
41
|
+
let { endpoint } = fixture;
|
|
42
|
+
const { response, error } = fixture;
|
|
43
|
+
if (controller.resolve) {
|
|
44
|
+
controller.resolve(endpoint, {
|
|
45
|
+
args,
|
|
46
|
+
response,
|
|
47
|
+
error,
|
|
48
|
+
fetchedAt: fetchedAt ?? Date.now(),
|
|
49
|
+
});
|
|
50
|
+
} else {
|
|
51
|
+
if (error === true) {
|
|
52
|
+
controller.setError(endpoint, ...args, response);
|
|
53
|
+
} else {
|
|
54
|
+
controller.setResponse(endpoint, ...args, response);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|