@data-client/vue 0.18.1 → 0.18.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +371 -0
- package/README.md +86 -41
- package/dist/index.js +223 -158
- package/dist/index.umd.min.js +1 -1
- package/lib/consumers/refetchTriggers.d.ts +12 -0
- package/lib/consumers/refetchTriggers.d.ts.map +1 -0
- package/lib/consumers/refetchTriggers.js +9 -0
- package/lib/consumers/useCache.d.ts +1 -1
- package/lib/consumers/useCache.d.ts.map +1 -1
- package/lib/consumers/useCache.js +3 -53
- package/lib/consumers/useCacheResponse.d.ts +20 -0
- package/lib/consumers/useCacheResponse.d.ts.map +1 -0
- package/lib/consumers/useCacheResponse.js +55 -0
- package/lib/consumers/useCountRef.d.ts +7 -0
- package/lib/consumers/useCountRef.d.ts.map +1 -0
- package/lib/consumers/useCountRef.js +10 -0
- package/lib/consumers/useDLE.d.ts +1 -1
- package/lib/consumers/useDLE.d.ts.map +1 -1
- package/lib/consumers/useDLE.js +16 -62
- package/lib/consumers/useDebounce.d.ts +1 -1
- package/lib/consumers/useDebounce.d.ts.map +1 -1
- package/lib/consumers/useDebounce.js +1 -2
- package/lib/consumers/useExpired.d.ts +12 -0
- package/lib/consumers/useExpired.d.ts.map +1 -0
- package/lib/consumers/useExpired.js +19 -0
- package/lib/consumers/useFetch.d.ts +8 -11
- package/lib/consumers/useFetch.d.ts.map +1 -1
- package/lib/consumers/useFetch.js +13 -24
- package/lib/consumers/useLive.js +1 -1
- package/lib/consumers/useLoading.d.ts +1 -1
- package/lib/consumers/useLoading.d.ts.map +1 -1
- package/lib/consumers/useLoading.js +1 -2
- package/lib/consumers/useQuery.d.ts +1 -1
- package/lib/consumers/useQuery.d.ts.map +1 -1
- package/lib/consumers/useQuery.js +6 -12
- package/lib/consumers/useResponseMeta.d.ts +23 -0
- package/lib/consumers/useResponseMeta.d.ts.map +1 -0
- package/lib/consumers/useResponseMeta.js +36 -0
- package/lib/consumers/useSubscription.js +4 -4
- package/lib/consumers/useSuspense.d.ts +1 -1
- package/lib/consumers/useSuspense.d.ts.map +1 -1
- package/lib/consumers/useSuspense.js +58 -34
- package/lib/context.d.ts +1 -1
- package/lib/context.d.ts.map +1 -1
- package/lib/context.js +3 -3
- package/lib/index.d.ts +2 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +4 -2
- package/lib/internal.d.ts +6 -0
- package/lib/internal.d.ts.map +1 -0
- package/lib/internal.js +6 -0
- package/lib/providers/DataClientPlugin.d.ts +1 -1
- package/lib/providers/DataClientPlugin.d.ts.map +1 -1
- package/lib/providers/DataClientPlugin.js +16 -4
- package/lib/providers/createDataClient.d.ts +10 -4
- package/lib/providers/createDataClient.d.ts.map +1 -1
- package/lib/providers/createDataClient.js +11 -5
- package/lib/providers/getDefaultManagers.d.ts.map +1 -1
- package/lib/providers/getDefaultManagers.js +1 -1
- package/lib/providers/index.d.ts +2 -1
- package/lib/providers/index.d.ts.map +1 -1
- package/lib/providers/index.js +1 -1
- package/lib/test/MockPlugin.d.ts +1 -1
- package/lib/test/MockPlugin.d.ts.map +1 -1
- package/lib/test/MockPlugin.js +1 -1
- package/lib/test/index.d.ts +2 -1
- package/lib/test/index.d.ts.map +1 -1
- package/lib/test/index.js +1 -1
- package/lib/test/mountDataClient.d.ts +5 -3
- package/lib/test/mountDataClient.d.ts.map +1 -1
- package/lib/test/mountDataClient.js +15 -10
- package/lib/test/renderDataCompose.d.ts +10 -4
- package/lib/test/renderDataCompose.d.ts.map +1 -1
- package/lib/test/renderDataCompose.js +2 -3
- package/package.json +20 -5
- package/src/__tests__/__snapshots__/context.test.ts.snap +2 -2
- package/src/__tests__/context.test.ts +1 -1
- package/src/__tests__/integration-garbage-collection.web.ts +80 -4
- package/src/__tests__/useCache.web.ts +160 -93
- package/src/__tests__/useDLE.web.ts +204 -96
- package/src/__tests__/useFetch.web.ts +100 -37
- package/src/__tests__/useQuery.web.ts +60 -50
- package/src/__tests__/useSubscription.web.ts +74 -75
- package/src/__tests__/useSuspense.web.ts +455 -46
- package/src/consumers/refetchTriggers.ts +22 -0
- package/src/consumers/useCache.ts +7 -70
- package/src/consumers/useCacheResponse.ts +56 -0
- package/src/consumers/useCountRef.ts +15 -0
- package/src/consumers/useDLE.ts +24 -83
- package/src/consumers/useDebounce.ts +2 -1
- package/src/consumers/useExpired.ts +26 -0
- package/src/consumers/useFetch.ts +34 -49
- package/src/consumers/useLive.ts +1 -1
- package/src/consumers/useLoading.ts +2 -1
- package/src/consumers/useQuery.ts +10 -13
- package/src/consumers/useResponseMeta.ts +41 -0
- package/src/consumers/useSubscription.ts +3 -3
- package/src/consumers/useSuspense.ts +66 -58
- package/src/context.ts +4 -3
- package/src/index.ts +7 -1
- package/src/internal.ts +12 -0
- package/src/providers/DataClientPlugin.ts +17 -3
- package/src/providers/__tests__/DataClientPlugin.test.ts +40 -1
- package/src/providers/createDataClient.ts +15 -5
- package/src/providers/getDefaultManagers.ts +2 -6
- package/src/providers/index.ts +2 -5
- package/src/test/MockPlugin.ts +3 -3
- package/src/test/README.md +10 -304
- package/src/test/__tests__/mountDataClient.test.ts +38 -0
- package/src/test/index.ts +2 -1
- package/src/test/mountDataClient.ts +22 -17
- package/src/test/renderDataCompose.ts +11 -7
package/src/context.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { initialState, Controller } from '@data-client/core';
|
|
2
2
|
import type { State } from '@data-client/core';
|
|
3
|
-
import { inject,
|
|
3
|
+
import { inject, shallowRef } from 'vue';
|
|
4
|
+
import type { InjectionKey, ShallowRef } from 'vue';
|
|
4
5
|
|
|
5
6
|
export const StateKey: InjectionKey<ShallowRef<State<unknown>>> = Symbol(
|
|
6
7
|
'DataClientState',
|
|
@@ -20,7 +21,7 @@ export function useController(): Controller {
|
|
|
20
21
|
if (process.env.NODE_ENV !== 'production') {
|
|
21
22
|
console.error(
|
|
22
23
|
'Reactive Data Client (Vue) composables dependency injection failed. Either you are missing the DataClientPlugin or you are using composables outside of script setup.\n' +
|
|
23
|
-
'Adding DataClientPlugin: https://dataclient.io/
|
|
24
|
+
'Adding DataClientPlugin: https://dataclient.io/vue/api/DataClientPlugin\n' +
|
|
24
25
|
'Comosables only work in script setup: https://vuejs.org/guide/reusability/composables.html#usage-restrictions',
|
|
25
26
|
);
|
|
26
27
|
}
|
|
@@ -36,7 +37,7 @@ export function injectState(): ShallowRef<State<unknown>> {
|
|
|
36
37
|
if (process.env.NODE_ENV !== 'production') {
|
|
37
38
|
console.error(
|
|
38
39
|
'Reactive Data Client (Vue) composables dependency injection failed. Either you are missing the DataClientPlugin or you are using composables outside of script setup.\n' +
|
|
39
|
-
'Adding DataClientPlugin: https://dataclient.io/
|
|
40
|
+
'Adding DataClientPlugin: https://dataclient.io/vue/api/DataClientPlugin\n' +
|
|
40
41
|
'Comosables only work in script setup: https://vuejs.org/guide/reusability/composables.html#usage-restrictions',
|
|
41
42
|
);
|
|
42
43
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export {
|
|
2
|
+
Controller,
|
|
3
|
+
ExpiryStatus,
|
|
4
|
+
GCPolicy,
|
|
5
|
+
actionTypes,
|
|
6
|
+
} from '@data-client/core';
|
|
2
7
|
export type {
|
|
3
8
|
EndpointExtraOptions,
|
|
4
9
|
FetchFunction,
|
|
@@ -40,6 +45,7 @@ export type {
|
|
|
40
45
|
GenericDispatch,
|
|
41
46
|
} from '@data-client/core';
|
|
42
47
|
|
|
48
|
+
export * as __INTERNAL__ from './internal.js';
|
|
43
49
|
export * from './consumers/index.js';
|
|
44
50
|
export * from './providers/index.js';
|
|
45
51
|
export * from './managers/index.js';
|
package/src/internal.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { __INTERNAL__ } from '@data-client/core';
|
|
2
|
+
|
|
3
|
+
export {
|
|
4
|
+
createReducer,
|
|
5
|
+
applyManager,
|
|
6
|
+
initManager,
|
|
7
|
+
actions,
|
|
8
|
+
} from '@data-client/core';
|
|
9
|
+
|
|
10
|
+
export const initialState = __INTERNAL__.initialState;
|
|
11
|
+
export const INVALID = __INTERNAL__.INVALID;
|
|
12
|
+
export const MemoCache = __INTERNAL__.MemoCache;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { Controller } from '@data-client/core';
|
|
2
2
|
import type { App } from 'vue';
|
|
3
3
|
|
|
4
|
-
import { createDataClient
|
|
4
|
+
import { createDataClient } from './createDataClient.js';
|
|
5
|
+
import type { ProvideOptions } from './createDataClient.js';
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* Vue 3 Plugin for Reactive Data Client
|
|
@@ -32,14 +33,27 @@ export const DataClientPlugin = {
|
|
|
32
33
|
|
|
33
34
|
provider.start();
|
|
34
35
|
|
|
35
|
-
|
|
36
|
+
const stop = () => {
|
|
36
37
|
/* istanbul ignore if */
|
|
37
38
|
if (process.env.NODE_ENV === 'development') {
|
|
38
39
|
console.info('Stopping DataClientPlugin');
|
|
39
40
|
}
|
|
40
41
|
|
|
41
42
|
provider.stop();
|
|
42
|
-
}
|
|
43
|
+
};
|
|
44
|
+
// app.onUnmount() was added in Vue 3.5
|
|
45
|
+
if (typeof app.onUnmount === 'function') {
|
|
46
|
+
app.onUnmount(stop);
|
|
47
|
+
} else {
|
|
48
|
+
const unmount = app.unmount;
|
|
49
|
+
app.unmount = function (this: App) {
|
|
50
|
+
try {
|
|
51
|
+
stop();
|
|
52
|
+
} finally {
|
|
53
|
+
unmount.call(this);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
}
|
|
43
57
|
|
|
44
58
|
return provider;
|
|
45
59
|
},
|
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
afterEach,
|
|
15
15
|
} from '@jest/globals';
|
|
16
16
|
import { mount } from '@vue/test-utils';
|
|
17
|
-
import { defineComponent, h, inject } from 'vue';
|
|
17
|
+
import { createApp, defineComponent, h, inject } from 'vue';
|
|
18
18
|
|
|
19
19
|
import { ControllerKey, StateKey } from '../../context';
|
|
20
20
|
import { DataClientPlugin } from '../DataClientPlugin';
|
|
@@ -253,6 +253,25 @@ describe('DataClientPlugin', () => {
|
|
|
253
253
|
expect(stopSpy).toHaveBeenCalledTimes(1);
|
|
254
254
|
});
|
|
255
255
|
|
|
256
|
+
it('should stop on app unmount without app.onUnmount (Vue < 3.5)', () => {
|
|
257
|
+
const app = createApp(
|
|
258
|
+
defineComponent({ setup: () => () => h('div', 'test') }),
|
|
259
|
+
);
|
|
260
|
+
// Vue < 3.5 has no app.onUnmount()
|
|
261
|
+
(app as any).onUnmount = undefined;
|
|
262
|
+
|
|
263
|
+
const provider = DataClientPlugin.install(app);
|
|
264
|
+
const stopSpy = jest.spyOn(provider, 'stop');
|
|
265
|
+
|
|
266
|
+
const el = document.createElement('div');
|
|
267
|
+
app.mount(el);
|
|
268
|
+
expect(stopSpy).not.toHaveBeenCalled();
|
|
269
|
+
|
|
270
|
+
app.unmount();
|
|
271
|
+
expect(stopSpy).toHaveBeenCalledTimes(1);
|
|
272
|
+
expect(el.innerHTML).toBe('');
|
|
273
|
+
});
|
|
274
|
+
|
|
256
275
|
it('should work with multiple components', () => {
|
|
257
276
|
const ComponentA = defineComponent({
|
|
258
277
|
name: 'ComponentA',
|
|
@@ -589,4 +608,24 @@ describe('DataClientPlugin', () => {
|
|
|
589
608
|
|
|
590
609
|
wrapper.unmount();
|
|
591
610
|
});
|
|
611
|
+
|
|
612
|
+
it('should accept a Controller subclass with its own members in app.use()', () => {
|
|
613
|
+
class MyController extends Controller {
|
|
614
|
+
doSomething = () => 'hi';
|
|
615
|
+
}
|
|
616
|
+
let controller: Controller | undefined;
|
|
617
|
+
const app = createApp({
|
|
618
|
+
setup() {
|
|
619
|
+
controller = inject(ControllerKey);
|
|
620
|
+
return () => null;
|
|
621
|
+
},
|
|
622
|
+
});
|
|
623
|
+
app.use(DataClientPlugin, { Controller: MyController });
|
|
624
|
+
app.mount(document.createElement('div'));
|
|
625
|
+
|
|
626
|
+
expect(controller).toBeInstanceOf(MyController);
|
|
627
|
+
expect((controller as MyController).doSomething()).toBe('hi');
|
|
628
|
+
|
|
629
|
+
app.unmount();
|
|
630
|
+
});
|
|
592
631
|
});
|
|
@@ -4,21 +4,26 @@ import {
|
|
|
4
4
|
applyManager,
|
|
5
5
|
initManager,
|
|
6
6
|
createReducer,
|
|
7
|
+
GCPolicy,
|
|
7
8
|
} from '@data-client/core';
|
|
8
9
|
import type { State, Manager, GCInterface } from '@data-client/core';
|
|
9
|
-
import { provide, shallowRef
|
|
10
|
+
import { provide, shallowRef } from 'vue';
|
|
11
|
+
import type { ShallowRef, App } from 'vue';
|
|
10
12
|
|
|
11
13
|
import { ControllerKey, StateKey } from '../context.js';
|
|
12
14
|
import { getDefaultManagers } from './getDefaultManagers.js';
|
|
13
15
|
|
|
16
|
+
/** Options for `app.use(DataClientPlugin, options)` */
|
|
14
17
|
export interface ProvideOptions {
|
|
15
18
|
managers?: Manager[];
|
|
16
19
|
initialState?: State<unknown>;
|
|
17
|
-
Controller?:
|
|
20
|
+
Controller?: new (props: { gcPolicy: GCInterface }) => DataController;
|
|
18
21
|
gcPolicy?: GCInterface;
|
|
22
|
+
/** @internal Set by DataClientPlugin */
|
|
19
23
|
app?: App;
|
|
20
24
|
}
|
|
21
25
|
|
|
26
|
+
/** @deprecated Internal to DataClientPlugin; will stop being exported */
|
|
22
27
|
export interface ProvidedDataClient {
|
|
23
28
|
controller: InstanceType<typeof DataController>;
|
|
24
29
|
/** Optimistic overlay state ref provided to consumers */
|
|
@@ -30,13 +35,18 @@ export interface ProvidedDataClient {
|
|
|
30
35
|
}
|
|
31
36
|
|
|
32
37
|
/**
|
|
33
|
-
* Core provider logic
|
|
34
|
-
*
|
|
38
|
+
* Core provider logic used by DataClientPlugin.
|
|
39
|
+
*
|
|
40
|
+
* @deprecated Internal to DataClientPlugin; will stop being exported. Use DataClientPlugin instead.
|
|
35
41
|
*/
|
|
36
42
|
export function createDataClient(
|
|
37
43
|
options: ProvideOptions = {},
|
|
38
44
|
): ProvidedDataClient {
|
|
39
|
-
const {
|
|
45
|
+
const {
|
|
46
|
+
Controller = DataController,
|
|
47
|
+
gcPolicy = new GCPolicy(),
|
|
48
|
+
app,
|
|
49
|
+
} = options;
|
|
40
50
|
|
|
41
51
|
// stable singletons for this provider scope
|
|
42
52
|
const controller = new Controller({ gcPolicy });
|
|
@@ -70,13 +70,9 @@ function constructManager<M extends { new (...args: any): Manager }>(
|
|
|
70
70
|
export type GetManagersOptions = {
|
|
71
71
|
devToolsManager?: DevToolsManager | DevToolsConfig | null;
|
|
72
72
|
networkManager?:
|
|
73
|
-
| NetworkManager
|
|
74
|
-
| ConstructorArgs<typeof NetworkManager>
|
|
75
|
-
| null;
|
|
73
|
+
NetworkManager | ConstructorArgs<typeof NetworkManager> | null;
|
|
76
74
|
subscriptionManager?:
|
|
77
|
-
| SubscriptionManager
|
|
78
|
-
| ConstructorArgs<typeof SubscriptionManager>
|
|
79
|
-
| null;
|
|
75
|
+
SubscriptionManager | ConstructorArgs<typeof SubscriptionManager> | null;
|
|
80
76
|
};
|
|
81
77
|
|
|
82
78
|
export type ConstructorArgs<T extends { new (...args: any): any }> =
|
package/src/providers/index.ts
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
1
|
export { getDefaultManagers } from './getDefaultManagers.js';
|
|
2
|
-
export {
|
|
3
|
-
|
|
4
|
-
type ProvideOptions,
|
|
5
|
-
type ProvidedDataClient,
|
|
6
|
-
} from './createDataClient.js';
|
|
2
|
+
export { createDataClient } from './createDataClient.js';
|
|
3
|
+
export type { ProvideOptions, ProvidedDataClient } from './createDataClient.js';
|
|
7
4
|
export { DataClientPlugin } from './DataClientPlugin.js';
|
package/src/test/MockPlugin.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Controller } from '@data-client/core';
|
|
2
|
-
import { MockController
|
|
2
|
+
import { MockController } from '@data-client/core/mock';
|
|
3
|
+
import type { MockProps } from '@data-client/core/mock';
|
|
3
4
|
import type { App } from 'vue';
|
|
4
5
|
|
|
5
6
|
import { ControllerKey } from '../context.js';
|
|
@@ -39,8 +40,7 @@ export const MockPlugin = {
|
|
|
39
40
|
|
|
40
41
|
// Get the controller from the app's global properties (set by DataClientPlugin)
|
|
41
42
|
const originalController = app.config.globalProperties.$dataClient as
|
|
42
|
-
|
|
|
43
|
-
| undefined;
|
|
43
|
+
Controller | undefined;
|
|
44
44
|
|
|
45
45
|
if (!originalController) {
|
|
46
46
|
/* istanbul ignore else */
|
package/src/test/README.md
CHANGED
|
@@ -1,306 +1,12 @@
|
|
|
1
1
|
# Vue Data Client Testing Utilities
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
### Basic Component Testing
|
|
14
|
-
|
|
15
|
-
```typescript
|
|
16
|
-
import { mountDataClient } 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 } = mountDataClient(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 `mountDataClient`. Your components will automatically suspend and show a fallback while data is loading:
|
|
46
|
-
|
|
47
|
-
```typescript
|
|
48
|
-
import { mountDataClient } 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 } = mountDataClient(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 { renderDataCompose } from '@data-client/vue/test';
|
|
77
|
-
import { reactive } from 'vue';
|
|
78
|
-
|
|
79
|
-
const useMyComposable = (props: { id: number }) => {
|
|
80
|
-
const data = useSuspense(MyResource.get, { id: props.id });
|
|
81
|
-
return { data, isLoading: false };
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
const props = reactive({ id: 1 });
|
|
85
|
-
const { result, controller, cleanup, waitForNextUpdate } = renderDataCompose(useMyComposable, {
|
|
86
|
-
props,
|
|
87
|
-
initialFixtures: [
|
|
88
|
-
{
|
|
89
|
-
endpoint: MyResource.get,
|
|
90
|
-
args: [{ id: 1 }],
|
|
91
|
-
response: { id: 1, name: 'Test' },
|
|
92
|
-
},
|
|
93
|
-
],
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
// Initially suspended
|
|
97
|
-
expect(result.current).toBeUndefined();
|
|
98
|
-
|
|
99
|
-
// Wait for the composable to resolve
|
|
100
|
-
await waitForNextUpdate();
|
|
101
|
-
|
|
102
|
-
// Now should have the actual data (useSuspense returns a Promise that resolves to ComputedRef)
|
|
103
|
-
expect(result.current).toBeDefined();
|
|
104
|
-
expect(result.current).toBeInstanceOf(Promise);
|
|
105
|
-
|
|
106
|
-
// Access the actual data
|
|
107
|
-
const dataRef = await result.current;
|
|
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
|
-
|
|
166
|
-
cleanup();
|
|
167
|
-
```
|
|
168
|
-
|
|
169
|
-
### Using Resolver Fixtures
|
|
170
|
-
|
|
171
|
-
```typescript
|
|
172
|
-
const { wrapper, controller, cleanup } = mountDataClient(TestComponent, {
|
|
173
|
-
resolverFixtures: [
|
|
174
|
-
{
|
|
175
|
-
endpoint: MyResource.get,
|
|
176
|
-
response: (request) => {
|
|
177
|
-
return {
|
|
178
|
-
id: request.args[0].id,
|
|
179
|
-
name: `Dynamic ${request.args[0].id}`,
|
|
180
|
-
};
|
|
181
|
-
},
|
|
182
|
-
},
|
|
183
|
-
],
|
|
184
|
-
});
|
|
185
|
-
|
|
186
|
-
// Test dynamic responses
|
|
187
|
-
const result = await controller.fetch(MyResource.get, { id: 123 });
|
|
188
|
-
expect(result.name).toBe('Dynamic 123');
|
|
189
|
-
```
|
|
190
|
-
|
|
191
|
-
## API Reference
|
|
192
|
-
|
|
193
|
-
### `mountDataClient(component, options)`
|
|
194
|
-
|
|
195
|
-
Renders a Vue component with DataClient provider for testing.
|
|
196
|
-
|
|
197
|
-
**Parameters:**
|
|
198
|
-
- `component`: Vue component to render
|
|
199
|
-
- `options`: Configuration options
|
|
200
|
-
|
|
201
|
-
**Returns:**
|
|
202
|
-
- `wrapper`: Vue Test Utils wrapper
|
|
203
|
-
- `controller`: DataClient controller instance
|
|
204
|
-
- `app`: Vue app instance
|
|
205
|
-
- `cleanup`: Function to clean up resources
|
|
206
|
-
- `allSettled`: Function to wait for all pending promises
|
|
207
|
-
|
|
208
|
-
### `renderDataCompose(composable, options)`
|
|
209
|
-
|
|
210
|
-
Renders a Vue composable with DataClient provider for testing.
|
|
211
|
-
|
|
212
|
-
**Parameters:**
|
|
213
|
-
- `composable`: Vue composable function
|
|
214
|
-
- `options`: Configuration options
|
|
215
|
-
|
|
216
|
-
**Returns:**
|
|
217
|
-
- `result`: Object with `current` property containing the composable's return value (`undefined` when suspended, Promise when resolved)
|
|
218
|
-
- `wrapper`: Vue Test Utils wrapper
|
|
219
|
-
- `controller`: DataClient controller instance
|
|
220
|
-
- `cleanup`: Function to clean up resources
|
|
221
|
-
- `allSettled`: Function to wait for all pending promises
|
|
222
|
-
- `waitForNextUpdate`: Function to wait for the composable to resolve from suspended state
|
|
223
|
-
|
|
224
|
-
### Options
|
|
225
|
-
|
|
226
|
-
```typescript
|
|
227
|
-
interface RenderDataClientOptions<P = any> {
|
|
228
|
-
props?: Reactive<P>; // Reactive props ref to pass to component
|
|
229
|
-
initialFixtures?: readonly Fixture[]; // Initial data fixtures
|
|
230
|
-
resolverFixtures?: readonly (Fixture | Interceptor)[]; // Dynamic response fixtures
|
|
231
|
-
getInitialInterceptorData?: () => any; // Initial data for interceptors
|
|
232
|
-
managers?: Manager[]; // Custom managers
|
|
233
|
-
initialState?: State<unknown>; // Custom initial state
|
|
234
|
-
gcPolicy?: GCInterface; // Custom garbage collection policy
|
|
235
|
-
wrapper?: any; // Custom wrapper component
|
|
236
|
-
}
|
|
237
|
-
```
|
|
238
|
-
|
|
239
|
-
### Fixture Types
|
|
240
|
-
|
|
241
|
-
```typescript
|
|
242
|
-
interface Fixture {
|
|
243
|
-
endpoint: FixtureEndpoint;
|
|
244
|
-
args: any[];
|
|
245
|
-
response: any;
|
|
246
|
-
error?: boolean;
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
interface Interceptor {
|
|
250
|
-
endpoint: FixtureEndpoint;
|
|
251
|
-
response: (request: {
|
|
252
|
-
body?: any;
|
|
253
|
-
headers?: Record<string, string>;
|
|
254
|
-
url: string;
|
|
255
|
-
method: string;
|
|
256
|
-
args: any[];
|
|
257
|
-
}) => any | Promise<any>;
|
|
258
|
-
error?: boolean;
|
|
259
|
-
}
|
|
260
|
-
```
|
|
261
|
-
|
|
262
|
-
## Best Practices
|
|
263
|
-
|
|
264
|
-
1. **Always call cleanup()** after your tests to prevent memory leaks
|
|
265
|
-
2. **Use fixtures** instead of mocking network requests when possible
|
|
266
|
-
3. **Test both success and error cases** using the `error` property in fixtures
|
|
267
|
-
4. **Use resolver fixtures** for dynamic responses based on request parameters
|
|
268
|
-
5. **Leverage the controller** for testing mutations and state updates
|
|
269
|
-
6. **Suspense is automatic** - no need to manually wrap components in Suspense
|
|
270
|
-
7. **Use reactive props** - Pass a `reactive` in the `props` option and set its members to change component props
|
|
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.
|
|
273
|
-
|
|
274
|
-
## Migration from Manual Setup
|
|
275
|
-
|
|
276
|
-
If you were previously setting up DataClient manually in tests:
|
|
277
|
-
|
|
278
|
-
```typescript
|
|
279
|
-
// Before
|
|
280
|
-
const wrapper = mount(MyComponent, {
|
|
281
|
-
global: {
|
|
282
|
-
plugins: [
|
|
283
|
-
[
|
|
284
|
-
DataClientPlugin,
|
|
285
|
-
{
|
|
286
|
-
managers: [new NetworkManager()],
|
|
287
|
-
initialState: mockState,
|
|
288
|
-
},
|
|
289
|
-
],
|
|
290
|
-
],
|
|
291
|
-
},
|
|
292
|
-
});
|
|
293
|
-
|
|
294
|
-
// After
|
|
295
|
-
const { wrapper, controller, cleanup } = mountDataClient(MyComponent, {
|
|
296
|
-
initialFixtures: [
|
|
297
|
-
{
|
|
298
|
-
endpoint: MyResource.get,
|
|
299
|
-
args: [{ id: 1 }],
|
|
300
|
-
response: { id: 1, name: 'Test' },
|
|
301
|
-
},
|
|
302
|
-
],
|
|
303
|
-
});
|
|
304
|
-
```
|
|
305
|
-
|
|
306
|
-
This approach is more declarative, easier to maintain, and provides better test isolation.
|
|
3
|
+
`@data-client/vue/test` provides testing utilities for Vue applications using `@data-client/vue`,
|
|
4
|
+
similar to `@data-client/test` for React.
|
|
5
|
+
|
|
6
|
+
- `mountDataClient()` mounts a component with `DataClientPlugin`, `<Suspense>` and fixtures:
|
|
7
|
+
[Unit testing components](https://dataclient.io/vue/guides/unit-testing-components)
|
|
8
|
+
- `renderDataCompose()` runs a composable the same way:
|
|
9
|
+
[Unit testing composables](https://dataclient.io/vue/guides/unit-testing-composables)
|
|
10
|
+
- `mockInitialState()` builds store state from fixtures:
|
|
11
|
+
[mockInitialState](https://dataclient.io/vue/api/mockInitialState)
|
|
12
|
+
- Fixture and interceptor formats: [Fixtures](https://dataclient.io/vue/api/Fixtures)
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { NetworkManager } from '@data-client/core';
|
|
1
2
|
import { defineComponent, h, computed, watch, reactive, nextTick } from 'vue';
|
|
2
3
|
|
|
3
4
|
import { CoolerArticleResource } from '../../../../../__tests__/new';
|
|
@@ -158,6 +159,43 @@ describe('mountDataClient', () => {
|
|
|
158
159
|
|
|
159
160
|
cleanup();
|
|
160
161
|
});
|
|
162
|
+
|
|
163
|
+
it('allSettled() waits on a NetworkManager passed in managers', async () => {
|
|
164
|
+
const TestComponent = defineComponent({
|
|
165
|
+
name: 'TestComponent',
|
|
166
|
+
setup() {
|
|
167
|
+
return () => h('div', 'Test');
|
|
168
|
+
},
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
const { allSettled, controller, cleanup } = mountDataClient(
|
|
172
|
+
TestComponent,
|
|
173
|
+
{
|
|
174
|
+
managers: [new NetworkManager()],
|
|
175
|
+
resolverFixtures: [
|
|
176
|
+
{
|
|
177
|
+
endpoint: CoolerArticleResource.get,
|
|
178
|
+
response: ({ id }: { id: number }) => ({ ...payload, id }),
|
|
179
|
+
delay: 50,
|
|
180
|
+
},
|
|
181
|
+
],
|
|
182
|
+
},
|
|
183
|
+
);
|
|
184
|
+
|
|
185
|
+
controller.fetch(CoolerArticleResource.get, { id: payload.id });
|
|
186
|
+
const results = await allSettled();
|
|
187
|
+
|
|
188
|
+
expect(results).toHaveLength(1);
|
|
189
|
+
expect(
|
|
190
|
+
controller.get(
|
|
191
|
+
CoolerArticleResource.get.schema,
|
|
192
|
+
{ id: payload.id },
|
|
193
|
+
controller.getState(),
|
|
194
|
+
)?.title,
|
|
195
|
+
).toBe(payload.title);
|
|
196
|
+
|
|
197
|
+
cleanup();
|
|
198
|
+
});
|
|
161
199
|
});
|
|
162
200
|
|
|
163
201
|
describe('reactive prop changes', () => {
|
package/src/test/index.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
export { MockController } from '@data-client/core/mock';
|
|
2
2
|
export { mountDataClient } from './mountDataClient.js';
|
|
3
3
|
export { renderDataCompose } from './renderDataCompose.js';
|
|
4
|
-
export { MockPlugin
|
|
4
|
+
export { MockPlugin } from './MockPlugin.js';
|
|
5
|
+
export type { MockPluginOptions } from './MockPlugin.js';
|
|
5
6
|
export type {
|
|
6
7
|
RenderDataClientOptions,
|
|
7
8
|
RenderDataClientResult,
|