@arrai-innovations/reactive-helpers 10.4.2 → 11.0.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/.circleci/config.yml +3 -3
- package/README.md +123 -146
- package/config/listCrud.js +0 -1
- package/docs.md +178 -34
- package/package.json +3 -1
- package/tests/unit/use/listFilter.spec.js +335 -165
- package/tests/unit/use/listInstance.spec.js +14 -1
- package/tests/unit/use/listSearch.spec.js +505 -0
- package/tests/unit/use/listSort.spec.js +17 -1
- package/tests/unit/use/listSubscription.spec.js +91 -108
- package/tests/unit/use/search.spec.js +217 -36
- package/tests/unit/utils/assignReactiveObject.spec.js +43 -1
- package/tests/unit/{use → utils}/watches.spec.js +27 -3
- package/use/cancellableIntent.js +8 -3
- package/use/index.js +2 -0
- package/use/list.js +41 -14
- package/use/listCalculated.js +22 -19
- package/use/listFilter.js +146 -152
- package/use/listInstance.js +26 -31
- package/use/listKeys.js +99 -0
- package/use/listRelated.js +22 -14
- package/use/listSearch.js +357 -0
- package/use/listSort.js +46 -49
- package/use/listSubscription.js +3 -13
- package/use/search.js +154 -64
- package/use/watchesRunning.js +1 -1
- package/utils/assignReactiveObject.js +58 -14
- package/utils/index.js +1 -0
- package/utils/keyDiff.js +13 -7
- package/utils/relatedCalculatedHelpers.js +17 -0
- package/utils/watches.js +14 -11
|
@@ -1,152 +1,64 @@
|
|
|
1
1
|
import { useListFilters, useListSort } from "../../../use/index.js";
|
|
2
2
|
import { doAwaitNot } from "../../../utils/watches.js";
|
|
3
|
-
import {
|
|
3
|
+
import { ref, unref, reactive } from "vue";
|
|
4
4
|
import { deepUnref } from "vue-deepunref";
|
|
5
5
|
|
|
6
6
|
describe("use/listFilter", () => {
|
|
7
|
-
let useListInstance, useListFilter,
|
|
7
|
+
let useListInstance, useListFilter, useListCalculated, useListRelated;
|
|
8
8
|
beforeEach(async () => {
|
|
9
9
|
const listInstanceModule = await import("../../../use/listInstance");
|
|
10
10
|
useListInstance = listInstanceModule.useListInstance;
|
|
11
11
|
const listFilterModule = await import("../../../use/listFilter");
|
|
12
12
|
useListFilter = listFilterModule.useListFilter;
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
});
|
|
13
|
+
const listRelatedModule = await import("../../../use/listRelated");
|
|
14
|
+
useListRelated = listRelatedModule.useListRelated;
|
|
15
|
+
const listCalculatedModule = await import("../../../use/listCalculated");
|
|
16
|
+
useListCalculated = listCalculatedModule.useListCalculated;
|
|
18
17
|
});
|
|
19
18
|
|
|
20
|
-
it("should match an allowed values list", async () => {
|
|
21
|
-
const list = useListInstance({ props: {} });
|
|
22
|
-
const filter = useListFilter({
|
|
23
|
-
parentState: list.state,
|
|
24
|
-
allowedValues: reactive({
|
|
25
|
-
1: true,
|
|
26
|
-
3: true,
|
|
27
|
-
}),
|
|
28
|
-
});
|
|
29
|
-
await nextTick();
|
|
30
|
-
expect(filter.state.objects).toEqual({});
|
|
31
|
-
list.addListObject({ id: 1, name: "one", has_things: true });
|
|
32
|
-
await nextTick();
|
|
33
|
-
expect(filter.state.objects).toEqual({
|
|
34
|
-
1: { id: 1, name: "one", has_things: true },
|
|
35
|
-
});
|
|
36
|
-
list.addListObject({ id: 2, name: "two", has_things: true });
|
|
37
|
-
await nextTick();
|
|
38
|
-
expect(filter.state.objects).toEqual({
|
|
39
|
-
1: { id: 1, name: "one", has_things: true },
|
|
40
|
-
});
|
|
41
|
-
list.addListObject({ id: 3, name: "three", has_things: true });
|
|
42
|
-
await nextTick();
|
|
43
|
-
expect(filter.state.objects).toEqual({
|
|
44
|
-
1: { id: 1, name: "one", has_things: true },
|
|
45
|
-
3: { id: 3, name: "three", has_things: true },
|
|
46
|
-
});
|
|
47
|
-
list.addListObject({ id: 4, name: "four", has_things: true });
|
|
48
|
-
await nextTick();
|
|
49
|
-
expect(filter.state.objects).toEqual({
|
|
50
|
-
1: { id: 1, name: "one", has_things: true },
|
|
51
|
-
3: { id: 3, name: "three", has_things: true },
|
|
52
|
-
});
|
|
53
|
-
});
|
|
54
19
|
it("should match an allowed filter function", async () => {
|
|
55
20
|
const list = useListInstance({ props: {} });
|
|
56
21
|
const filter = useListFilter({
|
|
57
22
|
parentState: list.state,
|
|
58
|
-
allowedFilter: (object) => object.id
|
|
59
|
-
});
|
|
60
|
-
await nextTick();
|
|
61
|
-
expect(filter.state.objects).toEqual({});
|
|
62
|
-
list.addListObject({ id: 1, name: "one", has_things: true });
|
|
63
|
-
await nextTick();
|
|
64
|
-
expect(filter.state.objects).toEqual({
|
|
65
|
-
1: { id: 1, name: "one", has_things: true },
|
|
66
|
-
});
|
|
67
|
-
list.addListObject({ id: 2, name: "two", has_things: true });
|
|
68
|
-
await nextTick();
|
|
69
|
-
expect(filter.state.objects).toEqual({
|
|
70
|
-
1: { id: 1, name: "one", has_things: true },
|
|
71
|
-
});
|
|
72
|
-
list.addListObject({ id: 3, name: "three", has_things: true });
|
|
73
|
-
await nextTick();
|
|
74
|
-
expect(filter.state.objects).toEqual({
|
|
75
|
-
1: { id: 1, name: "one", has_things: true },
|
|
76
|
-
3: { id: 3, name: "three", has_things: true },
|
|
77
|
-
});
|
|
78
|
-
list.addListObject({ id: 4, name: "four", has_things: true });
|
|
79
|
-
await nextTick();
|
|
80
|
-
expect(filter.state.objects).toEqual({
|
|
81
|
-
1: { id: 1, name: "one", has_things: true },
|
|
82
|
-
3: { id: 3, name: "three", has_things: true },
|
|
83
|
-
});
|
|
84
|
-
});
|
|
85
|
-
it("should match by search term", async () => {
|
|
86
|
-
const textSearchValue = ref("one");
|
|
87
|
-
const list = useListInstance({ props: {} });
|
|
88
|
-
const filter = useListFilter({
|
|
89
|
-
parentState: list.state,
|
|
90
|
-
useTextSearch: true,
|
|
91
|
-
textSearchRules: ["name"],
|
|
92
|
-
textSearchValue,
|
|
93
|
-
});
|
|
94
|
-
await nextTick();
|
|
95
|
-
// todo: it would be nice to wait for all searches to finish, instead of awaiting the first one back.
|
|
96
|
-
await doAwaitNot({
|
|
97
|
-
obj: filter.state,
|
|
98
|
-
prop: "searching",
|
|
23
|
+
allowedFilter: (object) => object.id === 1 || object.id === 3,
|
|
99
24
|
});
|
|
100
|
-
await nextTick();
|
|
101
25
|
expect(filter.state.objects).toEqual({});
|
|
102
26
|
list.addListObject({ id: 1, name: "one", has_things: true });
|
|
103
|
-
await nextTick();
|
|
104
27
|
await doAwaitNot({
|
|
105
28
|
obj: filter.state,
|
|
106
|
-
prop: "
|
|
29
|
+
prop: "running",
|
|
30
|
+
timeout: 100,
|
|
107
31
|
});
|
|
108
|
-
await nextTick();
|
|
109
32
|
expect(filter.state.objects).toEqual({
|
|
110
33
|
1: { id: 1, name: "one", has_things: true },
|
|
111
34
|
});
|
|
112
35
|
list.addListObject({ id: 2, name: "two", has_things: true });
|
|
113
|
-
await nextTick();
|
|
114
36
|
await doAwaitNot({
|
|
115
37
|
obj: filter.state,
|
|
116
|
-
prop: "
|
|
38
|
+
prop: "running",
|
|
39
|
+
timeout: 100,
|
|
117
40
|
});
|
|
118
|
-
await nextTick();
|
|
119
41
|
expect(filter.state.objects).toEqual({
|
|
120
42
|
1: { id: 1, name: "one", has_things: true },
|
|
121
43
|
});
|
|
122
44
|
list.addListObject({ id: 3, name: "three", has_things: true });
|
|
123
|
-
await nextTick();
|
|
124
45
|
await doAwaitNot({
|
|
125
46
|
obj: filter.state,
|
|
126
|
-
prop: "
|
|
47
|
+
prop: "running",
|
|
48
|
+
timeout: 100,
|
|
127
49
|
});
|
|
128
|
-
await nextTick();
|
|
129
50
|
expect(filter.state.objects).toEqual({
|
|
130
51
|
1: { id: 1, name: "one", has_things: true },
|
|
131
|
-
});
|
|
132
|
-
textSearchValue.value = "three";
|
|
133
|
-
await nextTick();
|
|
134
|
-
await doAwaitNot({
|
|
135
|
-
obj: filter.state,
|
|
136
|
-
prop: "searching",
|
|
137
|
-
});
|
|
138
|
-
await nextTick();
|
|
139
|
-
expect(filter.state.objects).toEqual({
|
|
140
52
|
3: { id: 3, name: "three", has_things: true },
|
|
141
53
|
});
|
|
142
54
|
list.addListObject({ id: 4, name: "four", has_things: true });
|
|
143
|
-
await nextTick();
|
|
144
55
|
await doAwaitNot({
|
|
145
56
|
obj: filter.state,
|
|
146
|
-
prop: "
|
|
57
|
+
prop: "running",
|
|
58
|
+
timeout: 100,
|
|
147
59
|
});
|
|
148
|
-
await nextTick();
|
|
149
60
|
expect(filter.state.objects).toEqual({
|
|
61
|
+
1: { id: 1, name: "one", has_things: true },
|
|
150
62
|
3: { id: 3, name: "three", has_things: true },
|
|
151
63
|
});
|
|
152
64
|
});
|
|
@@ -156,60 +68,41 @@ describe("use/listFilter", () => {
|
|
|
156
68
|
parentState: list.state,
|
|
157
69
|
excludedFilter: (object) => object.id == 2 || object.id == 4,
|
|
158
70
|
});
|
|
159
|
-
await nextTick();
|
|
160
71
|
expect(filter.state.objects).toEqual({});
|
|
161
72
|
list.addListObject({ id: 1, name: "one", has_things: true });
|
|
162
|
-
await
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
list.addListObject({ id: 2, name: "two", has_things: true });
|
|
167
|
-
await nextTick();
|
|
168
|
-
expect(filter.state.objects).toEqual({
|
|
169
|
-
1: { id: 1, name: "one", has_things: true },
|
|
170
|
-
});
|
|
171
|
-
list.addListObject({ id: 3, name: "three", has_things: true });
|
|
172
|
-
await nextTick();
|
|
173
|
-
expect(filter.state.objects).toEqual({
|
|
174
|
-
1: { id: 1, name: "one", has_things: true },
|
|
175
|
-
3: { id: 3, name: "three", has_things: true },
|
|
176
|
-
});
|
|
177
|
-
list.addListObject({ id: 4, name: "four", has_things: true });
|
|
178
|
-
await nextTick();
|
|
179
|
-
expect(filter.state.objects).toEqual({
|
|
180
|
-
1: { id: 1, name: "one", has_things: true },
|
|
181
|
-
3: { id: 3, name: "three", has_things: true },
|
|
182
|
-
});
|
|
183
|
-
});
|
|
184
|
-
it("should exclude an excludedValues parameter", async () => {
|
|
185
|
-
const list = useListInstance({ props: {} });
|
|
186
|
-
const filter = useListFilter({
|
|
187
|
-
parentState: list.state,
|
|
188
|
-
excludedValues: reactive({
|
|
189
|
-
2: true,
|
|
190
|
-
4: true,
|
|
191
|
-
}),
|
|
73
|
+
await doAwaitNot({
|
|
74
|
+
obj: filter.state,
|
|
75
|
+
prop: "running",
|
|
76
|
+
timeout: 100,
|
|
192
77
|
});
|
|
193
|
-
await nextTick();
|
|
194
|
-
expect(filter.state.objects).toEqual({});
|
|
195
|
-
list.addListObject({ id: 1, name: "one", has_things: true });
|
|
196
|
-
await nextTick();
|
|
197
78
|
expect(filter.state.objects).toEqual({
|
|
198
79
|
1: { id: 1, name: "one", has_things: true },
|
|
199
80
|
});
|
|
200
81
|
list.addListObject({ id: 2, name: "two", has_things: true });
|
|
201
|
-
await
|
|
82
|
+
await doAwaitNot({
|
|
83
|
+
obj: filter.state,
|
|
84
|
+
prop: "running",
|
|
85
|
+
timeout: 100,
|
|
86
|
+
});
|
|
202
87
|
expect(filter.state.objects).toEqual({
|
|
203
88
|
1: { id: 1, name: "one", has_things: true },
|
|
204
89
|
});
|
|
205
90
|
list.addListObject({ id: 3, name: "three", has_things: true });
|
|
206
|
-
await
|
|
91
|
+
await doAwaitNot({
|
|
92
|
+
obj: filter.state,
|
|
93
|
+
prop: "running",
|
|
94
|
+
timeout: 100,
|
|
95
|
+
});
|
|
207
96
|
expect(filter.state.objects).toEqual({
|
|
208
97
|
1: { id: 1, name: "one", has_things: true },
|
|
209
98
|
3: { id: 3, name: "three", has_things: true },
|
|
210
99
|
});
|
|
211
100
|
list.addListObject({ id: 4, name: "four", has_things: true });
|
|
212
|
-
await
|
|
101
|
+
await doAwaitNot({
|
|
102
|
+
obj: filter.state,
|
|
103
|
+
prop: "running",
|
|
104
|
+
timeout: 100,
|
|
105
|
+
});
|
|
213
106
|
expect(filter.state.objects).toEqual({
|
|
214
107
|
1: { id: 1, name: "one", has_things: true },
|
|
215
108
|
3: { id: 3, name: "three", has_things: true },
|
|
@@ -228,9 +121,7 @@ describe("use/listFilter", () => {
|
|
|
228
121
|
}
|
|
229
122
|
const filter = useListFilter({
|
|
230
123
|
parentState: listInstance.state,
|
|
231
|
-
useTextSearch: true,
|
|
232
124
|
});
|
|
233
|
-
await nextTick();
|
|
234
125
|
expect(filter.state.objects).toEqual(listInstance.state.objects);
|
|
235
126
|
});
|
|
236
127
|
describe("useListFilter operates on parentState modified by useListSort", () => {
|
|
@@ -259,7 +150,10 @@ describe("use/listFilter", () => {
|
|
|
259
150
|
const filter = useListFilter({
|
|
260
151
|
parentState: listSort.state,
|
|
261
152
|
});
|
|
262
|
-
await
|
|
153
|
+
await doAwaitNot({
|
|
154
|
+
obj: filter.state,
|
|
155
|
+
prop: "running",
|
|
156
|
+
});
|
|
263
157
|
expect(filter.state.order).toEqual(expectedOrder);
|
|
264
158
|
expect(filter.state.objectsInOrder).toEqual(orderedObjects);
|
|
265
159
|
});
|
|
@@ -286,32 +180,22 @@ describe("use/listFilter", () => {
|
|
|
286
180
|
},
|
|
287
181
|
},
|
|
288
182
|
});
|
|
183
|
+
const excludedFilter = (object) => object.id === 1 || object.name === "three";
|
|
184
|
+
const allowedFilter = (object) => object.id === 2 || object.name === "four";
|
|
289
185
|
const listFilterA = useListFilter({
|
|
290
186
|
parentState: listInstanceA.state,
|
|
291
|
-
|
|
292
|
-
id: 1,
|
|
293
|
-
name: "three",
|
|
294
|
-
},
|
|
187
|
+
excludedFilter,
|
|
295
188
|
});
|
|
296
189
|
const listFilterB = useListFilter({
|
|
297
190
|
parentState: listInstanceB.state,
|
|
298
|
-
|
|
299
|
-
id: 2,
|
|
300
|
-
name: "four",
|
|
301
|
-
},
|
|
191
|
+
allowedFilter,
|
|
302
192
|
});
|
|
303
193
|
const listFilterArgs = {
|
|
304
194
|
A: {
|
|
305
|
-
|
|
306
|
-
id: 1,
|
|
307
|
-
name: "three",
|
|
308
|
-
},
|
|
195
|
+
excludedFilter,
|
|
309
196
|
},
|
|
310
197
|
B: {
|
|
311
|
-
|
|
312
|
-
id: 2,
|
|
313
|
-
name: "four",
|
|
314
|
-
},
|
|
198
|
+
allowedFilter,
|
|
315
199
|
},
|
|
316
200
|
};
|
|
317
201
|
const listInstances = {
|
|
@@ -320,12 +204,298 @@ describe("use/listFilter", () => {
|
|
|
320
204
|
};
|
|
321
205
|
const listFilters = useListFilters(listFilterArgs, listInstances);
|
|
322
206
|
|
|
323
|
-
expect(listFilters.A.state.
|
|
324
|
-
expect(listFilters.B.state.
|
|
325
|
-
expect(
|
|
326
|
-
expect(
|
|
207
|
+
expect(listFilters.A.state.excludedFilter).toEqual(listFilterArgs.A.excludedFilter);
|
|
208
|
+
expect(listFilters.B.state.allowedFilter).toEqual(listFilterArgs.B.allowedFilter);
|
|
209
|
+
expect(unref(listFilters.A.parentState)).toEqual(unref(listFilterA.parentState));
|
|
210
|
+
expect(unref(listFilters.B.parentState)).toEqual(unref(listFilterB.parentState));
|
|
327
211
|
expect(deepUnref(listFilters.A.state)).toEqual(deepUnref(listFilterA.state));
|
|
328
212
|
expect(deepUnref(listFilters.B.state)).toEqual(deepUnref(listFilterB.state));
|
|
329
213
|
});
|
|
330
214
|
});
|
|
215
|
+
describe("useListFilters updates index when", () => {
|
|
216
|
+
it("parentInstance.objects is updated", async () => {
|
|
217
|
+
const list = useListInstance({ props: {} });
|
|
218
|
+
const filter = useListFilter({
|
|
219
|
+
parentState: list.state,
|
|
220
|
+
allowedFilter: (object) => !!object.allowed?.every((e) => e),
|
|
221
|
+
});
|
|
222
|
+
await doAwaitNot({
|
|
223
|
+
obj: filter.state,
|
|
224
|
+
prop: "running",
|
|
225
|
+
timeout: 100,
|
|
226
|
+
});
|
|
227
|
+
expect(filter.state.objects).toEqual({});
|
|
228
|
+
list.addListObject({ id: 1, name: "one", has_things: true, allowed: [true, true] });
|
|
229
|
+
await doAwaitNot({
|
|
230
|
+
obj: filter.state,
|
|
231
|
+
prop: "running",
|
|
232
|
+
timeout: 100,
|
|
233
|
+
});
|
|
234
|
+
expect(filter.state.objects).toEqual({
|
|
235
|
+
1: { id: 1, name: "one", has_things: true, allowed: [true, true] },
|
|
236
|
+
});
|
|
237
|
+
list.addListObject({ id: 2, name: "two", has_things: true, allowed: [true, false] });
|
|
238
|
+
await doAwaitNot({
|
|
239
|
+
obj: filter.state,
|
|
240
|
+
prop: "running",
|
|
241
|
+
timeout: 100,
|
|
242
|
+
});
|
|
243
|
+
expect(filter.state.objects).toEqual({
|
|
244
|
+
1: { id: 1, name: "one", has_things: true, allowed: [true, true] },
|
|
245
|
+
});
|
|
246
|
+
list.state.objects[1].allowed[0] = false;
|
|
247
|
+
list.state.objects[2].allowed[1] = true;
|
|
248
|
+
await doAwaitNot({
|
|
249
|
+
obj: filter.state,
|
|
250
|
+
prop: "running",
|
|
251
|
+
timeout: 100,
|
|
252
|
+
});
|
|
253
|
+
expect(filter.state.objects).toEqual({
|
|
254
|
+
2: { id: 2, name: "two", has_things: true, allowed: [true, true] },
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
it("parentInstance.relatedObjects is updated", async () => {
|
|
258
|
+
const list = useListInstance({ props: {} });
|
|
259
|
+
const relatedList = useListInstance({ props: {} });
|
|
260
|
+
const related = useListRelated({
|
|
261
|
+
parentState: list.state,
|
|
262
|
+
relatedObjectsRules: {
|
|
263
|
+
relatedRuleName: {
|
|
264
|
+
objects: relatedList.state.objects,
|
|
265
|
+
pkKey: "related_id",
|
|
266
|
+
},
|
|
267
|
+
},
|
|
268
|
+
});
|
|
269
|
+
const allowedFilter = ref();
|
|
270
|
+
allowedFilter.value = (object, relatedObject) =>
|
|
271
|
+
relatedObject.relatedRuleName.allowed && relatedObject.relatedRuleName.has_things && object.has_things;
|
|
272
|
+
const filter = useListFilter({
|
|
273
|
+
parentState: related.state,
|
|
274
|
+
allowedFilter,
|
|
275
|
+
});
|
|
276
|
+
await doAwaitNot({
|
|
277
|
+
obj: filter.state,
|
|
278
|
+
prop: "running",
|
|
279
|
+
timeout: 100,
|
|
280
|
+
});
|
|
281
|
+
expect(filter.state.objects).toEqual({});
|
|
282
|
+
relatedList.addListObject({ id: 2, name: "two", has_things: true, allowed: true });
|
|
283
|
+
list.addListObject({ id: 1, name: "one", has_things: true, related_id: 2 });
|
|
284
|
+
await doAwaitNot({
|
|
285
|
+
obj: filter.state,
|
|
286
|
+
prop: "running",
|
|
287
|
+
timeout: 100,
|
|
288
|
+
});
|
|
289
|
+
expect(filter.state.objects).toEqual({
|
|
290
|
+
1: { id: 1, name: "one", has_things: true, related_id: 2 },
|
|
291
|
+
});
|
|
292
|
+
relatedList.addListObject({ id: 4, name: "four", has_things: true, allowed: false });
|
|
293
|
+
list.addListObject({ id: 3, name: "three", has_things: true, related_id: 4 });
|
|
294
|
+
await doAwaitNot({
|
|
295
|
+
obj: filter.state,
|
|
296
|
+
prop: "running",
|
|
297
|
+
timeout: 100,
|
|
298
|
+
});
|
|
299
|
+
expect(filter.state.objects).toEqual({
|
|
300
|
+
1: { id: 1, name: "one", has_things: true, related_id: 2 },
|
|
301
|
+
});
|
|
302
|
+
relatedList.state.objects[2].allowed = false;
|
|
303
|
+
relatedList.state.objects[4].allowed = true;
|
|
304
|
+
await doAwaitNot({
|
|
305
|
+
obj: filter.state,
|
|
306
|
+
prop: "running",
|
|
307
|
+
timeout: 100,
|
|
308
|
+
});
|
|
309
|
+
expect(filter.state.objects).toEqual({
|
|
310
|
+
3: { id: 3, name: "three", has_things: true, related_id: 4 },
|
|
311
|
+
});
|
|
312
|
+
});
|
|
313
|
+
it("parentInstance.calculatedObjects is updated", async () => {
|
|
314
|
+
const list = useListInstance({ props: {} });
|
|
315
|
+
const calculated = useListCalculated({
|
|
316
|
+
parentState: list.state,
|
|
317
|
+
calculatedObjectsRules: reactive({
|
|
318
|
+
calculatedRuleName: (object) => object.things_count + object.other_things_count,
|
|
319
|
+
}),
|
|
320
|
+
});
|
|
321
|
+
const allowedFilter = ref();
|
|
322
|
+
allowedFilter.value = (object, relatedObject, calculatedObject) => calculatedObject.calculatedRuleName > 5;
|
|
323
|
+
const filter = useListFilter({
|
|
324
|
+
parentState: calculated.state,
|
|
325
|
+
allowedFilter,
|
|
326
|
+
});
|
|
327
|
+
await doAwaitNot({
|
|
328
|
+
obj: filter.state,
|
|
329
|
+
prop: "running",
|
|
330
|
+
});
|
|
331
|
+
expect(filter.state.objects).toEqual({});
|
|
332
|
+
list.addListObject({ id: 1, name: "one", has_things: true, things_count: 2, other_things_count: 3 });
|
|
333
|
+
await doAwaitNot({
|
|
334
|
+
obj: filter.state,
|
|
335
|
+
prop: "running",
|
|
336
|
+
});
|
|
337
|
+
expect(filter.state.objects).toEqual({});
|
|
338
|
+
list.addListObject({ id: 2, name: "two", has_things: true, things_count: 2, other_things_count: 4 });
|
|
339
|
+
await doAwaitNot({
|
|
340
|
+
obj: filter.state,
|
|
341
|
+
prop: "running",
|
|
342
|
+
});
|
|
343
|
+
expect(filter.state.objects).toEqual({
|
|
344
|
+
2: { id: 2, name: "two", has_things: true, things_count: 2, other_things_count: 4 },
|
|
345
|
+
});
|
|
346
|
+
list.state.objects[1].things_count = 4;
|
|
347
|
+
list.state.objects[2].other_things_count = 0;
|
|
348
|
+
await doAwaitNot({
|
|
349
|
+
obj: filter.state,
|
|
350
|
+
prop: "running",
|
|
351
|
+
});
|
|
352
|
+
expect(filter.state.objects).toEqual({
|
|
353
|
+
1: { id: 1, name: "one", has_things: true, things_count: 4, other_things_count: 3 },
|
|
354
|
+
});
|
|
355
|
+
});
|
|
356
|
+
});
|
|
357
|
+
it("you can use nested useListFilters", async () => {
|
|
358
|
+
const list = useListInstance({ props: {} });
|
|
359
|
+
|
|
360
|
+
function filter1AllowedFilter(object) {
|
|
361
|
+
return object.has_things && object.has_stuff;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
function filter2AllowedFilter(object) {
|
|
365
|
+
return object.has_things && object.has_other_stuff;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
const filter1 = useListFilter({
|
|
369
|
+
parentState: list.state,
|
|
370
|
+
allowedFilter: filter1AllowedFilter,
|
|
371
|
+
});
|
|
372
|
+
const filter2 = useListFilter({
|
|
373
|
+
parentState: filter1.state,
|
|
374
|
+
allowedFilter: filter2AllowedFilter,
|
|
375
|
+
});
|
|
376
|
+
await doAwaitNot({
|
|
377
|
+
obj: filter1.state,
|
|
378
|
+
prop: "running",
|
|
379
|
+
});
|
|
380
|
+
await doAwaitNot({
|
|
381
|
+
obj: filter2.state,
|
|
382
|
+
prop: "running",
|
|
383
|
+
});
|
|
384
|
+
expect(filter1.state.objects).toEqual({});
|
|
385
|
+
expect(filter1.state.order).toEqual([]);
|
|
386
|
+
expect(filter2.state.objects).toEqual({});
|
|
387
|
+
expect(filter2.state.order).toEqual([]);
|
|
388
|
+
list.addListObject({ id: 1, name: "one", has_things: true, has_stuff: true, has_other_stuff: true });
|
|
389
|
+
await doAwaitNot({
|
|
390
|
+
obj: filter1.state,
|
|
391
|
+
prop: "running",
|
|
392
|
+
});
|
|
393
|
+
await doAwaitNot({
|
|
394
|
+
obj: filter2.state,
|
|
395
|
+
prop: "running",
|
|
396
|
+
});
|
|
397
|
+
expect(filter1.state.objects).toEqual({
|
|
398
|
+
1: { id: 1, name: "one", has_things: true, has_stuff: true, has_other_stuff: true },
|
|
399
|
+
});
|
|
400
|
+
expect(filter1.state.order).toEqual(["1"]);
|
|
401
|
+
expect(filter2.state.objects).toEqual({
|
|
402
|
+
1: { id: 1, name: "one", has_things: true, has_stuff: true, has_other_stuff: true },
|
|
403
|
+
});
|
|
404
|
+
expect(filter2.state.order).toEqual(["1"]);
|
|
405
|
+
list.addListObject({ id: 2, name: "two", has_things: true, has_stuff: true, has_other_stuff: false });
|
|
406
|
+
await doAwaitNot({
|
|
407
|
+
obj: filter1.state,
|
|
408
|
+
prop: "running",
|
|
409
|
+
});
|
|
410
|
+
await doAwaitNot({
|
|
411
|
+
obj: filter2.state,
|
|
412
|
+
prop: "running",
|
|
413
|
+
});
|
|
414
|
+
expect(filter1.state.objects).toEqual({
|
|
415
|
+
1: { id: 1, name: "one", has_things: true, has_stuff: true, has_other_stuff: true },
|
|
416
|
+
2: { id: 2, name: "two", has_things: true, has_stuff: true, has_other_stuff: false },
|
|
417
|
+
});
|
|
418
|
+
expect(filter1.state.order).toEqual(["1", "2"]);
|
|
419
|
+
expect(filter2.state.objects).toEqual({
|
|
420
|
+
1: { id: 1, name: "one", has_things: true, has_stuff: true, has_other_stuff: true },
|
|
421
|
+
});
|
|
422
|
+
expect(filter2.state.order).toEqual(["1"]);
|
|
423
|
+
list.addListObject({ id: 3, name: "three", has_things: true, has_stuff: false, has_other_stuff: true });
|
|
424
|
+
await doAwaitNot({
|
|
425
|
+
obj: filter1.state,
|
|
426
|
+
prop: "running",
|
|
427
|
+
});
|
|
428
|
+
await doAwaitNot({
|
|
429
|
+
obj: filter2.state,
|
|
430
|
+
prop: "running",
|
|
431
|
+
});
|
|
432
|
+
expect(filter1.state.objects).toEqual({
|
|
433
|
+
1: { id: 1, name: "one", has_things: true, has_stuff: true, has_other_stuff: true },
|
|
434
|
+
2: { id: 2, name: "two", has_things: true, has_stuff: true, has_other_stuff: false },
|
|
435
|
+
});
|
|
436
|
+
expect(filter1.state.order).toEqual(["1", "2"]);
|
|
437
|
+
expect(filter2.state.objects).toEqual({
|
|
438
|
+
1: { id: 1, name: "one", has_things: true, has_stuff: true, has_other_stuff: true },
|
|
439
|
+
});
|
|
440
|
+
expect(filter2.state.order).toEqual(["1"]);
|
|
441
|
+
list.addListObject({ id: 4, name: "four", has_things: true, has_stuff: false, has_other_stuff: false });
|
|
442
|
+
await doAwaitNot({
|
|
443
|
+
obj: filter1.state,
|
|
444
|
+
prop: "running",
|
|
445
|
+
});
|
|
446
|
+
await doAwaitNot({
|
|
447
|
+
obj: filter2.state,
|
|
448
|
+
prop: "running",
|
|
449
|
+
});
|
|
450
|
+
expect(filter1.state.objects).toEqual({
|
|
451
|
+
1: { id: 1, name: "one", has_things: true, has_stuff: true, has_other_stuff: true },
|
|
452
|
+
2: { id: 2, name: "two", has_things: true, has_stuff: true, has_other_stuff: false },
|
|
453
|
+
});
|
|
454
|
+
expect(filter1.state.order).toEqual(["1", "2"]);
|
|
455
|
+
expect(filter2.state.objects).toEqual({
|
|
456
|
+
1: { id: 1, name: "one", has_things: true, has_stuff: true, has_other_stuff: true },
|
|
457
|
+
});
|
|
458
|
+
expect(filter2.state.order).toEqual(["1"]);
|
|
459
|
+
list.state.objects[1].has_stuff = false;
|
|
460
|
+
list.state.objects[2].has_other_stuff = true;
|
|
461
|
+
await doAwaitNot({
|
|
462
|
+
obj: filter1.state,
|
|
463
|
+
prop: "running",
|
|
464
|
+
});
|
|
465
|
+
await doAwaitNot({
|
|
466
|
+
obj: filter2.state,
|
|
467
|
+
prop: "running",
|
|
468
|
+
});
|
|
469
|
+
expect(filter1.state.objects).toEqual({
|
|
470
|
+
2: { id: 2, name: "two", has_things: true, has_stuff: true, has_other_stuff: true },
|
|
471
|
+
});
|
|
472
|
+
expect(filter1.state.order).toEqual(["2"]);
|
|
473
|
+
expect(filter2.state.objects).toEqual({
|
|
474
|
+
2: { id: 2, name: "two", has_things: true, has_stuff: true, has_other_stuff: true },
|
|
475
|
+
});
|
|
476
|
+
expect(filter2.state.order).toEqual(["2"]);
|
|
477
|
+
list.addListObject({ id: 5, name: "five", has_things: true, has_stuff: false, has_other_stuff: true });
|
|
478
|
+
list.addListObject({ id: 6, name: "six", has_things: true, has_stuff: true, has_other_stuff: false });
|
|
479
|
+
list.addListObject({ id: 7, name: "seven", has_things: false, has_stuff: true, has_other_stuff: true });
|
|
480
|
+
list.addListObject({ id: 8, name: "eight", has_things: true, has_stuff: true, has_other_stuff: true });
|
|
481
|
+
await doAwaitNot({
|
|
482
|
+
obj: filter1.state,
|
|
483
|
+
prop: "running",
|
|
484
|
+
});
|
|
485
|
+
await doAwaitNot({
|
|
486
|
+
obj: filter2.state,
|
|
487
|
+
prop: "running",
|
|
488
|
+
});
|
|
489
|
+
expect(filter1.state.objects).toEqual({
|
|
490
|
+
2: { id: 2, name: "two", has_things: true, has_stuff: true, has_other_stuff: true },
|
|
491
|
+
6: { id: 6, name: "six", has_things: true, has_stuff: true, has_other_stuff: false },
|
|
492
|
+
8: { id: 8, name: "eight", has_things: true, has_stuff: true, has_other_stuff: true },
|
|
493
|
+
});
|
|
494
|
+
expect(filter1.state.order).toEqual(["2", "6", "8"]);
|
|
495
|
+
expect(filter2.state.objects).toEqual({
|
|
496
|
+
2: { id: 2, name: "two", has_things: true, has_stuff: true, has_other_stuff: true },
|
|
497
|
+
8: { id: 8, name: "eight", has_things: true, has_stuff: true, has_other_stuff: true },
|
|
498
|
+
});
|
|
499
|
+
expect(filter2.state.order).toEqual(["2", "8"]);
|
|
500
|
+
});
|
|
331
501
|
});
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { doAwaitNot } from "../../../utils/index.js";
|
|
1
2
|
import { expectErrorToBeNull } from "../expectHelpers.js";
|
|
2
3
|
import flushPromises from "flush-promises";
|
|
3
4
|
import keyBy from "lodash-es/keyBy.js";
|
|
@@ -308,6 +309,7 @@ describe("use/listInstance.spec.js", function () {
|
|
|
308
309
|
expect(listInstance.state.errored).toBe(false);
|
|
309
310
|
expect(listInstance.state.loading).toBe(false);
|
|
310
311
|
expect({ ...listInstance.state.objects }).toEqual(crudListResolvedObjects2);
|
|
312
|
+
await nextTick();
|
|
311
313
|
await flushPromises();
|
|
312
314
|
expect(listInstance.state.order).toEqual(Object.keys(crudListResolvedObjects2));
|
|
313
315
|
expect(listInstance.state.objectsInOrder).toEqual(Object.values(crudListResolvedObjects2));
|
|
@@ -429,7 +431,7 @@ describe("use/listInstance.spec.js", function () {
|
|
|
429
431
|
expect(fakeId).toBeTruthy();
|
|
430
432
|
});
|
|
431
433
|
});
|
|
432
|
-
it("computes objectsInOrder and maintains state", () => {
|
|
434
|
+
it("computes objectsInOrder and maintains state", async () => {
|
|
433
435
|
const crudListResolvedPage3 = [
|
|
434
436
|
{
|
|
435
437
|
id: 3,
|
|
@@ -447,6 +449,7 @@ describe("use/listInstance.spec.js", function () {
|
|
|
447
449
|
name: "zxcv",
|
|
448
450
|
},
|
|
449
451
|
];
|
|
452
|
+
const crudListResolvedObjects3 = keyBy(crudListResolvedPage3, "id");
|
|
450
453
|
const addObject = {
|
|
451
454
|
id: 4,
|
|
452
455
|
__str__: "yuio",
|
|
@@ -467,12 +470,22 @@ describe("use/listInstance.spec.js", function () {
|
|
|
467
470
|
|
|
468
471
|
passedPageCallback(crudListResolvedPage3);
|
|
469
472
|
crudListResolve();
|
|
473
|
+
await doAwaitNot({
|
|
474
|
+
obj: listInstance.state,
|
|
475
|
+
prop: "loading",
|
|
476
|
+
});
|
|
477
|
+
expect(listInstance.state.objects).toEqual(crudListResolvedObjects3);
|
|
478
|
+
expect(listInstance.state.order).toEqual(crudListResolvedPage3.map((e) => e.id.toString()));
|
|
470
479
|
expect(listInstance.state.objectsInOrder).toEqual(crudListResolvedPage3);
|
|
471
480
|
listInstance.addListObject(addObject);
|
|
472
481
|
crudListResolvedPage3.push(addObject);
|
|
482
|
+
await nextTick();
|
|
483
|
+
expect(listInstance.state.order).toEqual(crudListResolvedPage3.map((e) => e.id.toString()));
|
|
473
484
|
expect(listInstance.state.objectsInOrder).toEqual(crudListResolvedPage3);
|
|
474
485
|
listInstance.deleteListObject(8);
|
|
475
486
|
crudListResolvedPage3.splice(1, 1);
|
|
487
|
+
await nextTick();
|
|
488
|
+
expect(listInstance.state.order).toEqual(crudListResolvedPage3.map((e) => e.id.toString()));
|
|
476
489
|
expect(listInstance.state.objectsInOrder).toEqual(crudListResolvedPage3);
|
|
477
490
|
});
|
|
478
491
|
});
|