@arrai-innovations/reactive-helpers 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/.circleci/config.yml +91 -0
  2. package/.commitlintrc.json +3 -0
  3. package/.editorconfig +5 -0
  4. package/.eslintignore +2 -0
  5. package/.eslintrc.js +26 -0
  6. package/.husky/commit-msg +2 -0
  7. package/.husky/pre-commit +2 -0
  8. package/.idea/encodings.xml +6 -0
  9. package/.idea/inspectionProfiles/Project_Default.xml +6 -0
  10. package/.idea/modules.xml +8 -0
  11. package/.idea/prettier.xml +8 -0
  12. package/.idea/reactive-helpers.iml +15 -0
  13. package/.lintstagedrc +11 -0
  14. package/.prettierignore +4 -0
  15. package/.prettierrc.js +5 -0
  16. package/README.md +598 -0
  17. package/babel.config.js +15 -0
  18. package/index.js +2 -0
  19. package/jest.config.mjs +27 -0
  20. package/package.json +59 -0
  21. package/tests/unit/.eslintrc.js +10 -0
  22. package/tests/unit/expectHelpers.js +6 -0
  23. package/tests/unit/mockOnUnmounted.js +9 -0
  24. package/tests/unit/use/index.spec.js +18 -0
  25. package/tests/unit/use/listFilter.spec.js +332 -0
  26. package/tests/unit/use/listInstance.spec.js +424 -0
  27. package/tests/unit/use/listRelated.spec.js +73 -0
  28. package/tests/unit/use/listSort.spec.js +220 -0
  29. package/tests/unit/use/listSubscription.spec.js +540 -0
  30. package/tests/unit/use/objectInstance.spec.js +897 -0
  31. package/tests/unit/use/objectSubscription.spec.js +671 -0
  32. package/tests/unit/use/search.spec.js +67 -0
  33. package/tests/unit/use/watches.spec.js +252 -0
  34. package/tests/unit/utils/assignReactiveObject.spec.js +127 -0
  35. package/tests/unit/utils/index.spec.js +18 -0
  36. package/tests/unit/utils/keyDiff.spec.js +67 -0
  37. package/tests/unit/utils/set.spec.js +68 -0
  38. package/tests/unit/utils/unrefAndToRawDeep.spec.js +170 -0
  39. package/use/index.js +8 -0
  40. package/use/listFilter.js +157 -0
  41. package/use/listInstance.js +144 -0
  42. package/use/listRelated.js +135 -0
  43. package/use/listSort.js +190 -0
  44. package/use/listSubscription.js +143 -0
  45. package/use/objectInstance.js +222 -0
  46. package/use/objectSubscription.js +188 -0
  47. package/use/search.js +104 -0
  48. package/utils/assignReactiveObject.js +62 -0
  49. package/utils/index.js +5 -0
  50. package/utils/keyDiff.js +10 -0
  51. package/utils/set.js +48 -0
  52. package/utils/unrefAndToRawDeep.js +49 -0
  53. package/utils/watches.js +170 -0
@@ -0,0 +1,220 @@
1
+ import { nextTick } from "vue";
2
+ import { doAwaitTimeout, unrefAndToRawDeep } from "../../../utils";
3
+
4
+ describe("use/useListSort", () => {
5
+ let listInstance,
6
+ orderByRules,
7
+ sortThrottleWait,
8
+ useListInstance,
9
+ useListInstances,
10
+ useListSort,
11
+ useListSorts,
12
+ setListSortDefaultOptions;
13
+ const contactsResolved = [
14
+ {
15
+ id: 15,
16
+ lexical_name: "one, contact",
17
+ organization: 42,
18
+ relatedObjects: {
19
+ organization: { id: 42, name: "org 42" },
20
+ },
21
+ },
22
+ {
23
+ id: 12,
24
+ lexical_name: "three, first contact",
25
+ organization: 51,
26
+ relatedObjects: {
27
+ organization: { id: 51, name: "area 51" },
28
+ },
29
+ },
30
+ {
31
+ id: 9,
32
+ lexical_name: "nine, number",
33
+ organization: 9,
34
+ relatedObjects: {
35
+ organization: { id: 9, name: "white album" },
36
+ },
37
+ },
38
+ ];
39
+ beforeEach(async () => {
40
+ const imported = await import("../../../use");
41
+ useListInstance = imported.useListInstance;
42
+ useListInstances = imported.useListInstances;
43
+ orderByRules = [
44
+ { key: "organization", desc: true, localeCompare: false },
45
+ { key: "lexical_name", desc: false, localeCompare: true },
46
+ ];
47
+ listInstance = useListInstance({
48
+ defaultRetrieveArgs: {
49
+ fields: ["id", "lexical_name", "organization", "relatedObjects"],
50
+ },
51
+ });
52
+ useListSort = imported.useListSort;
53
+ useListSorts = imported.useListSorts;
54
+ setListSortDefaultOptions = imported.setListSortDefaultOptions;
55
+ setListSortDefaultOptions({
56
+ sortThrottleWait: 0,
57
+ });
58
+ });
59
+
60
+ afterEach(() => jest.resetAllMocks());
61
+
62
+ it("generates initial values from inputs", () => {
63
+ const listSort = useListSort({ parentState: listInstance.state, orderByRules, sortThrottleWait });
64
+ expect(listSort.state.orderByRules).toEqual(orderByRules);
65
+ expect(listSort.state.order).toEqual([]);
66
+ expect(listSort.state.objectsInOrder).toEqual([]);
67
+ expect(listSort.state.sortCriteria).toEqual({});
68
+ expect(listSort.state.sortCriteriaWatches).toEqual({});
69
+ expect(listSort.state.orderByDesc).toEqual([true, false]);
70
+ });
71
+ describe("addSortCriteria and removeSortCriteria", () => {
72
+ it("triggers on watches updating state and sortCriteria", async () => {
73
+ const addObject = {
74
+ id: 35,
75
+ lexical_name: "six, JWST",
76
+ organization: 67,
77
+ relatedObjects: {
78
+ organization: { id: 67, name: "NASA" },
79
+ },
80
+ };
81
+ const testOrder1 = [];
82
+ const testOrder2 = ["35", "12", "15", "9"];
83
+ const testOrder3 = ["35", "15", "9"];
84
+
85
+ for (const contact of contactsResolved) {
86
+ listInstance.addListObject(contact);
87
+ }
88
+ const listSort = useListSort({ parentState: listInstance.state, orderByRules });
89
+ await nextTick();
90
+ expect(listSort.state.order).toEqual(testOrder1);
91
+ listInstance.addListObject(addObject);
92
+ await nextTick();
93
+ expect(listSort.state.order).toEqual(testOrder2);
94
+ listInstance.deleteListObject(12);
95
+ await nextTick();
96
+ expect(listSort.state.order).toEqual(testOrder3);
97
+ });
98
+ });
99
+ describe("sortWatch sifts various criteria", () => {
100
+ it("sorts with orderByObj.desc and x/yCriteria", async () => {
101
+ const testOrder1 = [];
102
+ const testOrder2 = ["9", "15", "12"];
103
+ const testOrder3 = ["12", "15", "9"];
104
+ const testOrder4 = ["15", "12", "9"];
105
+
106
+ for (const contact of contactsResolved) {
107
+ listInstance.addListObject(contact);
108
+ }
109
+
110
+ const listSort = useListSort({ parentState: listInstance.state, orderByRules, sortThrottleWait });
111
+ listSort.state.orderByRules.pop();
112
+ listSort.state.orderByRules.pop();
113
+ listSort.state.orderByRules.push({ key: "lexical_name", desc: false, localeCompare: true });
114
+ expect(listSort.state.order).toEqual(testOrder1);
115
+ await nextTick();
116
+ expect(listSort.state.order).toEqual(testOrder2);
117
+ listSort.state.orderByRules.pop();
118
+ listSort.state.orderByRules.push({ key: "organization", desc: true, localeCompare: true });
119
+ await nextTick();
120
+ expect(listSort.state.order).toEqual(testOrder3);
121
+ listSort.state.orderByRules.pop();
122
+ await nextTick();
123
+ expect(listSort.state.order).toEqual(testOrder4);
124
+ listSort.state.orderByRules.push({ key: "organization", desc: false, localeCompare: false });
125
+ await nextTick();
126
+ expect(listSort.state.order).toEqual(testOrder2);
127
+ });
128
+ });
129
+ it("useListSorts & useListInstances", async function () {
130
+ const fields = ["id", "__str__", "name"];
131
+ const orderByRules = [{ key: "name", desc: true, localeCompare: true }];
132
+ const listInstanceA = useListInstance({
133
+ crudArgs: { stream: "test_streamA" },
134
+ listArgs: { user: 1 },
135
+ retrieveArgs: {
136
+ fields,
137
+ },
138
+ });
139
+ const listInstanceB = useListInstance({
140
+ crudArgs: { stream: "test_streamB" },
141
+ listArgs: { user: 2 },
142
+ retrieveArgs: {
143
+ fields,
144
+ },
145
+ });
146
+ const listSortA = useListSort({
147
+ parentState: listInstanceA.state,
148
+ orderByRules,
149
+ });
150
+ const listSortB = useListSort({
151
+ parentState: listInstanceB.state,
152
+ orderByRules,
153
+ });
154
+ const listInstances = useListInstances({
155
+ A: {
156
+ crudArgs: { stream: "test_streamA" },
157
+ listArgs: { user: 1 },
158
+ retrieveArgs: {
159
+ fields,
160
+ },
161
+ },
162
+ B: {
163
+ crudArgs: { stream: "test_streamB" },
164
+ listArgs: { user: 2 },
165
+ retrieveArgs: {
166
+ fields,
167
+ },
168
+ },
169
+ });
170
+ const listSorts = useListSorts(
171
+ {
172
+ A: {
173
+ orderByRules,
174
+ },
175
+ B: {
176
+ orderByRules,
177
+ },
178
+ },
179
+ listInstances
180
+ );
181
+ expect(unrefAndToRawDeep(listSorts.A.parentState)).toEqual(unrefAndToRawDeep(listInstanceA.state));
182
+ expect(unrefAndToRawDeep(listSorts.B.parentState)).toEqual(unrefAndToRawDeep(listInstanceB.state));
183
+ expect(unrefAndToRawDeep(listSorts.A.state)).toEqual(unrefAndToRawDeep(listSortA.state));
184
+ expect(unrefAndToRawDeep(listSorts.B.state)).toEqual(unrefAndToRawDeep(listSortB.state));
185
+ });
186
+ describe("useListSort/sortThrottleWait", () => {
187
+ it("respects throttle time prior to triggering", async () => {
188
+ setListSortDefaultOptions({
189
+ sortThrottleWait: 200,
190
+ });
191
+ const addObject = {
192
+ id: 35,
193
+ lexical_name: "six, JWST",
194
+ organization: 67,
195
+ relatedObjects: {
196
+ organization: { id: 67, name: "NASA" },
197
+ },
198
+ };
199
+ for (const contact of contactsResolved) {
200
+ listInstance.addListObject(contact);
201
+ }
202
+
203
+ const testOrder1 = [];
204
+ const testOrder2 = ["35", "12", "15", "9"];
205
+ const testOrder3 = ["35", "15", "9"];
206
+
207
+ const listSort = useListSort({ parentState: listInstance.state, orderByRules, sortThrottleWait });
208
+ await nextTick();
209
+ expect(listSort.state.order).toEqual(testOrder1);
210
+ listInstance.addListObject(addObject);
211
+ await nextTick();
212
+ expect(listSort.state.order).toEqual(testOrder2);
213
+ listInstance.deleteListObject(12);
214
+ await nextTick();
215
+ expect(listSort.state.order).toEqual(testOrder2);
216
+ await doAwaitTimeout(200);
217
+ expect(listSort.state.order).toEqual(testOrder3);
218
+ });
219
+ });
220
+ });