@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
|
@@ -6,14 +6,16 @@ import type {
|
|
|
6
6
|
FetchFunction,
|
|
7
7
|
DenormalizeNullable,
|
|
8
8
|
} from '@data-client/core';
|
|
9
|
-
import { computed, watch, ref } from 'vue';
|
|
9
|
+
import { computed, watch, ref, unref } from 'vue';
|
|
10
10
|
|
|
11
11
|
import { useController, injectState } from '../context.js';
|
|
12
|
+
import type {
|
|
13
|
+
MaybeRefsOrGetters,
|
|
14
|
+
MaybeRefsOrGettersNullable,
|
|
15
|
+
} from '../types.js';
|
|
12
16
|
|
|
13
17
|
/**
|
|
14
18
|
* Fetch an Endpoint if it is not in cache or stale.
|
|
15
|
-
* Non-suspense; returns the fetch Promise when a request is issued, otherwise undefined.
|
|
16
|
-
* Mirrors React useFetch semantics.
|
|
17
19
|
* @see https://dataclient.io/docs/api/useFetch
|
|
18
20
|
*/
|
|
19
21
|
export default function useFetch<
|
|
@@ -24,7 +26,7 @@ export default function useFetch<
|
|
|
24
26
|
>,
|
|
25
27
|
>(
|
|
26
28
|
endpoint: E,
|
|
27
|
-
...args:
|
|
29
|
+
...args: MaybeRefsOrGetters<Parameters<E>>
|
|
28
30
|
): E['schema'] extends undefined | null ? ReturnType<E>
|
|
29
31
|
: Promise<Denormalize<E['schema']>>;
|
|
30
32
|
|
|
@@ -36,7 +38,7 @@ export default function useFetch<
|
|
|
36
38
|
>,
|
|
37
39
|
>(
|
|
38
40
|
endpoint: E,
|
|
39
|
-
...args:
|
|
41
|
+
...args: MaybeRefsOrGettersNullable<Parameters<E>> | readonly [null]
|
|
40
42
|
): E['schema'] extends undefined | null ? ReturnType<E> | undefined
|
|
41
43
|
: Promise<DenormalizeNullable<E['schema']>> | undefined;
|
|
42
44
|
|
|
@@ -44,27 +46,31 @@ export default function useFetch(endpoint: any, ...args: any[]): any {
|
|
|
44
46
|
const stateRef = injectState();
|
|
45
47
|
const controller = useController();
|
|
46
48
|
|
|
49
|
+
// Track top-level reactive args (Refs are unwrapped). This allows props/refs to trigger updates.
|
|
50
|
+
const resolvedArgs = computed(() => args.map(a => unref(a as any)) as any);
|
|
51
|
+
|
|
47
52
|
// Compute a key that changes when args change (including reactive props)
|
|
48
53
|
const argsKey = computed(() =>
|
|
49
|
-
|
|
54
|
+
resolvedArgs.value[0] !== null ? endpoint.key(...resolvedArgs.value) : '',
|
|
50
55
|
);
|
|
51
56
|
|
|
52
57
|
// Compute response meta reactively so we can respond to store updates
|
|
53
58
|
const responseMeta = computed(() => {
|
|
54
|
-
return
|
|
55
|
-
|
|
56
|
-
|
|
59
|
+
return controller.getResponseMeta(
|
|
60
|
+
endpoint,
|
|
61
|
+
...resolvedArgs.value,
|
|
62
|
+
stateRef.value,
|
|
63
|
+
);
|
|
57
64
|
});
|
|
58
65
|
|
|
59
66
|
const lastPromise = ref<Promise<any> | undefined>(undefined);
|
|
60
67
|
|
|
61
68
|
const maybeFetch = () => {
|
|
62
|
-
if (
|
|
69
|
+
if (!argsKey.value) return;
|
|
63
70
|
const meta = responseMeta.value;
|
|
64
|
-
if (!meta) return;
|
|
65
71
|
const forceFetch = meta.expiryStatus === ExpiryStatus.Invalid;
|
|
66
72
|
if (Date.now() <= meta.expiresAt && !forceFetch) return;
|
|
67
|
-
lastPromise.value = controller.fetch(endpoint, ...
|
|
73
|
+
lastPromise.value = controller.fetch(endpoint, ...resolvedArgs.value);
|
|
68
74
|
};
|
|
69
75
|
|
|
70
76
|
// Trigger on initial call
|
|
@@ -74,9 +80,12 @@ export default function useFetch(endpoint: any, ...args: any[]): any {
|
|
|
74
80
|
watch(
|
|
75
81
|
() => {
|
|
76
82
|
const m = responseMeta.value;
|
|
77
|
-
return
|
|
78
|
-
|
|
79
|
-
|
|
83
|
+
return [
|
|
84
|
+
m.expiresAt,
|
|
85
|
+
m.expiryStatus,
|
|
86
|
+
stateRef.value.lastReset,
|
|
87
|
+
argsKey.value,
|
|
88
|
+
];
|
|
80
89
|
},
|
|
81
90
|
() => {
|
|
82
91
|
maybeFetch();
|
package/src/consumers/useLive.ts
CHANGED
|
@@ -10,6 +10,10 @@ import type { DeepReadonly, ComputedRef } from 'vue';
|
|
|
10
10
|
|
|
11
11
|
import useSubscription from './useSubscription.js';
|
|
12
12
|
import useSuspense from './useSuspense.js';
|
|
13
|
+
import type {
|
|
14
|
+
MaybeRefsOrGetters,
|
|
15
|
+
MaybeRefsOrGettersNullable,
|
|
16
|
+
} from '../types.js';
|
|
13
17
|
|
|
14
18
|
/**
|
|
15
19
|
* Ensure an endpoint is available. Keeps it fresh once it is.
|
|
@@ -27,7 +31,7 @@ export default function useLive<
|
|
|
27
31
|
>,
|
|
28
32
|
>(
|
|
29
33
|
endpoint: E,
|
|
30
|
-
...args:
|
|
34
|
+
...args: MaybeRefsOrGetters<Parameters<E>>
|
|
31
35
|
): Promise<
|
|
32
36
|
DeepReadonly<
|
|
33
37
|
ComputedRef<
|
|
@@ -45,7 +49,7 @@ export default function useLive<
|
|
|
45
49
|
>,
|
|
46
50
|
>(
|
|
47
51
|
endpoint: E,
|
|
48
|
-
...args:
|
|
52
|
+
...args: MaybeRefsOrGettersNullable<Parameters<E>> | readonly [null]
|
|
49
53
|
): Promise<
|
|
50
54
|
DeepReadonly<
|
|
51
55
|
ComputedRef<
|
|
@@ -1,32 +1,39 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
DenormalizeNullable,
|
|
3
|
-
NI,
|
|
4
3
|
Queryable,
|
|
5
4
|
SchemaArgs,
|
|
6
5
|
} from '@data-client/core';
|
|
7
|
-
import { computed, watch, type ComputedRef } from 'vue';
|
|
6
|
+
import { computed, unref, watch, type ComputedRef } from 'vue';
|
|
8
7
|
|
|
9
8
|
import { useController, injectState } from '../context.js';
|
|
9
|
+
import type { MaybeRefsOrGetters } from '../types.js';
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
|
-
* Query the store
|
|
12
|
+
* Query the store.
|
|
13
13
|
*
|
|
14
|
-
*
|
|
15
|
-
* the result is not found or invalid.
|
|
16
|
-
* Mirrors React's useQuery semantics using Vue reactivity.
|
|
14
|
+
* `useQuery` results are globally memoized.
|
|
17
15
|
* @see https://dataclient.io/docs/api/useQuery
|
|
18
16
|
*/
|
|
19
17
|
export default function useQuery<S extends Queryable>(
|
|
20
18
|
schema: S,
|
|
21
|
-
...args:
|
|
22
|
-
): ComputedRef<DenormalizeNullable<S> | undefined
|
|
19
|
+
...args: MaybeRefsOrGetters<SchemaArgs<S>>
|
|
20
|
+
): ComputedRef<DenormalizeNullable<S> | undefined>;
|
|
21
|
+
|
|
22
|
+
export default function useQuery(schema: any, ...args: any[]): any {
|
|
23
23
|
const stateRef = injectState();
|
|
24
24
|
const controller = useController();
|
|
25
25
|
|
|
26
|
+
// Track top-level reactive args (Refs are unwrapped). This allows props/refs to trigger updates.
|
|
27
|
+
const resolvedArgs = computed(() => args.map(a => unref(a as any)) as any);
|
|
28
|
+
|
|
26
29
|
// Compute query meta based on state and args. This mirrors React's memoization
|
|
27
30
|
// that keys off state.entities/indexes and args.
|
|
28
31
|
const queryMeta = computed(() =>
|
|
29
|
-
controller.getQueryMeta(
|
|
32
|
+
controller.getQueryMeta(
|
|
33
|
+
schema,
|
|
34
|
+
...resolvedArgs.value,
|
|
35
|
+
stateRef.value as any,
|
|
36
|
+
),
|
|
30
37
|
);
|
|
31
38
|
|
|
32
39
|
// Maintain GC refcounts on data mount/changes
|
|
@@ -6,6 +6,7 @@ import type {
|
|
|
6
6
|
import { computed, unref, watch } from 'vue';
|
|
7
7
|
|
|
8
8
|
import { useController } from '../context.js';
|
|
9
|
+
import type { MaybeRefsOrGettersNullable } from '../types.js';
|
|
9
10
|
|
|
10
11
|
/**
|
|
11
12
|
* Keeps a resource fresh by subscribing to updates.
|
|
@@ -18,7 +19,10 @@ export default function useSubscription<
|
|
|
18
19
|
Schema | undefined,
|
|
19
20
|
undefined | false
|
|
20
21
|
>,
|
|
21
|
-
>(
|
|
22
|
+
>(
|
|
23
|
+
endpoint: E,
|
|
24
|
+
...args: MaybeRefsOrGettersNullable<Parameters<E>> | readonly [null]
|
|
25
|
+
) {
|
|
22
26
|
const controller = useController();
|
|
23
27
|
|
|
24
28
|
// Track top-level reactive args (Refs are unwrapped). This allows props/refs to trigger resubscribe.
|
|
@@ -9,6 +9,7 @@ import type {
|
|
|
9
9
|
} from '@data-client/core';
|
|
10
10
|
import {
|
|
11
11
|
computed,
|
|
12
|
+
unref,
|
|
12
13
|
watch,
|
|
13
14
|
readonly,
|
|
14
15
|
type DeepReadonly,
|
|
@@ -16,7 +17,19 @@ import {
|
|
|
16
17
|
} from 'vue';
|
|
17
18
|
|
|
18
19
|
import { useController, injectState } from '../context.js';
|
|
20
|
+
import type {
|
|
21
|
+
MaybeRefsOrGetters,
|
|
22
|
+
MaybeRefsOrGettersNullable,
|
|
23
|
+
} from '../types.js';
|
|
19
24
|
|
|
25
|
+
/**
|
|
26
|
+
* Ensure an endpoint is available.
|
|
27
|
+
* Suspends until it is.
|
|
28
|
+
*
|
|
29
|
+
* @see https://dataclient.io/docs/api/useSuspense
|
|
30
|
+
* @throws {Promise} If data is not yet available.
|
|
31
|
+
* @throws {NetworkError} If fetch fails.
|
|
32
|
+
*/
|
|
20
33
|
export default function useSuspense<
|
|
21
34
|
E extends EndpointInterface<
|
|
22
35
|
FetchFunction,
|
|
@@ -25,7 +38,7 @@ export default function useSuspense<
|
|
|
25
38
|
>,
|
|
26
39
|
>(
|
|
27
40
|
endpoint: E,
|
|
28
|
-
...args:
|
|
41
|
+
...args: MaybeRefsOrGetters<Parameters<E>>
|
|
29
42
|
): Promise<
|
|
30
43
|
DeepReadonly<
|
|
31
44
|
ComputedRef<
|
|
@@ -43,7 +56,7 @@ export default function useSuspense<
|
|
|
43
56
|
>,
|
|
44
57
|
>(
|
|
45
58
|
endpoint: E,
|
|
46
|
-
...args:
|
|
59
|
+
...args: MaybeRefsOrGettersNullable<Parameters<E>> | readonly [null]
|
|
47
60
|
): Promise<
|
|
48
61
|
DeepReadonly<
|
|
49
62
|
ComputedRef<
|
|
@@ -60,38 +73,42 @@ export default async function useSuspense(
|
|
|
60
73
|
const stateRef = injectState();
|
|
61
74
|
const controller = useController();
|
|
62
75
|
|
|
76
|
+
// Track top-level reactive args (Refs are unwrapped). This allows props/refs to trigger updates.
|
|
77
|
+
const resolvedArgs = computed(() => args.map(a => unref(a as any)) as any);
|
|
78
|
+
|
|
63
79
|
// Compute a key that changes when args change (including reactive props)
|
|
64
80
|
const argsKey = computed(() =>
|
|
65
|
-
|
|
81
|
+
resolvedArgs.value[0] !== null ? endpoint.key(...resolvedArgs.value) : '',
|
|
66
82
|
);
|
|
67
83
|
|
|
68
84
|
// Compute response meta reactively so we can respond to store updates
|
|
69
85
|
const responseMeta = computed(() => {
|
|
70
|
-
return
|
|
71
|
-
|
|
72
|
-
|
|
86
|
+
return controller.getResponseMeta(
|
|
87
|
+
endpoint,
|
|
88
|
+
...resolvedArgs.value,
|
|
89
|
+
stateRef.value,
|
|
90
|
+
);
|
|
73
91
|
});
|
|
74
92
|
|
|
75
93
|
const maybeFetch = async () => {
|
|
76
94
|
const currentKey = argsKey.value;
|
|
77
95
|
if (!currentKey) return;
|
|
78
96
|
const meta = responseMeta.value;
|
|
79
|
-
if (!meta) return;
|
|
80
97
|
const forceFetch = meta.expiryStatus === ExpiryStatus.Invalid;
|
|
81
98
|
if (Date.now() <= meta.expiresAt && !forceFetch) return;
|
|
82
|
-
await controller.fetch(endpoint, ...
|
|
99
|
+
await controller.fetch(endpoint, ...resolvedArgs.value);
|
|
83
100
|
};
|
|
84
101
|
|
|
85
|
-
// Trigger on initial call
|
|
86
|
-
await maybeFetch();
|
|
87
|
-
|
|
88
102
|
// Watch for changes to key, expiry, or store state that require refetch
|
|
89
103
|
watch(
|
|
90
104
|
() => {
|
|
91
105
|
const m = responseMeta.value;
|
|
92
|
-
return
|
|
93
|
-
|
|
94
|
-
|
|
106
|
+
return [
|
|
107
|
+
m.expiresAt,
|
|
108
|
+
m.expiryStatus,
|
|
109
|
+
stateRef.value.lastReset,
|
|
110
|
+
argsKey.value,
|
|
111
|
+
];
|
|
95
112
|
},
|
|
96
113
|
() => {
|
|
97
114
|
return maybeFetch();
|
|
@@ -100,14 +117,17 @@ export default async function useSuspense(
|
|
|
100
117
|
|
|
101
118
|
// Maintain GC refcounts on data mount/changes
|
|
102
119
|
watch(
|
|
103
|
-
() => responseMeta.value
|
|
120
|
+
() => responseMeta.value.data,
|
|
104
121
|
(_newVal, _oldVal, onCleanup) => {
|
|
105
|
-
const decrement = responseMeta.value
|
|
122
|
+
const decrement = responseMeta.value.countRef();
|
|
106
123
|
onCleanup(() => decrement?.());
|
|
107
124
|
},
|
|
108
125
|
{ immediate: true },
|
|
109
126
|
);
|
|
110
127
|
|
|
128
|
+
// Trigger on initial call
|
|
129
|
+
await maybeFetch();
|
|
130
|
+
|
|
111
131
|
// Return readonly computed ref - Vue automatically unwraps in templates and reactive contexts
|
|
112
|
-
return readonly(computed(() => responseMeta.value
|
|
132
|
+
return readonly(computed(() => responseMeta.value.data));
|
|
113
133
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Controller } from '@data-client/core';
|
|
1
2
|
import type { App } from 'vue';
|
|
2
3
|
|
|
3
4
|
import { createDataClient, type ProvideOptions } from './createDataClient.js';
|
|
@@ -22,18 +23,32 @@ export const DataClientPlugin = {
|
|
|
22
23
|
install(app: App, options: ProvideOptions = {}) {
|
|
23
24
|
const provider = createDataClient({ ...options, app });
|
|
24
25
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
},
|
|
31
|
-
beforeUnmount() {
|
|
32
|
-
provider.stop();
|
|
33
|
-
},
|
|
34
|
-
});
|
|
26
|
+
app.config.globalProperties.$dataClient = provider.controller;
|
|
27
|
+
|
|
28
|
+
/* istanbul ignore if */
|
|
29
|
+
if (process.env.NODE_ENV === 'development') {
|
|
30
|
+
console.info('Starting DataClientPlugin');
|
|
35
31
|
}
|
|
36
32
|
|
|
33
|
+
provider.start();
|
|
34
|
+
|
|
35
|
+
app.onUnmount(() => {
|
|
36
|
+
/* istanbul ignore if */
|
|
37
|
+
if (process.env.NODE_ENV === 'development') {
|
|
38
|
+
console.info('Stopping DataClientPlugin');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
provider.stop();
|
|
42
|
+
});
|
|
43
|
+
|
|
37
44
|
return provider;
|
|
38
45
|
},
|
|
39
46
|
};
|
|
47
|
+
|
|
48
|
+
// install global property types
|
|
49
|
+
|
|
50
|
+
declare module 'vue' {
|
|
51
|
+
interface ComponentCustomProperties {
|
|
52
|
+
$dataClient: Controller;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -161,7 +161,7 @@ describe('DataClientPlugin', () => {
|
|
|
161
161
|
wrapper.unmount();
|
|
162
162
|
});
|
|
163
163
|
|
|
164
|
-
it('should call start on
|
|
164
|
+
it('should call start ONCE on app install (plugin lifecycle)', () => {
|
|
165
165
|
const startSpy = jest.fn();
|
|
166
166
|
const originalCreateDataClient =
|
|
167
167
|
require('../createDataClient.js').createDataClient;
|
|
@@ -194,13 +194,14 @@ describe('DataClientPlugin', () => {
|
|
|
194
194
|
},
|
|
195
195
|
});
|
|
196
196
|
|
|
197
|
-
|
|
197
|
+
// Start should be called exactly once when plugin is installed
|
|
198
|
+
expect(startSpy).toHaveBeenCalledTimes(1);
|
|
198
199
|
|
|
199
200
|
wrapper.unmount();
|
|
200
201
|
jest.restoreAllMocks();
|
|
201
202
|
});
|
|
202
203
|
|
|
203
|
-
it('should call stop on
|
|
204
|
+
it('should call stop ONCE on app unmount (plugin lifecycle)', () => {
|
|
204
205
|
const stopSpy = jest.fn();
|
|
205
206
|
const originalCreateDataClient =
|
|
206
207
|
require('../createDataClient.js').createDataClient;
|
|
@@ -233,9 +234,13 @@ describe('DataClientPlugin', () => {
|
|
|
233
234
|
},
|
|
234
235
|
});
|
|
235
236
|
|
|
237
|
+
// Stop should not be called while app is running
|
|
236
238
|
expect(stopSpy).not.toHaveBeenCalled();
|
|
239
|
+
|
|
237
240
|
wrapper.unmount();
|
|
238
|
-
|
|
241
|
+
|
|
242
|
+
// Stop should be called exactly once when app unmounts
|
|
243
|
+
expect(stopSpy).toHaveBeenCalledTimes(1);
|
|
239
244
|
|
|
240
245
|
jest.restoreAllMocks();
|
|
241
246
|
});
|
|
@@ -278,6 +283,193 @@ describe('DataClientPlugin', () => {
|
|
|
278
283
|
wrapper.unmount();
|
|
279
284
|
});
|
|
280
285
|
|
|
286
|
+
it('should NOT call start for each component mount - only once for app', () => {
|
|
287
|
+
const startSpy = jest.fn();
|
|
288
|
+
const originalCreateDataClient =
|
|
289
|
+
require('../createDataClient.js').createDataClient;
|
|
290
|
+
|
|
291
|
+
const mockCreateDataClient = jest.fn(options => {
|
|
292
|
+
const provider = originalCreateDataClient(options);
|
|
293
|
+
const originalStart = provider.start;
|
|
294
|
+
provider.start = () => {
|
|
295
|
+
startSpy();
|
|
296
|
+
originalStart();
|
|
297
|
+
};
|
|
298
|
+
return provider;
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
jest
|
|
302
|
+
.spyOn(require('../createDataClient.js'), 'createDataClient')
|
|
303
|
+
.mockImplementation(mockCreateDataClient as any);
|
|
304
|
+
|
|
305
|
+
const ComponentA = defineComponent({
|
|
306
|
+
name: 'ComponentA',
|
|
307
|
+
setup() {
|
|
308
|
+
const controller = inject(ControllerKey);
|
|
309
|
+
return () => h('div', { id: 'a' }, controller ? 'yes' : 'no');
|
|
310
|
+
},
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
const ComponentB = defineComponent({
|
|
314
|
+
name: 'ComponentB',
|
|
315
|
+
setup() {
|
|
316
|
+
const controller = inject(ControllerKey);
|
|
317
|
+
return () => h('div', { id: 'b' }, controller ? 'yes' : 'no');
|
|
318
|
+
},
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
const ComponentC = defineComponent({
|
|
322
|
+
name: 'ComponentC',
|
|
323
|
+
setup() {
|
|
324
|
+
const controller = inject(ControllerKey);
|
|
325
|
+
return () => h('div', { id: 'c' }, controller ? 'yes' : 'no');
|
|
326
|
+
},
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
const ParentComponent = defineComponent({
|
|
330
|
+
name: 'ParentComponent',
|
|
331
|
+
setup() {
|
|
332
|
+
return () => h('div', [h(ComponentA), h(ComponentB), h(ComponentC)]);
|
|
333
|
+
},
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
const wrapper = mount(ParentComponent, {
|
|
337
|
+
global: {
|
|
338
|
+
plugins: [[DataClientPlugin]],
|
|
339
|
+
},
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
// Verify all components mounted successfully
|
|
343
|
+
expect(wrapper.find('#a').text()).toBe('yes');
|
|
344
|
+
expect(wrapper.find('#b').text()).toBe('yes');
|
|
345
|
+
expect(wrapper.find('#c').text()).toBe('yes');
|
|
346
|
+
|
|
347
|
+
// Start should be called exactly ONCE despite 3 components mounting
|
|
348
|
+
expect(startSpy).toHaveBeenCalledTimes(1);
|
|
349
|
+
|
|
350
|
+
wrapper.unmount();
|
|
351
|
+
jest.restoreAllMocks();
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
it('should NOT call stop for individual component unmount - only on app unmount', () => {
|
|
355
|
+
const stopSpy = jest.fn();
|
|
356
|
+
const originalCreateDataClient =
|
|
357
|
+
require('../createDataClient.js').createDataClient;
|
|
358
|
+
|
|
359
|
+
const mockCreateDataClient = jest.fn(options => {
|
|
360
|
+
const provider = originalCreateDataClient(options);
|
|
361
|
+
const originalStop = provider.stop;
|
|
362
|
+
provider.stop = () => {
|
|
363
|
+
stopSpy();
|
|
364
|
+
originalStop();
|
|
365
|
+
};
|
|
366
|
+
return provider;
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
jest
|
|
370
|
+
.spyOn(require('../createDataClient.js'), 'createDataClient')
|
|
371
|
+
.mockImplementation(mockCreateDataClient as any);
|
|
372
|
+
|
|
373
|
+
const ComponentA = defineComponent({
|
|
374
|
+
name: 'ComponentA',
|
|
375
|
+
setup() {
|
|
376
|
+
const controller = inject(ControllerKey);
|
|
377
|
+
return () => h('div', { id: 'a' }, controller ? 'yes' : 'no');
|
|
378
|
+
},
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
const ComponentB = defineComponent({
|
|
382
|
+
name: 'ComponentB',
|
|
383
|
+
setup() {
|
|
384
|
+
const controller = inject(ControllerKey);
|
|
385
|
+
return () => h('div', { id: 'b' }, controller ? 'yes' : 'no');
|
|
386
|
+
},
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
const ComponentC = defineComponent({
|
|
390
|
+
name: 'ComponentC',
|
|
391
|
+
setup() {
|
|
392
|
+
const controller = inject(ControllerKey);
|
|
393
|
+
return () => h('div', { id: 'c' }, controller ? 'yes' : 'no');
|
|
394
|
+
},
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
const ParentComponent = defineComponent({
|
|
398
|
+
name: 'ParentComponent',
|
|
399
|
+
setup() {
|
|
400
|
+
return () => h('div', [h(ComponentA), h(ComponentB), h(ComponentC)]);
|
|
401
|
+
},
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
const wrapper = mount(ParentComponent, {
|
|
405
|
+
global: {
|
|
406
|
+
plugins: [[DataClientPlugin]],
|
|
407
|
+
},
|
|
408
|
+
});
|
|
409
|
+
|
|
410
|
+
// Verify all components mounted
|
|
411
|
+
expect(wrapper.find('#a').exists()).toBe(true);
|
|
412
|
+
expect(wrapper.find('#b').exists()).toBe(true);
|
|
413
|
+
expect(wrapper.find('#c').exists()).toBe(true);
|
|
414
|
+
|
|
415
|
+
// Stop should NOT be called while components are running
|
|
416
|
+
expect(stopSpy).not.toHaveBeenCalled();
|
|
417
|
+
|
|
418
|
+
wrapper.unmount();
|
|
419
|
+
|
|
420
|
+
// Stop should be called exactly ONCE when app unmounts
|
|
421
|
+
// (not once per component - only once for the app)
|
|
422
|
+
expect(stopSpy).toHaveBeenCalledTimes(1);
|
|
423
|
+
|
|
424
|
+
jest.restoreAllMocks();
|
|
425
|
+
});
|
|
426
|
+
|
|
427
|
+
it('should maintain same controller/state instance across all components', () => {
|
|
428
|
+
const controllers: any[] = [];
|
|
429
|
+
const states: any[] = [];
|
|
430
|
+
|
|
431
|
+
const createTestComponent = (name: string) =>
|
|
432
|
+
defineComponent({
|
|
433
|
+
name,
|
|
434
|
+
setup() {
|
|
435
|
+
const controller = inject(ControllerKey);
|
|
436
|
+
const state = inject(StateKey);
|
|
437
|
+
controllers.push(controller);
|
|
438
|
+
states.push(state);
|
|
439
|
+
return () => h('div', { id: name.toLowerCase() }, 'test');
|
|
440
|
+
},
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
const ComponentA = createTestComponent('ComponentA');
|
|
444
|
+
const ComponentB = createTestComponent('ComponentB');
|
|
445
|
+
const ComponentC = createTestComponent('ComponentC');
|
|
446
|
+
|
|
447
|
+
const ParentComponent = defineComponent({
|
|
448
|
+
name: 'ParentComponent',
|
|
449
|
+
setup() {
|
|
450
|
+
return () => h('div', [h(ComponentA), h(ComponentB), h(ComponentC)]);
|
|
451
|
+
},
|
|
452
|
+
});
|
|
453
|
+
|
|
454
|
+
const wrapper = mount(ParentComponent, {
|
|
455
|
+
global: {
|
|
456
|
+
plugins: [[DataClientPlugin]],
|
|
457
|
+
},
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
// All components should receive the SAME controller instance
|
|
461
|
+
expect(controllers.length).toBe(3);
|
|
462
|
+
expect(controllers[0]).toBe(controllers[1]);
|
|
463
|
+
expect(controllers[1]).toBe(controllers[2]);
|
|
464
|
+
|
|
465
|
+
// All components should receive the SAME state ref
|
|
466
|
+
expect(states.length).toBe(3);
|
|
467
|
+
expect(states[0]).toBe(states[1]);
|
|
468
|
+
expect(states[1]).toBe(states[2]);
|
|
469
|
+
|
|
470
|
+
wrapper.unmount();
|
|
471
|
+
});
|
|
472
|
+
|
|
281
473
|
it('should provide reactive state', async () => {
|
|
282
474
|
const TestComponent = defineComponent({
|
|
283
475
|
name: 'TestComponent',
|
|
@@ -312,6 +504,10 @@ describe('DataClientPlugin', () => {
|
|
|
312
504
|
const app = {
|
|
313
505
|
mixin: jest.fn(),
|
|
314
506
|
provide: jest.fn(),
|
|
507
|
+
config: {
|
|
508
|
+
globalProperties: {},
|
|
509
|
+
},
|
|
510
|
+
onUnmount: jest.fn(),
|
|
315
511
|
} as any;
|
|
316
512
|
|
|
317
513
|
const result = DataClientPlugin.install(app, {});
|
|
@@ -327,6 +523,10 @@ describe('DataClientPlugin', () => {
|
|
|
327
523
|
it('should handle app without mixin method gracefully', () => {
|
|
328
524
|
const app = {
|
|
329
525
|
provide: jest.fn(),
|
|
526
|
+
config: {
|
|
527
|
+
globalProperties: {},
|
|
528
|
+
},
|
|
529
|
+
onUnmount: jest.fn(),
|
|
330
530
|
} as any;
|
|
331
531
|
|
|
332
532
|
expect(() => {
|