@arrai-innovations/reactive-helpers 18.0.0 → 18.0.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/package.json +20 -6
- package/types/config/listCrud.d.ts +153 -0
- package/types/config/listCrud.d.ts.map +1 -0
- package/types/config/objectCrud.d.ts +171 -0
- package/types/config/objectCrud.d.ts.map +1 -0
- package/types/index.d.ts +37 -0
- package/types/index.d.ts.map +1 -0
- package/types/tests/unit/config/listCrud.spec.d.ts +2 -0
- package/types/tests/unit/config/listCrud.spec.d.ts.map +1 -0
- package/types/tests/unit/config/objectCrud.spec.d.ts +2 -0
- package/types/tests/unit/config/objectCrud.spec.d.ts.map +1 -0
- package/types/tests/unit/crudPromise.d.ts +25 -0
- package/types/tests/unit/crudPromise.d.ts.map +1 -0
- package/types/tests/unit/expectHelpers.d.ts +2 -0
- package/types/tests/unit/expectHelpers.d.ts.map +1 -0
- package/types/tests/unit/mockOnUnmounted.d.ts +2 -0
- package/types/tests/unit/mockOnUnmounted.d.ts.map +1 -0
- package/types/tests/unit/poll.d.ts +2 -0
- package/types/tests/unit/poll.d.ts.map +1 -0
- package/types/tests/unit/use/cancellableIntent.spec.d.ts +2 -0
- package/types/tests/unit/use/cancellableIntent.spec.d.ts.map +1 -0
- package/types/tests/unit/use/listCalculated.spec.d.ts +2 -0
- package/types/tests/unit/use/listCalculated.spec.d.ts.map +1 -0
- package/types/tests/unit/use/listFilter.spec.d.ts +2 -0
- package/types/tests/unit/use/listFilter.spec.d.ts.map +1 -0
- package/types/tests/unit/use/listInstance.spec.d.ts +2 -0
- package/types/tests/unit/use/listInstance.spec.d.ts.map +1 -0
- package/types/tests/unit/use/listRelated.spec.d.ts +2 -0
- package/types/tests/unit/use/listRelated.spec.d.ts.map +1 -0
- package/types/tests/unit/use/listSearch.spec.d.ts +2 -0
- package/types/tests/unit/use/listSearch.spec.d.ts.map +1 -0
- package/types/tests/unit/use/listSort.spec.d.ts +2 -0
- package/types/tests/unit/use/listSort.spec.d.ts.map +1 -0
- package/types/tests/unit/use/listSubscription.spec.d.ts +2 -0
- package/types/tests/unit/use/listSubscription.spec.d.ts.map +1 -0
- package/types/tests/unit/use/loadingError.spec.d.ts +2 -0
- package/types/tests/unit/use/loadingError.spec.d.ts.map +1 -0
- package/types/tests/unit/use/objectInstance.spec.d.ts +2 -0
- package/types/tests/unit/use/objectInstance.spec.d.ts.map +1 -0
- package/types/tests/unit/use/objectSubscription.spec.d.ts +2 -0
- package/types/tests/unit/use/objectSubscription.spec.d.ts.map +1 -0
- package/types/tests/unit/use/proxyLoadingError.spec.d.ts +2 -0
- package/types/tests/unit/use/proxyLoadingError.spec.d.ts.map +1 -0
- package/types/tests/unit/use/search.spec.d.ts +2 -0
- package/types/tests/unit/use/search.spec.d.ts.map +1 -0
- package/types/tests/unit/utils/assignReactiveObject.spec.d.ts +2 -0
- package/types/tests/unit/utils/assignReactiveObject.spec.d.ts.map +1 -0
- package/types/tests/unit/utils/classes.spec.d.ts +2 -0
- package/types/tests/unit/utils/classes.spec.d.ts.map +1 -0
- package/types/tests/unit/utils/deleteKey.spec.d.ts +2 -0
- package/types/tests/unit/utils/deleteKey.spec.d.ts.map +1 -0
- package/types/tests/unit/utils/flattenPaths.spec.d.ts +2 -0
- package/types/tests/unit/utils/flattenPaths.spec.d.ts.map +1 -0
- package/types/tests/unit/utils/keyDiff.spec.d.ts +2 -0
- package/types/tests/unit/utils/keyDiff.spec.d.ts.map +1 -0
- package/types/tests/unit/utils/set.spec.d.ts +2 -0
- package/types/tests/unit/utils/set.spec.d.ts.map +1 -0
- package/types/tests/unit/utils/watches.spec.d.ts +2 -0
- package/types/tests/unit/utils/watches.spec.d.ts.map +1 -0
- package/types/use/cancellableIntent.d.ts +129 -0
- package/types/use/cancellableIntent.d.ts.map +1 -0
- package/types/use/combineClasses.d.ts +43 -0
- package/types/use/combineClasses.d.ts.map +1 -0
- package/types/use/list.d.ts +176 -0
- package/types/use/list.d.ts.map +1 -0
- package/types/use/listCalculated.d.ts +192 -0
- package/types/use/listCalculated.d.ts.map +1 -0
- package/types/use/listFilter.d.ts +203 -0
- package/types/use/listFilter.d.ts.map +1 -0
- package/types/use/listInstance.d.ts +357 -0
- package/types/use/listInstance.d.ts.map +1 -0
- package/types/use/listKeys.d.ts +15 -0
- package/types/use/listKeys.d.ts.map +1 -0
- package/types/use/listRelated.d.ts +239 -0
- package/types/use/listRelated.d.ts.map +1 -0
- package/types/use/listSearch.d.ts +298 -0
- package/types/use/listSearch.d.ts.map +1 -0
- package/types/use/listSort.d.ts +235 -0
- package/types/use/listSort.d.ts.map +1 -0
- package/types/use/listSubscription.d.ts +218 -0
- package/types/use/listSubscription.d.ts.map +1 -0
- package/types/use/loadingError.d.ts +81 -0
- package/types/use/loadingError.d.ts.map +1 -0
- package/types/use/object.d.ts +67 -0
- package/types/use/object.d.ts.map +1 -0
- package/types/use/objectCalculated.d.ts +215 -0
- package/types/use/objectCalculated.d.ts.map +1 -0
- package/types/use/objectInstance.d.ts +289 -0
- package/types/use/objectInstance.d.ts.map +1 -0
- package/types/use/objectRelated.d.ts +264 -0
- package/types/use/objectRelated.d.ts.map +1 -0
- package/types/use/objectSubscription.d.ts +249 -0
- package/types/use/objectSubscription.d.ts.map +1 -0
- package/types/use/paginatedListInstance.d.ts +51 -0
- package/types/use/paginatedListInstance.d.ts.map +1 -0
- package/types/use/proxyLoadingError.d.ts +36 -0
- package/types/use/proxyLoadingError.d.ts.map +1 -0
- package/types/use/search.d.ts +144 -0
- package/types/use/search.d.ts.map +1 -0
- package/types/use/watchesRunning.d.ts +63 -0
- package/types/use/watchesRunning.d.ts.map +1 -0
- package/types/utils/assignReactiveObject.d.ts +115 -0
- package/types/utils/assignReactiveObject.d.ts.map +1 -0
- package/types/utils/classes.d.ts +22 -0
- package/types/utils/classes.d.ts.map +1 -0
- package/types/utils/compact.d.ts +4 -0
- package/types/utils/compact.d.ts.map +1 -0
- package/types/utils/deleteKey.d.ts +16 -0
- package/types/utils/deleteKey.d.ts.map +1 -0
- package/types/utils/flattenPaths.d.ts +23 -0
- package/types/utils/flattenPaths.d.ts.map +1 -0
- package/types/utils/getFakePk.d.ts +13 -0
- package/types/utils/getFakePk.d.ts.map +1 -0
- package/types/utils/keepAliveTry.d.ts +15 -0
- package/types/utils/keepAliveTry.d.ts.map +1 -0
- package/types/utils/keyDiff.d.ts +63 -0
- package/types/utils/keyDiff.d.ts.map +1 -0
- package/types/utils/loadingCombine.d.ts +9 -0
- package/types/utils/loadingCombine.d.ts.map +1 -0
- package/types/utils/proxyRunning.d.ts +9 -0
- package/types/utils/proxyRunning.d.ts.map +1 -0
- package/types/utils/relatedCalculatedHelpers.d.ts +5 -0
- package/types/utils/relatedCalculatedHelpers.d.ts.map +1 -0
- package/types/utils/set.d.ts +7 -0
- package/types/utils/set.d.ts.map +1 -0
- package/types/utils/transformWalk.d.ts +2 -0
- package/types/utils/transformWalk.d.ts.map +1 -0
- package/types/utils/watches.d.ts +155 -0
- package/types/utils/watches.d.ts.map +1 -0
- package/use/loadingError.js +1 -1
- package/.circleci/config.yml +0 -88
- package/.commitlintrc.json +0 -25
- package/.editorconfig +0 -5
- package/.husky/commit-msg +0 -3
- package/.husky/pre-commit +0 -3
- package/.idea/codeStyles/codeStyleConfig.xml +0 -5
- package/.idea/encodings.xml +0 -6
- package/.idea/inspectionProfiles/Project_Default.xml +0 -7
- package/.idea/modules.xml +0 -8
- package/.idea/prettier.xml +0 -8
- package/.idea/reactive-helpers.iml +0 -15
- package/.prettierignore +0 -5
- package/.prettierrc.cjs +0 -5
- package/.vscode/extensions.json +0 -3
- package/docs/README.md +0 -45
- package/docs/config/listCrud.md +0 -422
- package/docs/config/objectCrud.md +0 -427
- package/docs/use/cancellableIntent.md +0 -165
- package/docs/use/combineClasses.md +0 -62
- package/docs/use/list.md +0 -361
- package/docs/use/listCalculated.md +0 -794
- package/docs/use/listFilter.md +0 -897
- package/docs/use/listInstance.md +0 -559
- package/docs/use/listKeys.md +0 -91
- package/docs/use/listRelated.md +0 -706
- package/docs/use/listSearch.md +0 -903
- package/docs/use/listSort.md +0 -1171
- package/docs/use/listSubscription.md +0 -393
- package/docs/use/loadingError.md +0 -115
- package/docs/use/object.md +0 -668
- package/docs/use/objectCalculated.md +0 -741
- package/docs/use/objectInstance.md +0 -685
- package/docs/use/objectRelated.md +0 -723
- package/docs/use/objectSubscription.md +0 -538
- package/docs/use/paginatedListInstance.md +0 -155
- package/docs/use/proxyLoadingError.md +0 -51
- package/docs/use/search.md +0 -321
- package/docs/use/watchesRunning.md +0 -84
- package/docs/utils/assignReactiveObject.md +0 -413
- package/docs/utils/classes.md +0 -139
- package/docs/utils/compact.md +0 -67
- package/docs/utils/deleteKey.md +0 -61
- package/docs/utils/flattenPaths.md +0 -53
- package/docs/utils/getFakePk.md +0 -39
- package/docs/utils/keepAliveTry.md +0 -59
- package/docs/utils/keyDiff.md +0 -117
- package/docs/utils/loadingCombine.md +0 -30
- package/docs/utils/proxyRunning.md +0 -39
- package/docs/utils/relatedCalculatedHelpers.md +0 -93
- package/docs/utils/set.md +0 -223
- package/docs/utils/transformWalk.md +0 -68
- package/docs/utils/watches.md +0 -489
- package/eslint.config.js +0 -125
- package/lint-staged.config.js +0 -10
- package/makeTypeDoc.sh +0 -25
- package/make_type_doc.js +0 -59
- package/tests/unit/config/listCrud.spec.js +0 -114
- package/tests/unit/config/objectCrud.spec.js +0 -160
- package/tests/unit/crudPromise.js +0 -40
- package/tests/unit/expectHelpers.js +0 -6
- package/tests/unit/mockOnUnmounted.js +0 -9
- package/tests/unit/poll.js +0 -50
- package/tests/unit/use/cancellableIntent.spec.js +0 -152
- package/tests/unit/use/listCalculated.spec.js +0 -135
- package/tests/unit/use/listFilter.spec.js +0 -484
- package/tests/unit/use/listInstance.spec.js +0 -1166
- package/tests/unit/use/listRelated.spec.js +0 -260
- package/tests/unit/use/listSearch.spec.js +0 -586
- package/tests/unit/use/listSort.spec.js +0 -418
- package/tests/unit/use/listSubscription.spec.js +0 -845
- package/tests/unit/use/loadingError.spec.js +0 -43
- package/tests/unit/use/objectInstance.spec.js +0 -1525
- package/tests/unit/use/objectSubscription.spec.js +0 -849
- package/tests/unit/use/proxyLoadingError.spec.js +0 -64
- package/tests/unit/use/search.spec.js +0 -248
- package/tests/unit/utils/assignReactiveObject.spec.js +0 -593
- package/tests/unit/utils/classes.spec.js +0 -427
- package/tests/unit/utils/deleteKey.spec.js +0 -265
- package/tests/unit/utils/flattenPaths.spec.js +0 -126
- package/tests/unit/utils/keyDiff.spec.js +0 -110
- package/tests/unit/utils/set.spec.js +0 -81
- package/tests/unit/utils/watches.spec.js +0 -276
- package/tsconfig.json +0 -19
- package/typedoc-local-plugins/customize-output.js +0 -13
- package/typedoc.json +0 -11
- package/vitest.config.js +0 -11
|
@@ -1,593 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
addOrUpdateReactiveObject,
|
|
3
|
-
assignReactiveObject,
|
|
4
|
-
AssignReactiveObjectError,
|
|
5
|
-
assignReactiveObjectDeep,
|
|
6
|
-
assignReactiveArray,
|
|
7
|
-
} from "../../../utils/assignReactiveObject.js";
|
|
8
|
-
import { computed, EffectScope, effectScope, reactive, toRef, unref } from "vue";
|
|
9
|
-
import { deepUnref } from "vue-deepunref";
|
|
10
|
-
|
|
11
|
-
describe("utils/assignReactiveObject", function () {
|
|
12
|
-
describe("assignReactiveObject", function () {
|
|
13
|
-
describe("should update the target", function () {
|
|
14
|
-
it("when both target and source are not reactive", function () {
|
|
15
|
-
const target = {
|
|
16
|
-
a: 1,
|
|
17
|
-
b: 2,
|
|
18
|
-
c: 3,
|
|
19
|
-
};
|
|
20
|
-
const source = {
|
|
21
|
-
a: 4,
|
|
22
|
-
b: 5,
|
|
23
|
-
c: 6,
|
|
24
|
-
};
|
|
25
|
-
assignReactiveObject(target, source);
|
|
26
|
-
expect(target).toEqual({
|
|
27
|
-
a: 4,
|
|
28
|
-
b: 5,
|
|
29
|
-
c: 6,
|
|
30
|
-
});
|
|
31
|
-
});
|
|
32
|
-
it("when both target and source are reactive, to not break reactivity", function () {
|
|
33
|
-
const target = reactive({
|
|
34
|
-
a: 1,
|
|
35
|
-
b: 2,
|
|
36
|
-
c: 3,
|
|
37
|
-
});
|
|
38
|
-
const source = reactive({
|
|
39
|
-
a: 4,
|
|
40
|
-
b: 5,
|
|
41
|
-
c: 6,
|
|
42
|
-
});
|
|
43
|
-
const tes = effectScope();
|
|
44
|
-
let computedSum;
|
|
45
|
-
tes.run(() => {
|
|
46
|
-
computedSum = computed(() => target.a + target.b + target.c);
|
|
47
|
-
});
|
|
48
|
-
try {
|
|
49
|
-
// @ts-ignore - effectScope.run is immediate
|
|
50
|
-
expect(computedSum.value).toBe(6);
|
|
51
|
-
assignReactiveObject(target, source);
|
|
52
|
-
expect(target).toEqual({
|
|
53
|
-
a: 4,
|
|
54
|
-
b: 5,
|
|
55
|
-
c: 6,
|
|
56
|
-
});
|
|
57
|
-
// @ts-ignore - effectScope.run is immediate
|
|
58
|
-
expect(computedSum.value).toBe(15);
|
|
59
|
-
} finally {
|
|
60
|
-
computedSum = null;
|
|
61
|
-
tes.stop();
|
|
62
|
-
}
|
|
63
|
-
});
|
|
64
|
-
it("when target is reactive and source are reactive, for non-primitive values", function () {
|
|
65
|
-
const target = reactive({
|
|
66
|
-
a: { e: 8 },
|
|
67
|
-
b: { f: 16 },
|
|
68
|
-
c: null,
|
|
69
|
-
});
|
|
70
|
-
const source = reactive({
|
|
71
|
-
a: { e: 1 },
|
|
72
|
-
b: { f: 2 },
|
|
73
|
-
c: { g: 4 },
|
|
74
|
-
});
|
|
75
|
-
target.c = toRef(source, "c");
|
|
76
|
-
const tes = effectScope();
|
|
77
|
-
let computedSum;
|
|
78
|
-
tes.run(() => {
|
|
79
|
-
computedSum = computed(() => target.a.e + target.b.f + target.c.g);
|
|
80
|
-
});
|
|
81
|
-
try {
|
|
82
|
-
expect(unref(target.a)).not.toBe(unref(source.a));
|
|
83
|
-
expect(unref(target.b)).not.toBe(unref(source.b));
|
|
84
|
-
expect(unref(target.c)).toBe(unref(source.c));
|
|
85
|
-
// @ts-ignore - effectScope.run is immediate
|
|
86
|
-
expect(computedSum.value).toBe(28);
|
|
87
|
-
assignReactiveObject(target, source);
|
|
88
|
-
expect(target).toEqual({
|
|
89
|
-
a: { e: 1 },
|
|
90
|
-
b: { f: 2 },
|
|
91
|
-
c: { g: 4 },
|
|
92
|
-
});
|
|
93
|
-
expect(unref(target.a)).toBe(unref(source.a));
|
|
94
|
-
expect(unref(target.b)).toBe(unref(source.b));
|
|
95
|
-
expect(unref(target.c)).toBe(unref(source.c));
|
|
96
|
-
// @ts-ignore - effectScope.run is immediate
|
|
97
|
-
expect(computedSum.value).toBe(7);
|
|
98
|
-
} finally {
|
|
99
|
-
computedSum = null;
|
|
100
|
-
tes.stop();
|
|
101
|
-
}
|
|
102
|
-
});
|
|
103
|
-
it("does nothing when all targets are objectlike and already point to the source", function () {
|
|
104
|
-
const target = reactive({
|
|
105
|
-
a: undefined,
|
|
106
|
-
b: undefined,
|
|
107
|
-
c: undefined,
|
|
108
|
-
});
|
|
109
|
-
const source = reactive({
|
|
110
|
-
a: { e: 1 },
|
|
111
|
-
b: { f: 2 },
|
|
112
|
-
c: { g: 4 },
|
|
113
|
-
});
|
|
114
|
-
target.a = toRef(source, "a");
|
|
115
|
-
target.b = toRef(source, "b");
|
|
116
|
-
target.c = toRef(source, "c");
|
|
117
|
-
expect(unref(toRef(target, "a"))).toBe(unref(source.a));
|
|
118
|
-
expect(unref(toRef(target, "b"))).toBe(unref(source.b));
|
|
119
|
-
expect(unref(toRef(target, "c"))).toBe(unref(source.c));
|
|
120
|
-
expect(target.a).toBe(source.a);
|
|
121
|
-
expect(target.b).toBe(source.b);
|
|
122
|
-
expect(target.c).toBe(source.c);
|
|
123
|
-
const didAnything = assignReactiveObject(target, source);
|
|
124
|
-
expect(didAnything).toBeFalsy();
|
|
125
|
-
expect(unref(toRef(target, "a"))).toBe(unref(source.a));
|
|
126
|
-
expect(unref(toRef(target, "b"))).toBe(unref(source.b));
|
|
127
|
-
expect(unref(toRef(target, "c"))).toBe(unref(source.c));
|
|
128
|
-
expect(target.a).toBe(source.a);
|
|
129
|
-
expect(target.b).toBe(source.b);
|
|
130
|
-
expect(target.c).toBe(source.c);
|
|
131
|
-
});
|
|
132
|
-
});
|
|
133
|
-
describe("should throw an error", function () {
|
|
134
|
-
it("when target is not an array or object", function () {
|
|
135
|
-
expect(() => assignReactiveObject(null, {})).toThrowError(
|
|
136
|
-
new AssignReactiveObjectError("target must be an object or an array, not null", "invalid-type")
|
|
137
|
-
);
|
|
138
|
-
expect(() => assignReactiveObject(undefined, {})).toThrowError(
|
|
139
|
-
new AssignReactiveObjectError("target must be an object or an array, not undefined", "invalid-type")
|
|
140
|
-
);
|
|
141
|
-
expect(() => assignReactiveObject(1, {})).toThrowError(
|
|
142
|
-
new AssignReactiveObjectError("target must be an object or an array, not 1", "invalid-type")
|
|
143
|
-
);
|
|
144
|
-
expect(() => assignReactiveObject(NaN, {})).toThrowError(
|
|
145
|
-
new AssignReactiveObjectError("target must be an object or an array, not NaN", "invalid-type")
|
|
146
|
-
);
|
|
147
|
-
expect(() => assignReactiveObject(Infinity, {})).toThrowError(
|
|
148
|
-
new AssignReactiveObjectError("target must be an object or an array, not Infinity", "invalid-type")
|
|
149
|
-
);
|
|
150
|
-
});
|
|
151
|
-
it("when source is not an array or object", function () {
|
|
152
|
-
expect(() => assignReactiveObject({}, null)).toThrowError(
|
|
153
|
-
new AssignReactiveObjectError("source must be an object or an array, not null", "invalid-type")
|
|
154
|
-
);
|
|
155
|
-
expect(() => assignReactiveObject({}, undefined)).toThrowError(
|
|
156
|
-
new AssignReactiveObjectError("source must be an object or an array, not undefined", "invalid-type")
|
|
157
|
-
);
|
|
158
|
-
expect(() => assignReactiveObject({}, 1)).toThrowError(
|
|
159
|
-
new AssignReactiveObjectError("source must be an object or an array, not 1", "invalid-type")
|
|
160
|
-
);
|
|
161
|
-
expect(() => assignReactiveObject({}, NaN)).toThrowError(
|
|
162
|
-
new AssignReactiveObjectError("source must be an object or an array, not NaN", "invalid-type")
|
|
163
|
-
);
|
|
164
|
-
expect(() => assignReactiveObject({}, Infinity)).toThrowError(
|
|
165
|
-
new AssignReactiveObjectError("source must be an object or an array, not Infinity", "invalid-type")
|
|
166
|
-
);
|
|
167
|
-
});
|
|
168
|
-
});
|
|
169
|
-
it("should update nested Sets within reactive objects", async () => {
|
|
170
|
-
const target = reactive({
|
|
171
|
-
setA: new Set([1, 2, 3]),
|
|
172
|
-
value: 10,
|
|
173
|
-
});
|
|
174
|
-
const source = reactive({
|
|
175
|
-
setA: new Set([3, 4, 5]),
|
|
176
|
-
value: 20,
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
const es = effectScope();
|
|
180
|
-
let computedSum;
|
|
181
|
-
es.run(() => {
|
|
182
|
-
computedSum = computed(() => {
|
|
183
|
-
let sum = 0;
|
|
184
|
-
for (const num of target.setA) {
|
|
185
|
-
sum += num;
|
|
186
|
-
}
|
|
187
|
-
return sum + target.value;
|
|
188
|
-
});
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
try {
|
|
192
|
-
// @ts-ignore - computedSum.value won't be undefined, it is calculated immediately as required
|
|
193
|
-
expect(computedSum.value).toBe(16);
|
|
194
|
-
|
|
195
|
-
assignReactiveObject(target, source);
|
|
196
|
-
|
|
197
|
-
expect(Array.from(target.setA)).toEqual([3, 4, 5]);
|
|
198
|
-
expect(target.value).toBe(20);
|
|
199
|
-
// @ts-ignore - computedSum.value won't be undefined, it is calculated immediately as required
|
|
200
|
-
expect(computedSum.value).toBe(32);
|
|
201
|
-
|
|
202
|
-
source.setA.add(6);
|
|
203
|
-
expect(target.setA.has(6)).toBe(true);
|
|
204
|
-
// @ts-ignore - computedSum.value won't be undefined, it is calculated immediately as required
|
|
205
|
-
expect(computedSum.value).toBe(38);
|
|
206
|
-
|
|
207
|
-
source.value = 25;
|
|
208
|
-
expect(target.value).toBe(25);
|
|
209
|
-
// @ts-ignore - computedSum.value won't be undefined, it is calculated immediately as required
|
|
210
|
-
expect(computedSum.value).toBe(43);
|
|
211
|
-
} finally {
|
|
212
|
-
es.stop();
|
|
213
|
-
}
|
|
214
|
-
});
|
|
215
|
-
it("should update nested Maps within reactive objects", () => {
|
|
216
|
-
const target = reactive({
|
|
217
|
-
mapA: new Map([
|
|
218
|
-
["x", 1],
|
|
219
|
-
["y", 2],
|
|
220
|
-
]),
|
|
221
|
-
value: 5,
|
|
222
|
-
});
|
|
223
|
-
const source = reactive({
|
|
224
|
-
mapA: new Map([
|
|
225
|
-
["y", 3],
|
|
226
|
-
["z", 4],
|
|
227
|
-
]),
|
|
228
|
-
value: 15,
|
|
229
|
-
});
|
|
230
|
-
|
|
231
|
-
const es = effectScope();
|
|
232
|
-
let computedSum;
|
|
233
|
-
es.run(() => {
|
|
234
|
-
computedSum = computed(() => {
|
|
235
|
-
let sum = 0;
|
|
236
|
-
for (const val of target.mapA.values()) {
|
|
237
|
-
sum += val;
|
|
238
|
-
}
|
|
239
|
-
return sum + target.value;
|
|
240
|
-
});
|
|
241
|
-
});
|
|
242
|
-
|
|
243
|
-
try {
|
|
244
|
-
// @ts-ignore - computedSum.value won't be undefined, it is calculated immediately as required
|
|
245
|
-
expect(computedSum.value).toBe(8);
|
|
246
|
-
|
|
247
|
-
assignReactiveObject(target, source);
|
|
248
|
-
|
|
249
|
-
expect(Array.from(target.mapA.entries())).toEqual([
|
|
250
|
-
["y", 3],
|
|
251
|
-
["z", 4],
|
|
252
|
-
]);
|
|
253
|
-
expect(target.value).toBe(15);
|
|
254
|
-
// @ts-ignore - computedSum.value won't be undefined, it is calculated immediately as required
|
|
255
|
-
expect(computedSum.value).toBe(22);
|
|
256
|
-
|
|
257
|
-
source.mapA.set("w", 5);
|
|
258
|
-
expect(target.mapA.has("w")).toBe(true);
|
|
259
|
-
// @ts-ignore - computedSum.value won't be undefined, it is calculated immediately as required
|
|
260
|
-
expect(computedSum.value).toBe(27);
|
|
261
|
-
|
|
262
|
-
source.value = 20;
|
|
263
|
-
expect(target.value).toBe(20);
|
|
264
|
-
// @ts-ignore - computedSum.value won't be undefined, it is calculated immediately as required
|
|
265
|
-
expect(computedSum.value).toBe(32);
|
|
266
|
-
} finally {
|
|
267
|
-
es.stop();
|
|
268
|
-
}
|
|
269
|
-
});
|
|
270
|
-
it("should update Sets within arrays", () => {
|
|
271
|
-
const target = reactive([new Set([1, 2]), new Set([3, 4])]);
|
|
272
|
-
const source = reactive([new Set([2, 3]), new Set([4, 5])]);
|
|
273
|
-
|
|
274
|
-
const es = effectScope();
|
|
275
|
-
let computedSum;
|
|
276
|
-
es.run(() => {
|
|
277
|
-
computedSum = computed(() => {
|
|
278
|
-
return target.reduce((sum, set) => {
|
|
279
|
-
for (const num of set) {
|
|
280
|
-
sum += num;
|
|
281
|
-
}
|
|
282
|
-
return sum;
|
|
283
|
-
}, 0);
|
|
284
|
-
});
|
|
285
|
-
});
|
|
286
|
-
|
|
287
|
-
try {
|
|
288
|
-
// @ts-ignore - computedSum.value won't be undefined, it is calculated immediately as required
|
|
289
|
-
expect(computedSum.value).toBe(10);
|
|
290
|
-
|
|
291
|
-
assignReactiveObject(target, source);
|
|
292
|
-
|
|
293
|
-
expect(target.length).toBe(2);
|
|
294
|
-
expect(Array.from(target[0])).toEqual([2, 3]);
|
|
295
|
-
expect(Array.from(target[1])).toEqual([4, 5]);
|
|
296
|
-
// @ts-ignore - computedSum.value won't be undefined, it is calculated immediately as required
|
|
297
|
-
expect(computedSum.value).toBe(14);
|
|
298
|
-
|
|
299
|
-
source[0].add(6);
|
|
300
|
-
expect(target[0].has(6)).toBe(true);
|
|
301
|
-
// @ts-ignore - computedSum.value won't be undefined, it is calculated immediately as required
|
|
302
|
-
expect(computedSum.value).toBe(20);
|
|
303
|
-
|
|
304
|
-
source[1].delete(4);
|
|
305
|
-
expect(target[1].has(4)).toBe(false);
|
|
306
|
-
// @ts-ignore - computedSum.value won't be undefined, it is calculated immediately as required
|
|
307
|
-
expect(computedSum.value).toBe(16);
|
|
308
|
-
} finally {
|
|
309
|
-
es.stop();
|
|
310
|
-
}
|
|
311
|
-
});
|
|
312
|
-
it("should update Maps within arrays", () => {
|
|
313
|
-
const target = reactive([
|
|
314
|
-
new Map([
|
|
315
|
-
["a", 1],
|
|
316
|
-
["b", 2],
|
|
317
|
-
]),
|
|
318
|
-
new Map([["c", 3]]),
|
|
319
|
-
]);
|
|
320
|
-
const source = reactive([
|
|
321
|
-
new Map([
|
|
322
|
-
["a", 2],
|
|
323
|
-
["d", 4],
|
|
324
|
-
]),
|
|
325
|
-
new Map([
|
|
326
|
-
["c", 3],
|
|
327
|
-
["e", 5],
|
|
328
|
-
]),
|
|
329
|
-
]);
|
|
330
|
-
|
|
331
|
-
const es = effectScope();
|
|
332
|
-
let computedSum;
|
|
333
|
-
es.run(() => {
|
|
334
|
-
computedSum = computed(() => {
|
|
335
|
-
return target.reduce((sum, map) => {
|
|
336
|
-
for (const val of map.values()) {
|
|
337
|
-
sum += val;
|
|
338
|
-
}
|
|
339
|
-
return sum;
|
|
340
|
-
}, 0);
|
|
341
|
-
});
|
|
342
|
-
});
|
|
343
|
-
|
|
344
|
-
try {
|
|
345
|
-
// @ts-ignore - computedSum.value won't be undefined, it is calculated immediately as required
|
|
346
|
-
expect(computedSum.value).toBe(6);
|
|
347
|
-
|
|
348
|
-
assignReactiveObject(target, source);
|
|
349
|
-
|
|
350
|
-
expect(target.length).toBe(2);
|
|
351
|
-
expect(Array.from(target[0].entries())).toEqual([
|
|
352
|
-
["a", 2],
|
|
353
|
-
["d", 4],
|
|
354
|
-
]);
|
|
355
|
-
expect(Array.from(target[1].entries())).toEqual([
|
|
356
|
-
["c", 3],
|
|
357
|
-
["e", 5],
|
|
358
|
-
]);
|
|
359
|
-
// @ts-ignore - computedSum.value won't be undefined, it is calculated immediately as required
|
|
360
|
-
expect(computedSum.value).toBe(14);
|
|
361
|
-
|
|
362
|
-
source[0].set("f", 6);
|
|
363
|
-
expect(target[0].has("f")).toBe(true);
|
|
364
|
-
// @ts-ignore - computedSum.value won't be undefined, it is calculated immediately as required
|
|
365
|
-
expect(computedSum.value).toBe(20);
|
|
366
|
-
|
|
367
|
-
source[1].delete("c");
|
|
368
|
-
expect(target[1].has("c")).toBe(false);
|
|
369
|
-
// @ts-ignore - computedSum.value won't be undefined, it is calculated immediately as required
|
|
370
|
-
expect(computedSum.value).toBe(17);
|
|
371
|
-
} finally {
|
|
372
|
-
es.stop();
|
|
373
|
-
}
|
|
374
|
-
});
|
|
375
|
-
});
|
|
376
|
-
describe("assignReactiveObjectDeep", () => {
|
|
377
|
-
it("should recursively assign nested Sets and Maps", () => {
|
|
378
|
-
const target = reactive({
|
|
379
|
-
level1: {
|
|
380
|
-
set: new Set([1]),
|
|
381
|
-
map: new Map([["a", 1]]),
|
|
382
|
-
level2: {
|
|
383
|
-
array: [new Set([2])],
|
|
384
|
-
object: {
|
|
385
|
-
map: new Map([["b", 2]]),
|
|
386
|
-
},
|
|
387
|
-
},
|
|
388
|
-
},
|
|
389
|
-
});
|
|
390
|
-
|
|
391
|
-
const source = reactive({
|
|
392
|
-
level1: {
|
|
393
|
-
set: new Set([3]),
|
|
394
|
-
map: new Map([["a", 3]]),
|
|
395
|
-
level2: {
|
|
396
|
-
array: [new Set([4])],
|
|
397
|
-
object: {
|
|
398
|
-
map: new Map([["b", 4]]),
|
|
399
|
-
},
|
|
400
|
-
},
|
|
401
|
-
},
|
|
402
|
-
});
|
|
403
|
-
|
|
404
|
-
const es = effectScope();
|
|
405
|
-
let computedSum;
|
|
406
|
-
es.run(() => {
|
|
407
|
-
computedSum = computed(() => {
|
|
408
|
-
let sum = 0;
|
|
409
|
-
for (const num of target.level1.set) sum += num;
|
|
410
|
-
for (const val of target.level1.map.values()) sum += val;
|
|
411
|
-
for (const num of target.level1.level2.array[0]) sum += num;
|
|
412
|
-
for (const val of target.level1.level2.object.map.values()) sum += val;
|
|
413
|
-
return sum;
|
|
414
|
-
});
|
|
415
|
-
});
|
|
416
|
-
|
|
417
|
-
try {
|
|
418
|
-
// @ts-ignore - computedSum.value won't be undefined, it is calculated immediately as required
|
|
419
|
-
expect(computedSum.value).toBe(6);
|
|
420
|
-
|
|
421
|
-
assignReactiveObjectDeep(target, source);
|
|
422
|
-
|
|
423
|
-
expect(Array.from(target.level1.set)).toEqual([3]);
|
|
424
|
-
expect(Array.from(target.level1.map.entries())).toEqual([["a", 3]]);
|
|
425
|
-
expect(Array.from(target.level1.level2.array[0])).toEqual([4]);
|
|
426
|
-
expect(Array.from(target.level1.level2.object.map.entries())).toEqual([["b", 4]]);
|
|
427
|
-
// @ts-ignore - computedSum.value won't be undefined, it is calculated immediately as required
|
|
428
|
-
expect(computedSum.value).toBe(14);
|
|
429
|
-
|
|
430
|
-
source.level1.set.add(5);
|
|
431
|
-
expect(target.level1.set.has(5)).toBe(true);
|
|
432
|
-
// @ts-ignore - computedSum.value won't be undefined, it is calculated immediately as required
|
|
433
|
-
expect(computedSum.value).toBe(19);
|
|
434
|
-
|
|
435
|
-
source.level1.level2.object.map.set("c", 6);
|
|
436
|
-
expect(target.level1.level2.object.map.has("c")).toBe(true);
|
|
437
|
-
// @ts-ignore - computedSum.value won't be undefined, it is calculated immediately as required
|
|
438
|
-
expect(computedSum.value).toBe(25);
|
|
439
|
-
} finally {
|
|
440
|
-
es.stop();
|
|
441
|
-
}
|
|
442
|
-
});
|
|
443
|
-
});
|
|
444
|
-
it("should work as in the example for the readme", function () {
|
|
445
|
-
const es = new EffectScope();
|
|
446
|
-
es.run(() => {
|
|
447
|
-
const target = reactive({ a: 1, b: undefined });
|
|
448
|
-
const source = { a: 3, b: 4 };
|
|
449
|
-
const source2 = reactive({ a: undefined, b: 5 });
|
|
450
|
-
|
|
451
|
-
const a = toRef(target, "a");
|
|
452
|
-
const b = toRef(target, "b");
|
|
453
|
-
const mySum = computed(() => (a.value || 0) + (b.value || 0));
|
|
454
|
-
|
|
455
|
-
expect(mySum.value).toBe(1);
|
|
456
|
-
assignReactiveObject(target, source);
|
|
457
|
-
expect(mySum.value).toBe(7);
|
|
458
|
-
addOrUpdateReactiveObject(target, source2);
|
|
459
|
-
expect({ ...target }).toEqual({ a: 3, b: 5 });
|
|
460
|
-
expect(mySum.value).toBe(8);
|
|
461
|
-
source2.b = 6;
|
|
462
|
-
expect(mySum.value).toBe(9);
|
|
463
|
-
assignReactiveObject(target, source2);
|
|
464
|
-
expect({ ...target }).toEqual({ b: 6 });
|
|
465
|
-
expect(mySum.value).toBe(6);
|
|
466
|
-
source2.b = 10;
|
|
467
|
-
expect(mySum.value).toBe(10);
|
|
468
|
-
});
|
|
469
|
-
es.stop();
|
|
470
|
-
});
|
|
471
|
-
describe("should propagate changes when used to chain multiple levels of reactivity", function () {
|
|
472
|
-
it("array", function () {
|
|
473
|
-
// assign reactive object will make lists of refs for each layer, causing refs to refs to refs
|
|
474
|
-
// this isn't great but consumers should use other methods to avoid this
|
|
475
|
-
const target = reactive({
|
|
476
|
-
a: [1, 2, 3],
|
|
477
|
-
});
|
|
478
|
-
const source = reactive({
|
|
479
|
-
a: [4, 5, 6],
|
|
480
|
-
});
|
|
481
|
-
const source2 = reactive({
|
|
482
|
-
a: [7, 8, 9],
|
|
483
|
-
});
|
|
484
|
-
expect(source.a).toEqual([4, 5, 6]);
|
|
485
|
-
assignReactiveObject(target.a, source.a);
|
|
486
|
-
expect(source.a).toEqual([4, 5, 6]);
|
|
487
|
-
expect(deepUnref(target.a)).toEqual([4, 5, 6]);
|
|
488
|
-
assignReactiveObject(source.a, source2.a);
|
|
489
|
-
expect(source2.a).toEqual([7, 8, 9]);
|
|
490
|
-
expect(deepUnref(source.a)).toEqual([7, 8, 9]);
|
|
491
|
-
expect(deepUnref(deepUnref(target.a))).toEqual([7, 8, 9]);
|
|
492
|
-
});
|
|
493
|
-
it("object", function () {
|
|
494
|
-
const target = reactive({
|
|
495
|
-
a: { b: 1, c: 2, d: 3 },
|
|
496
|
-
});
|
|
497
|
-
const source = reactive({
|
|
498
|
-
a: { b: 4, c: 5, d: 6 },
|
|
499
|
-
});
|
|
500
|
-
const source2 = reactive({
|
|
501
|
-
a: { b: 7, c: 8, d: 9 },
|
|
502
|
-
});
|
|
503
|
-
expect(source.a).toEqual({ b: 4, c: 5, d: 6 });
|
|
504
|
-
assignReactiveObject(target.a, source.a);
|
|
505
|
-
expect(target.a).toEqual({ b: 4, c: 5, d: 6 });
|
|
506
|
-
expect(source.a).toEqual({ b: 4, c: 5, d: 6 });
|
|
507
|
-
assignReactiveObject(source.a, source2.a);
|
|
508
|
-
expect(source2.a).toEqual({ b: 7, c: 8, d: 9 });
|
|
509
|
-
expect(source.a).toEqual({ b: 7, c: 8, d: 9 });
|
|
510
|
-
expect(target.a).toEqual({ b: 7, c: 8, d: 9 });
|
|
511
|
-
});
|
|
512
|
-
});
|
|
513
|
-
describe("assignReactiveArray", () => {
|
|
514
|
-
it("should assign values from source to target array", () => {
|
|
515
|
-
const target = reactive([1, 2, 3]);
|
|
516
|
-
const source = reactive([4, 5, 6]);
|
|
517
|
-
|
|
518
|
-
const didAnything = assignReactiveArray(target, source);
|
|
519
|
-
expect(didAnything).toBe(true);
|
|
520
|
-
|
|
521
|
-
// Unref the elements for comparison
|
|
522
|
-
const unrefTarget = target.map(unref);
|
|
523
|
-
expect(unrefTarget).toEqual([4, 5, 6]);
|
|
524
|
-
});
|
|
525
|
-
|
|
526
|
-
it("should handle source array longer than target", () => {
|
|
527
|
-
const target = reactive([1, 2]);
|
|
528
|
-
const source = reactive([3, 4, 5]);
|
|
529
|
-
|
|
530
|
-
assignReactiveArray(target, source);
|
|
531
|
-
|
|
532
|
-
const unrefTarget = target.map(unref);
|
|
533
|
-
expect(unrefTarget).toEqual([3, 4, 5]);
|
|
534
|
-
});
|
|
535
|
-
|
|
536
|
-
it("should handle source array shorter than target", () => {
|
|
537
|
-
const target = reactive([1, 2, 3]);
|
|
538
|
-
const source = reactive([4]);
|
|
539
|
-
|
|
540
|
-
assignReactiveArray(target, source);
|
|
541
|
-
|
|
542
|
-
const unrefTarget = target.map(unref);
|
|
543
|
-
expect(unrefTarget).toEqual([4]);
|
|
544
|
-
});
|
|
545
|
-
|
|
546
|
-
it("should not create reactive links between array elements", () => {
|
|
547
|
-
const target = reactive([{ val: 1 }, { val: 2 }]);
|
|
548
|
-
const source = reactive([{ val: 3 }, { val: 4 }]);
|
|
549
|
-
|
|
550
|
-
assignReactiveArray(target, source);
|
|
551
|
-
|
|
552
|
-
const unrefTarget = target.map(unref);
|
|
553
|
-
expect(unrefTarget).toEqual([{ val: 3 }, { val: 4 }]);
|
|
554
|
-
|
|
555
|
-
// Modify source
|
|
556
|
-
source[0].val = 5;
|
|
557
|
-
|
|
558
|
-
// Since the elements are refs to source, target should reflect changes
|
|
559
|
-
expect(unref(target[0]).val).toBe(5);
|
|
560
|
-
});
|
|
561
|
-
|
|
562
|
-
it("should reverse the target array if it matches the reversed source", () => {
|
|
563
|
-
const target = reactive([1, 2, 3]);
|
|
564
|
-
const source = reactive([3, 2, 1]);
|
|
565
|
-
|
|
566
|
-
const didAnything = assignReactiveArray(target, source);
|
|
567
|
-
expect(didAnything).toBe(true);
|
|
568
|
-
|
|
569
|
-
const unrefTarget = target.map(unref);
|
|
570
|
-
expect(unrefTarget).toEqual([3, 2, 1]);
|
|
571
|
-
});
|
|
572
|
-
|
|
573
|
-
it("should handle nested arrays and objects", () => {
|
|
574
|
-
const target = reactive([[1], [2], [3]]);
|
|
575
|
-
const source = reactive([[4], [5], [6]]);
|
|
576
|
-
|
|
577
|
-
assignReactiveArray(target, source);
|
|
578
|
-
|
|
579
|
-
const unrefTarget = target.map(unref).map((item) => (Array.isArray(item) ? item.map(unref) : item));
|
|
580
|
-
expect(unrefTarget).toEqual([[4], [5], [6]]);
|
|
581
|
-
});
|
|
582
|
-
|
|
583
|
-
it("should handle nested arrays and objects with different lengths", () => {
|
|
584
|
-
const target = reactive([[1], [2], [3]]);
|
|
585
|
-
const source = reactive([[4], [5]]);
|
|
586
|
-
|
|
587
|
-
assignReactiveArray(target, source);
|
|
588
|
-
|
|
589
|
-
const unrefTarget = target.map(unref).map((item) => (Array.isArray(item) ? item.map(unref) : item));
|
|
590
|
-
expect(unrefTarget).toEqual([[4], [5]]);
|
|
591
|
-
});
|
|
592
|
-
});
|
|
593
|
-
});
|