@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.
Files changed (216) hide show
  1. package/package.json +20 -6
  2. package/types/config/listCrud.d.ts +153 -0
  3. package/types/config/listCrud.d.ts.map +1 -0
  4. package/types/config/objectCrud.d.ts +171 -0
  5. package/types/config/objectCrud.d.ts.map +1 -0
  6. package/types/index.d.ts +37 -0
  7. package/types/index.d.ts.map +1 -0
  8. package/types/tests/unit/config/listCrud.spec.d.ts +2 -0
  9. package/types/tests/unit/config/listCrud.spec.d.ts.map +1 -0
  10. package/types/tests/unit/config/objectCrud.spec.d.ts +2 -0
  11. package/types/tests/unit/config/objectCrud.spec.d.ts.map +1 -0
  12. package/types/tests/unit/crudPromise.d.ts +25 -0
  13. package/types/tests/unit/crudPromise.d.ts.map +1 -0
  14. package/types/tests/unit/expectHelpers.d.ts +2 -0
  15. package/types/tests/unit/expectHelpers.d.ts.map +1 -0
  16. package/types/tests/unit/mockOnUnmounted.d.ts +2 -0
  17. package/types/tests/unit/mockOnUnmounted.d.ts.map +1 -0
  18. package/types/tests/unit/poll.d.ts +2 -0
  19. package/types/tests/unit/poll.d.ts.map +1 -0
  20. package/types/tests/unit/use/cancellableIntent.spec.d.ts +2 -0
  21. package/types/tests/unit/use/cancellableIntent.spec.d.ts.map +1 -0
  22. package/types/tests/unit/use/listCalculated.spec.d.ts +2 -0
  23. package/types/tests/unit/use/listCalculated.spec.d.ts.map +1 -0
  24. package/types/tests/unit/use/listFilter.spec.d.ts +2 -0
  25. package/types/tests/unit/use/listFilter.spec.d.ts.map +1 -0
  26. package/types/tests/unit/use/listInstance.spec.d.ts +2 -0
  27. package/types/tests/unit/use/listInstance.spec.d.ts.map +1 -0
  28. package/types/tests/unit/use/listRelated.spec.d.ts +2 -0
  29. package/types/tests/unit/use/listRelated.spec.d.ts.map +1 -0
  30. package/types/tests/unit/use/listSearch.spec.d.ts +2 -0
  31. package/types/tests/unit/use/listSearch.spec.d.ts.map +1 -0
  32. package/types/tests/unit/use/listSort.spec.d.ts +2 -0
  33. package/types/tests/unit/use/listSort.spec.d.ts.map +1 -0
  34. package/types/tests/unit/use/listSubscription.spec.d.ts +2 -0
  35. package/types/tests/unit/use/listSubscription.spec.d.ts.map +1 -0
  36. package/types/tests/unit/use/loadingError.spec.d.ts +2 -0
  37. package/types/tests/unit/use/loadingError.spec.d.ts.map +1 -0
  38. package/types/tests/unit/use/objectInstance.spec.d.ts +2 -0
  39. package/types/tests/unit/use/objectInstance.spec.d.ts.map +1 -0
  40. package/types/tests/unit/use/objectSubscription.spec.d.ts +2 -0
  41. package/types/tests/unit/use/objectSubscription.spec.d.ts.map +1 -0
  42. package/types/tests/unit/use/proxyLoadingError.spec.d.ts +2 -0
  43. package/types/tests/unit/use/proxyLoadingError.spec.d.ts.map +1 -0
  44. package/types/tests/unit/use/search.spec.d.ts +2 -0
  45. package/types/tests/unit/use/search.spec.d.ts.map +1 -0
  46. package/types/tests/unit/utils/assignReactiveObject.spec.d.ts +2 -0
  47. package/types/tests/unit/utils/assignReactiveObject.spec.d.ts.map +1 -0
  48. package/types/tests/unit/utils/classes.spec.d.ts +2 -0
  49. package/types/tests/unit/utils/classes.spec.d.ts.map +1 -0
  50. package/types/tests/unit/utils/deleteKey.spec.d.ts +2 -0
  51. package/types/tests/unit/utils/deleteKey.spec.d.ts.map +1 -0
  52. package/types/tests/unit/utils/flattenPaths.spec.d.ts +2 -0
  53. package/types/tests/unit/utils/flattenPaths.spec.d.ts.map +1 -0
  54. package/types/tests/unit/utils/keyDiff.spec.d.ts +2 -0
  55. package/types/tests/unit/utils/keyDiff.spec.d.ts.map +1 -0
  56. package/types/tests/unit/utils/set.spec.d.ts +2 -0
  57. package/types/tests/unit/utils/set.spec.d.ts.map +1 -0
  58. package/types/tests/unit/utils/watches.spec.d.ts +2 -0
  59. package/types/tests/unit/utils/watches.spec.d.ts.map +1 -0
  60. package/types/use/cancellableIntent.d.ts +129 -0
  61. package/types/use/cancellableIntent.d.ts.map +1 -0
  62. package/types/use/combineClasses.d.ts +43 -0
  63. package/types/use/combineClasses.d.ts.map +1 -0
  64. package/types/use/list.d.ts +176 -0
  65. package/types/use/list.d.ts.map +1 -0
  66. package/types/use/listCalculated.d.ts +192 -0
  67. package/types/use/listCalculated.d.ts.map +1 -0
  68. package/types/use/listFilter.d.ts +203 -0
  69. package/types/use/listFilter.d.ts.map +1 -0
  70. package/types/use/listInstance.d.ts +357 -0
  71. package/types/use/listInstance.d.ts.map +1 -0
  72. package/types/use/listKeys.d.ts +15 -0
  73. package/types/use/listKeys.d.ts.map +1 -0
  74. package/types/use/listRelated.d.ts +239 -0
  75. package/types/use/listRelated.d.ts.map +1 -0
  76. package/types/use/listSearch.d.ts +298 -0
  77. package/types/use/listSearch.d.ts.map +1 -0
  78. package/types/use/listSort.d.ts +235 -0
  79. package/types/use/listSort.d.ts.map +1 -0
  80. package/types/use/listSubscription.d.ts +218 -0
  81. package/types/use/listSubscription.d.ts.map +1 -0
  82. package/types/use/loadingError.d.ts +81 -0
  83. package/types/use/loadingError.d.ts.map +1 -0
  84. package/types/use/object.d.ts +67 -0
  85. package/types/use/object.d.ts.map +1 -0
  86. package/types/use/objectCalculated.d.ts +215 -0
  87. package/types/use/objectCalculated.d.ts.map +1 -0
  88. package/types/use/objectInstance.d.ts +289 -0
  89. package/types/use/objectInstance.d.ts.map +1 -0
  90. package/types/use/objectRelated.d.ts +264 -0
  91. package/types/use/objectRelated.d.ts.map +1 -0
  92. package/types/use/objectSubscription.d.ts +249 -0
  93. package/types/use/objectSubscription.d.ts.map +1 -0
  94. package/types/use/paginatedListInstance.d.ts +51 -0
  95. package/types/use/paginatedListInstance.d.ts.map +1 -0
  96. package/types/use/proxyLoadingError.d.ts +36 -0
  97. package/types/use/proxyLoadingError.d.ts.map +1 -0
  98. package/types/use/search.d.ts +144 -0
  99. package/types/use/search.d.ts.map +1 -0
  100. package/types/use/watchesRunning.d.ts +63 -0
  101. package/types/use/watchesRunning.d.ts.map +1 -0
  102. package/types/utils/assignReactiveObject.d.ts +115 -0
  103. package/types/utils/assignReactiveObject.d.ts.map +1 -0
  104. package/types/utils/classes.d.ts +22 -0
  105. package/types/utils/classes.d.ts.map +1 -0
  106. package/types/utils/compact.d.ts +4 -0
  107. package/types/utils/compact.d.ts.map +1 -0
  108. package/types/utils/deleteKey.d.ts +16 -0
  109. package/types/utils/deleteKey.d.ts.map +1 -0
  110. package/types/utils/flattenPaths.d.ts +23 -0
  111. package/types/utils/flattenPaths.d.ts.map +1 -0
  112. package/types/utils/getFakePk.d.ts +13 -0
  113. package/types/utils/getFakePk.d.ts.map +1 -0
  114. package/types/utils/keepAliveTry.d.ts +15 -0
  115. package/types/utils/keepAliveTry.d.ts.map +1 -0
  116. package/types/utils/keyDiff.d.ts +63 -0
  117. package/types/utils/keyDiff.d.ts.map +1 -0
  118. package/types/utils/loadingCombine.d.ts +9 -0
  119. package/types/utils/loadingCombine.d.ts.map +1 -0
  120. package/types/utils/proxyRunning.d.ts +9 -0
  121. package/types/utils/proxyRunning.d.ts.map +1 -0
  122. package/types/utils/relatedCalculatedHelpers.d.ts +5 -0
  123. package/types/utils/relatedCalculatedHelpers.d.ts.map +1 -0
  124. package/types/utils/set.d.ts +7 -0
  125. package/types/utils/set.d.ts.map +1 -0
  126. package/types/utils/transformWalk.d.ts +2 -0
  127. package/types/utils/transformWalk.d.ts.map +1 -0
  128. package/types/utils/watches.d.ts +155 -0
  129. package/types/utils/watches.d.ts.map +1 -0
  130. package/use/loadingError.js +1 -1
  131. package/.circleci/config.yml +0 -88
  132. package/.commitlintrc.json +0 -25
  133. package/.editorconfig +0 -5
  134. package/.husky/commit-msg +0 -3
  135. package/.husky/pre-commit +0 -3
  136. package/.idea/codeStyles/codeStyleConfig.xml +0 -5
  137. package/.idea/encodings.xml +0 -6
  138. package/.idea/inspectionProfiles/Project_Default.xml +0 -7
  139. package/.idea/modules.xml +0 -8
  140. package/.idea/prettier.xml +0 -8
  141. package/.idea/reactive-helpers.iml +0 -15
  142. package/.prettierignore +0 -5
  143. package/.prettierrc.cjs +0 -5
  144. package/.vscode/extensions.json +0 -3
  145. package/docs/README.md +0 -45
  146. package/docs/config/listCrud.md +0 -422
  147. package/docs/config/objectCrud.md +0 -427
  148. package/docs/use/cancellableIntent.md +0 -165
  149. package/docs/use/combineClasses.md +0 -62
  150. package/docs/use/list.md +0 -361
  151. package/docs/use/listCalculated.md +0 -794
  152. package/docs/use/listFilter.md +0 -897
  153. package/docs/use/listInstance.md +0 -559
  154. package/docs/use/listKeys.md +0 -91
  155. package/docs/use/listRelated.md +0 -706
  156. package/docs/use/listSearch.md +0 -903
  157. package/docs/use/listSort.md +0 -1171
  158. package/docs/use/listSubscription.md +0 -393
  159. package/docs/use/loadingError.md +0 -115
  160. package/docs/use/object.md +0 -668
  161. package/docs/use/objectCalculated.md +0 -741
  162. package/docs/use/objectInstance.md +0 -685
  163. package/docs/use/objectRelated.md +0 -723
  164. package/docs/use/objectSubscription.md +0 -538
  165. package/docs/use/paginatedListInstance.md +0 -155
  166. package/docs/use/proxyLoadingError.md +0 -51
  167. package/docs/use/search.md +0 -321
  168. package/docs/use/watchesRunning.md +0 -84
  169. package/docs/utils/assignReactiveObject.md +0 -413
  170. package/docs/utils/classes.md +0 -139
  171. package/docs/utils/compact.md +0 -67
  172. package/docs/utils/deleteKey.md +0 -61
  173. package/docs/utils/flattenPaths.md +0 -53
  174. package/docs/utils/getFakePk.md +0 -39
  175. package/docs/utils/keepAliveTry.md +0 -59
  176. package/docs/utils/keyDiff.md +0 -117
  177. package/docs/utils/loadingCombine.md +0 -30
  178. package/docs/utils/proxyRunning.md +0 -39
  179. package/docs/utils/relatedCalculatedHelpers.md +0 -93
  180. package/docs/utils/set.md +0 -223
  181. package/docs/utils/transformWalk.md +0 -68
  182. package/docs/utils/watches.md +0 -489
  183. package/eslint.config.js +0 -125
  184. package/lint-staged.config.js +0 -10
  185. package/makeTypeDoc.sh +0 -25
  186. package/make_type_doc.js +0 -59
  187. package/tests/unit/config/listCrud.spec.js +0 -114
  188. package/tests/unit/config/objectCrud.spec.js +0 -160
  189. package/tests/unit/crudPromise.js +0 -40
  190. package/tests/unit/expectHelpers.js +0 -6
  191. package/tests/unit/mockOnUnmounted.js +0 -9
  192. package/tests/unit/poll.js +0 -50
  193. package/tests/unit/use/cancellableIntent.spec.js +0 -152
  194. package/tests/unit/use/listCalculated.spec.js +0 -135
  195. package/tests/unit/use/listFilter.spec.js +0 -484
  196. package/tests/unit/use/listInstance.spec.js +0 -1166
  197. package/tests/unit/use/listRelated.spec.js +0 -260
  198. package/tests/unit/use/listSearch.spec.js +0 -586
  199. package/tests/unit/use/listSort.spec.js +0 -418
  200. package/tests/unit/use/listSubscription.spec.js +0 -845
  201. package/tests/unit/use/loadingError.spec.js +0 -43
  202. package/tests/unit/use/objectInstance.spec.js +0 -1525
  203. package/tests/unit/use/objectSubscription.spec.js +0 -849
  204. package/tests/unit/use/proxyLoadingError.spec.js +0 -64
  205. package/tests/unit/use/search.spec.js +0 -248
  206. package/tests/unit/utils/assignReactiveObject.spec.js +0 -593
  207. package/tests/unit/utils/classes.spec.js +0 -427
  208. package/tests/unit/utils/deleteKey.spec.js +0 -265
  209. package/tests/unit/utils/flattenPaths.spec.js +0 -126
  210. package/tests/unit/utils/keyDiff.spec.js +0 -110
  211. package/tests/unit/utils/set.spec.js +0 -81
  212. package/tests/unit/utils/watches.spec.js +0 -276
  213. package/tsconfig.json +0 -19
  214. package/typedoc-local-plugins/customize-output.js +0 -13
  215. package/typedoc.json +0 -11
  216. package/vitest.config.js +0 -11
@@ -1,845 +0,0 @@
1
- import { doAwaitNot } from "../../../utils/watches.js";
2
- import { CancellableResolvable } from "../crudPromise.js";
3
- import { poll } from "../poll.js";
4
- import flushPromises from "flush-promises";
5
- import { nextTick, reactive } from "vue";
6
- import { deepUnref } from "vue-deepunref";
7
-
8
- describe("use/listSubscription.spec.js", function () {
9
- let useListSubscription,
10
- ListSubscriptionError,
11
- useListSubscriptions,
12
- useListInstance,
13
- useListInstances,
14
- crudList,
15
- crudListResolvable = [],
16
- crudSubscribe,
17
- crudSubscribeResolvable = [],
18
- passedSubscriptionEventCallback,
19
- warnMock;
20
- beforeEach(async () => {
21
- warnMock = vi.spyOn(console, "warn").mockImplementation(() => undefined);
22
- crudListResolvable = [];
23
- crudSubscribeResolvable = [];
24
- crudListResolvable.push(new CancellableResolvable());
25
- crudSubscribeResolvable.push(new CancellableResolvable());
26
- const listCrudModule = await import("../../../config/listCrud.js");
27
- const listInstanceModule = await import("../../../use/listInstance.js");
28
- crudList = vi
29
- .fn()
30
- .mockImplementationOnce(() => crudListResolvable[0].promise)
31
- .mockImplementation(() => {
32
- const newResolvable = new CancellableResolvable();
33
- crudListResolvable.push(newResolvable);
34
- return newResolvable.promise;
35
- });
36
- const listSubscriptionModule = await import("../../../use/listSubscription.js");
37
- crudSubscribe = vi
38
- .fn()
39
- .mockImplementationOnce(({ subscriptionEventCallback }) => {
40
- // this function cannot be async, or the resulting promise will lose its .cancel() method
41
- passedSubscriptionEventCallback = subscriptionEventCallback;
42
- return crudSubscribeResolvable[0].promise;
43
- })
44
- .mockImplementation(({ subscriptionEventCallback }) => {
45
- // this function cannot be async, or the resulting promise will lose its .cancel() method
46
- const newResolvable = new CancellableResolvable();
47
- crudSubscribeResolvable.push(newResolvable);
48
- passedSubscriptionEventCallback = subscriptionEventCallback;
49
- return newResolvable.promise;
50
- });
51
- listCrudModule.setListCrud({
52
- list: crudList,
53
- subscribe: crudSubscribe,
54
- args: { stream: "test_stream" },
55
- });
56
- useListInstance = listInstanceModule.useListInstance;
57
- useListInstances = listInstanceModule.useListInstances;
58
-
59
- useListSubscription = listSubscriptionModule.useListSubscription;
60
- ListSubscriptionError = listSubscriptionModule.ListSubscriptionError;
61
- useListSubscriptions = listSubscriptionModule.useListSubscriptions;
62
- });
63
- afterEach(function () {
64
- vi.resetAllMocks();
65
- });
66
- const fields = ["id", "__str__", "name"];
67
- describe("lifecycle", function () {
68
- it("success", async function () {
69
- const listArgs = reactive({
70
- user: 1,
71
- });
72
- const retrieveArgs = reactive({
73
- fields: fields,
74
- });
75
- const listSubscription = useListSubscription({
76
- props: {
77
- pkKey: "id",
78
- listArgs,
79
- retrieveArgs,
80
- },
81
- keepOldPages: false,
82
- clearListOnListIntentTriggered: false,
83
- });
84
- listSubscription.subscribe();
85
- await nextTick();
86
- await flushPromises();
87
- expect(crudSubscribe).toHaveBeenCalledWith({
88
- crudArgs: { stream: "test_stream" },
89
- pkKey: "id",
90
- listArgs: { user: 1 },
91
- retrieveArgs: { fields: fields },
92
- subscriptionEventCallback: expect.any(Function),
93
- });
94
- expect(crudSubscribe).toHaveBeenCalledTimes(1);
95
- expect(listSubscription.state.subscribed).toBe(true);
96
-
97
- crudListResolvable[0].resolve();
98
- crudSubscribeResolvable[0].resolve();
99
- await poll(() => listSubscription.state.subscribed);
100
-
101
- passedSubscriptionEventCallback(
102
- {
103
- id: 1,
104
- __str__: "qwer",
105
- name: "qwer",
106
- },
107
- "create"
108
- );
109
-
110
- expect(listSubscription.listInstance.state.objects).toEqual({
111
- 1: {
112
- id: 1,
113
- __str__: "qwer",
114
- name: "qwer",
115
- },
116
- });
117
-
118
- passedSubscriptionEventCallback(
119
- {
120
- id: 1,
121
- __str__: "qwert",
122
- fame: "qwert",
123
- },
124
- "update"
125
- );
126
-
127
- expect(listSubscription.listInstance.state.objects).toEqual({
128
- 1: {
129
- id: 1,
130
- __str__: "qwert",
131
- fame: "qwert",
132
- },
133
- });
134
-
135
- passedSubscriptionEventCallback(1, "delete");
136
-
137
- expect(listSubscription.listInstance.state.objects).toEqual({});
138
- expect(listSubscription.state.subscribed).toBe(true);
139
-
140
- const returnValue = await listSubscription.unsubscribe();
141
- expect(listSubscription.state.intendToSubscribe).toBe(false);
142
- expect(listSubscription.state.intendToList).toBe(false);
143
- expect(crudListResolvable[0].promise.cancel).toHaveBeenCalledTimes(0);
144
- crudSubscribeResolvable[0].cancel.resolve(true);
145
- await doAwaitNot({
146
- obj: listSubscription.subscribeIntent.state,
147
- prop: "active",
148
- });
149
- expect(crudSubscribeResolvable[0].promise.cancel).toHaveBeenCalledTimes(1);
150
- expect(returnValue).toBe(true);
151
-
152
- expect(listSubscription.state.subscribed).toBe(false);
153
- });
154
- it("missing data", async function () {
155
- const listArgs = reactive({
156
- user: 1,
157
- });
158
- const retrieveArgs = reactive({
159
- fields: fields,
160
- });
161
- const listSubscription = useListSubscription({
162
- props: { pkKey: "id", listArgs, retrieveArgs },
163
- keepOldPages: false,
164
- clearListOnListIntentTriggered: false,
165
- });
166
- listSubscription.subscribe();
167
- crudSubscribeResolvable[0].resolve();
168
- await poll(() => listSubscription.state.subscribed);
169
- expect(crudSubscribe).toHaveBeenCalledWith({
170
- crudArgs: { stream: "test_stream" },
171
- pkKey: "id",
172
- listArgs: { user: 1 },
173
- retrieveArgs: { fields: fields },
174
- subscriptionEventCallback: expect.any(Function),
175
- });
176
- expect(crudSubscribe).toHaveBeenCalledTimes(1);
177
-
178
- expect(() => passedSubscriptionEventCallback({}, "create")).toThrow(ListSubscriptionError);
179
- expect(() => passedSubscriptionEventCallback({}, "create")).toThrow(
180
- "got update with no data ({}), action: create"
181
- );
182
-
183
- expect(() =>
184
- passedSubscriptionEventCallback(
185
- {
186
- id: 1,
187
- __str__: "qwer",
188
- name: "qwer",
189
- },
190
- "freate"
191
- )
192
- ).toThrow(ListSubscriptionError);
193
- expect(() =>
194
- passedSubscriptionEventCallback(
195
- {
196
- id: 1,
197
- __str__: "qwer",
198
- name: "qwer",
199
- },
200
- "freate"
201
- )
202
- ).toThrow("got update for unknown action: freate\n{ id: 1, __str__: 'qwer', name: 'qwer' }");
203
-
204
- expect(() =>
205
- passedSubscriptionEventCallback(
206
- {
207
- __str__: "qwer",
208
- name: "qwer",
209
- },
210
- "create"
211
- )
212
- ).toThrow(ListSubscriptionError);
213
- expect(() =>
214
- passedSubscriptionEventCallback(
215
- {
216
- __str__: "qwer",
217
- name: "qwer",
218
- },
219
- "create"
220
- )
221
- ).toThrow("addFromSubscription: data missing pk(id).\n{ __str__: 'qwer', name: 'qwer' }");
222
-
223
- passedSubscriptionEventCallback(
224
- {
225
- id: 1,
226
- __str__: "qwer",
227
- name: "qwer",
228
- },
229
- "create"
230
- );
231
- passedSubscriptionEventCallback(
232
- {
233
- id: 1,
234
- __str__: "qwer",
235
- name: "qwer",
236
- },
237
- "create"
238
- );
239
- expect(warnMock).toHaveBeenCalledWith("addFromSubscription: add for pk(id) already in objects (1).");
240
-
241
- expect(listSubscription.listInstance.state.objects).toEqual({
242
- 1: {
243
- id: 1,
244
- __str__: "qwer",
245
- name: "qwer",
246
- },
247
- });
248
-
249
- passedSubscriptionEventCallback(
250
- {
251
- id: 2,
252
- __str__: "qwer",
253
- name: "qwer",
254
- },
255
- "update"
256
- );
257
- expect(warnMock).toHaveBeenCalledWith("updateFromSubscription: update for pk(id) not in objects (2).");
258
-
259
- expect(() =>
260
- passedSubscriptionEventCallback(
261
- {
262
- __str__: "qwer",
263
- name: "qwer",
264
- },
265
- "update"
266
- )
267
- ).toThrow(ListSubscriptionError);
268
- expect(() =>
269
- passedSubscriptionEventCallback(
270
- {
271
- __str__: "qwer",
272
- name: "qwer",
273
- },
274
- "update"
275
- )
276
- ).toThrow("updateFromSubscription: data missing pk(id).\n{ __str__: 'qwer', name: 'qwer' }");
277
-
278
- passedSubscriptionEventCallback(
279
- {
280
- __str__: "qwer",
281
- name: "qwer",
282
- },
283
- "delete"
284
- );
285
- expect(warnMock).toHaveBeenCalledWith(
286
- "deleteFromSubscription: delete for pk(id) not in objects ({ __str__: 'qwer', name: 'qwer' })."
287
- );
288
-
289
- passedSubscriptionEventCallback(2, "delete");
290
- expect(warnMock).toHaveBeenCalledWith("deleteFromSubscription: delete for pk(id) not in objects (2).");
291
-
292
- await poll(() => listSubscription.state.subscribed);
293
- const unsubscribe = listSubscription.unsubscribe();
294
- crudSubscribeResolvable[0].cancel.resolve(true);
295
- const returnValue = await unsubscribe;
296
- await poll(() => !listSubscription.state.subscribed);
297
- expect(crudSubscribeResolvable[0].promise.cancel).toHaveBeenCalledTimes(1);
298
- expect(returnValue).toBe(true);
299
- expect(listSubscription.state.subscribed).toBe(false);
300
- });
301
- it("unsubscribe false", async function () {
302
- const listArgs = reactive({
303
- user: 1,
304
- });
305
- const retrieveArgs = reactive({
306
- fields: fields,
307
- });
308
- const listSubscription = useListSubscription({
309
- props: {
310
- pkKey: "id",
311
- listArgs,
312
- retrieveArgs,
313
- },
314
- keepOldPages: false,
315
- clearListOnListIntentTriggered: false,
316
- });
317
- expect(listSubscription.unsubscribe()).toBe(false);
318
- listSubscription.subscribe();
319
- crudSubscribeResolvable[0].resolve();
320
- await poll(() => listSubscription.state.subscribed);
321
- expect(crudSubscribe).toHaveBeenCalledWith({
322
- crudArgs: { stream: "test_stream" },
323
- pkKey: "id",
324
- listArgs: { user: 1 },
325
- retrieveArgs: { fields: fields },
326
- subscriptionEventCallback: expect.any(Function),
327
- });
328
- expect(crudSubscribe).toHaveBeenCalledTimes(1);
329
-
330
- const unsubscribePromise = listSubscription.unsubscribe();
331
- crudSubscribeResolvable[0].cancel.resolve(true);
332
- const returnValue = await unsubscribePromise;
333
- await poll(() => !listSubscription.state.subscribed);
334
- expect(crudSubscribeResolvable[0].promise.cancel).toHaveBeenCalledTimes(1);
335
- expect(returnValue).toBe(true);
336
- expect(listSubscription.state.subscribed).toBe(false);
337
- expect(listSubscription.unsubscribe()).toBe(false);
338
- });
339
- it("just unsubscribe", async function () {
340
- crudSubscribeResolvable[0].promise.cancel.mockImplementation(() => Promise.resolve(false));
341
- const listArgs = reactive({
342
- user: 1,
343
- });
344
- const retrieveArgs = reactive({
345
- fields: fields,
346
- });
347
- const listSubscriptionProps = reactive({
348
- pkKey: "id",
349
- listArgs,
350
- retrieveArgs,
351
- });
352
- const listSubscription = useListSubscription({
353
- props: listSubscriptionProps,
354
- keepOldPages: false,
355
- clearListOnListIntentTriggered: false,
356
- });
357
-
358
- const returnValue = await listSubscription.unsubscribe();
359
- expect(crudSubscribeResolvable[0].promise.cancel).toHaveBeenCalledTimes(0);
360
- expect(returnValue).toBe(false);
361
- expect(listSubscription.state.subscribed).toBeUndefined();
362
- });
363
- it("double subscribe", async function () {
364
- crudSubscribeResolvable[0].promise.cancel.mockImplementation(() => Promise.resolve(true));
365
- const listArgs = reactive({
366
- user: 1,
367
- });
368
- const retrieveArgs = reactive({
369
- fields: fields,
370
- });
371
- const listSubscription = useListSubscription({
372
- props: {
373
- pkKey: "id",
374
- listArgs,
375
- retrieveArgs,
376
- },
377
- keepOldPages: false,
378
- clearListOnListIntentTriggered: false,
379
- });
380
- const firstReturnValue = listSubscription.subscribe();
381
- const secondReturnValue = listSubscription.subscribe();
382
- await nextTick();
383
- await flushPromises();
384
- expect(crudSubscribe).toHaveBeenCalledWith({
385
- crudArgs: { stream: "test_stream" },
386
- pkKey: "id",
387
- listArgs: { user: 1 },
388
- retrieveArgs: { fields: fields },
389
- subscriptionEventCallback: expect.any(Function),
390
- });
391
- expect(crudSubscribe).toHaveBeenCalledTimes(1);
392
-
393
- expect(firstReturnValue).toBe(true);
394
- expect(secondReturnValue).toBe(false);
395
-
396
- const unsubscribePromise = listSubscription.unsubscribe();
397
- crudSubscribeResolvable[0].cancel.resolve(true);
398
- const returnValue = await unsubscribePromise;
399
- await poll(() => !listSubscription.state.subscribed);
400
- expect(crudSubscribeResolvable[0].promise.cancel).toHaveBeenCalledTimes(1);
401
- expect(returnValue).toBe(true);
402
- expect(listSubscription.state.subscribed).toBe(false);
403
- });
404
- it("manual list instance", async function () {
405
- crudSubscribeResolvable[0].promise.cancel.mockImplementation(() => Promise.resolve(true));
406
- const listArgs = reactive({
407
- user: 1,
408
- });
409
- const retrieveArgs = reactive({
410
- fields: fields,
411
- });
412
- const listInstance = useListInstance({
413
- props: { pkKey: "id", listArgs, retrieveArgs },
414
- keepOldPages: false,
415
- });
416
- const listSubscription = useListSubscription({
417
- listInstance,
418
- clearListOnListIntentTriggered: false,
419
- });
420
- expect(listSubscription.listInstance).toBe(listInstance);
421
- listSubscription.subscribe();
422
- await nextTick();
423
- await flushPromises();
424
- expect(crudSubscribe).toHaveBeenCalledWith({
425
- crudArgs: { stream: "test_stream" },
426
- pkKey: "id",
427
- listArgs: { user: 1 },
428
- retrieveArgs: { fields: fields },
429
- subscriptionEventCallback: expect.any(Function),
430
- });
431
- expect(crudSubscribe).toHaveBeenCalledTimes(1);
432
-
433
- passedSubscriptionEventCallback(
434
- {
435
- id: 1,
436
- __str__: "qwer",
437
- name: "qwer",
438
- },
439
- "create"
440
- );
441
-
442
- expect(listInstance.state.objects).toEqual({
443
- 1: {
444
- id: 1,
445
- __str__: "qwer",
446
- name: "qwer",
447
- },
448
- });
449
-
450
- passedSubscriptionEventCallback(
451
- {
452
- id: 1,
453
- __str__: "qwert",
454
- fame: "qwert",
455
- },
456
- "update"
457
- );
458
-
459
- expect(listInstance.state.objects).toEqual({
460
- 1: {
461
- id: 1,
462
- __str__: "qwert",
463
- fame: "qwert",
464
- },
465
- });
466
-
467
- passedSubscriptionEventCallback(1, "delete");
468
-
469
- expect(listInstance.state.objects).toEqual({});
470
-
471
- const unsubscribePromise = listSubscription.unsubscribe();
472
- crudSubscribeResolvable[0].cancel.resolve(true);
473
- const returnValue = await unsubscribePromise;
474
- await poll(() => !listSubscription.state.subscribed);
475
- expect(crudSubscribeResolvable[0].promise.cancel).toHaveBeenCalledTimes(1);
476
- expect(returnValue).toBe(true);
477
- expect(listSubscription.state.subscribed).toBe(false);
478
- });
479
- it("subscribe resubscribes when listArgs or retrieveArgs change", async function () {
480
- crudSubscribeResolvable[0].promise.cancel.mockImplementation(() => Promise.resolve(true));
481
- const listArgs = reactive({
482
- user: 1,
483
- });
484
- const retrieveArgs = reactive({
485
- fields: fields,
486
- });
487
- const listSubscription = useListSubscription({
488
- props: reactive({
489
- pkKey: "id",
490
- listArgs,
491
- retrieveArgs,
492
- }),
493
- keepOldPages: false,
494
- clearListOnListIntentTriggered: false,
495
- });
496
- listSubscription.subscribe();
497
- await poll(() => listSubscription.state.subscribed);
498
- expect(crudSubscribe).toHaveBeenCalledWith({
499
- crudArgs: { stream: "test_stream" },
500
- pkKey: "id",
501
- listArgs: { user: 1 },
502
- retrieveArgs: { fields: fields },
503
- subscriptionEventCallback: expect.any(Function),
504
- });
505
- expect(crudSubscribe).toHaveBeenCalledTimes(1);
506
- expect(listSubscription.state.subscribed).toBe(true);
507
- expect(listSubscription.state.intendToSubscribe).toBe(true);
508
- expect(listSubscription.state.intendToList).toBe(true);
509
- await crudSubscribeResolvable[0].resolve();
510
- await crudListResolvable[0].resolve();
511
- await poll(() => listSubscription.state.subscribed);
512
- listArgs.user = 2;
513
- retrieveArgs.fields = ["name"];
514
- await nextTick();
515
- await poll(() => crudSubscribeResolvable[0].promise.cancel.mock.calls.length === 1);
516
- await crudSubscribeResolvable[0].cancel.resolve(true);
517
- await poll(() => crudListResolvable.length === 2);
518
- await crudListResolvable[1].resolve();
519
- await poll(() => crudSubscribeResolvable.length === 2);
520
- await crudSubscribeResolvable[1].resolve();
521
- expect(crudSubscribe).toHaveBeenCalledWith({
522
- crudArgs: { stream: "test_stream" },
523
- listArgs: { user: 2 },
524
- pkKey: "id",
525
- retrieveArgs: { fields: ["name"] },
526
- subscriptionEventCallback: expect.any(Function),
527
- });
528
- expect(crudSubscribeResolvable.length).toBe(2);
529
- await crudSubscribeResolvable[1].resolve();
530
- await crudListResolvable[1].resolve();
531
- await poll(() => listSubscription.state.subscribed);
532
- expect(crudSubscribeResolvable[0].promise.cancel).toHaveBeenCalledTimes(1);
533
-
534
- expect(crudSubscribe).toHaveBeenCalledTimes(2);
535
-
536
- const unsubscribePromise = listSubscription.unsubscribe();
537
- crudSubscribeResolvable[1].cancel.resolve(true);
538
- const returnValue = await unsubscribePromise;
539
- expect(returnValue).toBe(true);
540
- await poll(() => !listSubscription.state.subscribed);
541
- expect(crudSubscribeResolvable[1].promise.cancel).toHaveBeenCalledTimes(1);
542
- expect(listSubscription.state.subscribed).toBe(false);
543
- });
544
- });
545
- describe("useListSubscription", function () {
546
- it("throw error when missing list instance and props", async function () {
547
- expect(() => useListSubscription({})).toThrow(
548
- "useListSubscription should be passed listInstance or props and functions."
549
- );
550
- });
551
- it("throw error when both listInstance and props passed in", async function () {
552
- crudSubscribeResolvable[0].promise.cancel.mockImplementation(() => Promise.resolve(true));
553
- const listArgs = reactive({
554
- user: 1,
555
- });
556
- const retrieveArgs = reactive({
557
- fields: fields,
558
- });
559
- const listInstance = useListInstance({
560
- props: { pkKey: "id", listArgs, retrieveArgs },
561
- keepOldPages: false,
562
- });
563
- const props = { pkKey: "id", listArgs, retrieveArgs };
564
- expect(() => useListSubscription({ listInstance, props })).toThrow(
565
- "useListSubscription should be passed listInstance or props and functions, not both."
566
- );
567
- });
568
- it("throw error when missing clearListOnListIntentTriggered", async function () {
569
- crudSubscribeResolvable[0].promise.cancel.mockImplementation(() => Promise.resolve(true));
570
- const listArgs = reactive({
571
- user: 1,
572
- });
573
- const retrieveArgs = reactive({
574
- fields: fields,
575
- });
576
- const listInstance = useListInstance({
577
- props: { pkKey: "id", listArgs, retrieveArgs },
578
- keepOldPages: false,
579
- });
580
- expect(() => useListSubscription({ listInstance })).toThrow(
581
- "useListSubscription should be passed clearListOnListIntentTriggered."
582
- );
583
- });
584
- it("throw error when missing keepOldPages and instance", async function () {
585
- crudSubscribeResolvable[0].promise.cancel.mockImplementation(() => Promise.resolve(true));
586
- const listArgs = reactive({
587
- user: 1,
588
- });
589
- const retrieveArgs = reactive({
590
- fields: fields,
591
- });
592
- const props = { pkKey: "id", listArgs, retrieveArgs, keepOldPages: false };
593
- expect(() => useListSubscription({ props, clearListOnListIntentTriggered: false })).toThrow(
594
- "useListSubscription should be passed listInstance or keepOldPages."
595
- );
596
- });
597
- });
598
- it("useListSubscriptions", async function () {
599
- const listSubscriptionA = useListSubscription({
600
- props: {
601
- crudArgs: { stream: "test_streamA" },
602
- pkKey: "id",
603
- listArgs: { user: 1 },
604
- retrieveArgs: {
605
- fields,
606
- },
607
- },
608
- keepOldPages: false,
609
- clearListOnListIntentTriggered: false,
610
- });
611
- const listSubscriptionB = useListSubscription({
612
- props: {
613
- crudArgs: { stream: "test_streamB" },
614
- pkKey: "id",
615
- listArgs: { user: 2 },
616
- retrieveArgs: {
617
- fields,
618
- },
619
- },
620
- keepOldPages: false,
621
- clearListOnListIntentTriggered: false,
622
- });
623
- const listSubscription = useListSubscriptions({
624
- A: {
625
- props: {
626
- crudArgs: { stream: "test_streamA" },
627
- pkKey: "id",
628
- listArgs: { user: 1 },
629
- retrieveArgs: {
630
- fields,
631
- },
632
- },
633
- keepOldPages: false,
634
- clearListOnListIntentTriggered: false,
635
- },
636
- B: {
637
- props: {
638
- crudArgs: { stream: "test_streamB" },
639
- pkKey: "id",
640
- listArgs: { user: 2 },
641
- retrieveArgs: {
642
- fields,
643
- },
644
- },
645
- keepOldPages: false,
646
- clearListOnListIntentTriggered: false,
647
- },
648
- });
649
- expect(deepUnref(listSubscription.A.state)).toEqual(deepUnref(listSubscriptionA.state));
650
- expect(deepUnref(listSubscription.B.state)).toEqual(deepUnref(listSubscriptionB.state));
651
- });
652
- it("useListSubscriptions & useListInstances", async function () {
653
- const listInstanceA = useListInstance({
654
- props: {
655
- crudArgs: { stream: "test_streamA" },
656
- pkKey: "id",
657
- listArgs: { user: 1 },
658
- retrieveArgs: {
659
- fields,
660
- },
661
- },
662
- keepOldPages: false,
663
- });
664
- const listInstanceB = useListInstance({
665
- props: {
666
- crudArgs: { stream: "test_streamB" },
667
- pkKey: "id",
668
- listArgs: { user: 2 },
669
- retrieveArgs: {
670
- fields,
671
- },
672
- },
673
- keepOldPages: false,
674
- });
675
- const listSubscriptionA = useListSubscription({
676
- listInstance: listInstanceA,
677
- clearListOnListIntentTriggered: false,
678
- });
679
- const listSubscriptionB = useListSubscription({
680
- listInstance: listInstanceB,
681
- clearListOnListIntentTriggered: false,
682
- });
683
- const listInstances = useListInstances({
684
- A: {
685
- listInstance: listInstanceA,
686
- props: {
687
- crudArgs: { stream: "test_streamA" },
688
- pkKey: "id",
689
- listArgs: { user: 1 },
690
- retrieveArgs: {
691
- fields,
692
- },
693
- },
694
- keepOldPages: false,
695
- },
696
- B: {
697
- listInstance: listInstanceB,
698
- props: {
699
- crudArgs: { stream: "test_streamB" },
700
- pkKey: "id",
701
- listArgs: { user: 2 },
702
- retrieveArgs: {
703
- fields,
704
- },
705
- },
706
- keepOldPages: false,
707
- },
708
- });
709
- const listSubscription = useListSubscriptions(
710
- {
711
- A: {
712
- listInstance: listInstanceA,
713
- clearListOnListIntentTriggered: false,
714
- },
715
- B: {
716
- listInstance: listInstanceB,
717
- clearListOnListIntentTriggered: false,
718
- },
719
- },
720
- listInstances
721
- );
722
- expect(deepUnref(listSubscription.A.listInstance.state)).toEqual(deepUnref(listInstanceA.state));
723
- expect(deepUnref(listSubscription.B.listInstance.state)).toEqual(deepUnref(listInstanceB.state));
724
- expect(deepUnref(listSubscription.A.state)).toEqual(deepUnref(listSubscriptionA.state));
725
- expect(deepUnref(listSubscription.B.state)).toEqual(deepUnref(listSubscriptionB.state));
726
- });
727
- it("custom pkKey", async function () {
728
- const listArgs = reactive({
729
- user: 1,
730
- });
731
- const retrieveArgs = reactive({
732
- fields: fields,
733
- });
734
- const listSubscription = useListSubscription({
735
- props: {
736
- pkKey: "hash",
737
- listArgs,
738
- retrieveArgs,
739
- },
740
- keepOldPages: false,
741
- clearListOnListIntentTriggered: false,
742
- });
743
- listSubscription.subscribe();
744
- await nextTick();
745
- await flushPromises();
746
- expect(crudSubscribe).toHaveBeenCalledWith({
747
- crudArgs: { stream: "test_stream" },
748
- pkKey: "hash",
749
- listArgs: { user: 1 },
750
- retrieveArgs: { fields: fields },
751
- subscriptionEventCallback: expect.any(Function),
752
- });
753
- expect(crudSubscribe).toHaveBeenCalledTimes(1);
754
- expect(listSubscription.state.subscribed).toBe(true);
755
-
756
- crudListResolvable[0].resolve();
757
- crudSubscribeResolvable[0].resolve();
758
- await poll(() => listSubscription.state.subscribed);
759
-
760
- passedSubscriptionEventCallback(
761
- {
762
- hash: 1,
763
- __str__: "blur",
764
- name: "blur",
765
- },
766
- "create"
767
- );
768
-
769
- expect(listSubscription.listInstance.state.objects).toEqual({
770
- 1: {
771
- hash: 1,
772
- __str__: "blur",
773
- name: "blur",
774
- },
775
- });
776
-
777
- passedSubscriptionEventCallback(
778
- {
779
- hash: 1,
780
- __str__: "blur",
781
- fame: "blur",
782
- },
783
- "update"
784
- );
785
-
786
- expect(listSubscription.listInstance.state.objects).toEqual({
787
- 1: {
788
- hash: 1,
789
- __str__: "blur",
790
- fame: "blur",
791
- },
792
- });
793
- });
794
-
795
- describe("clearListOnListIntentTriggered true", function () {
796
- it("on true", async function () {
797
- crudSubscribeResolvable[0].promise.cancel.mockImplementation(() => Promise.resolve(true));
798
- const listArgs = reactive({
799
- user: 1,
800
- });
801
- const retrieveArgs = reactive({
802
- fields: fields,
803
- });
804
- const listInstance = useListInstance({
805
- props: { pkKey: "id", listArgs, retrieveArgs },
806
- keepOldPages: false,
807
- });
808
- listInstance.clearList = vi.fn().mockImplementationOnce(() => undefined);
809
- const listSubscription = useListSubscription({
810
- listInstance,
811
- clearListOnListIntentTriggered: true,
812
- });
813
- listSubscription.subscribe();
814
- await nextTick();
815
- await flushPromises();
816
- listArgs.user = 2;
817
- expect(listInstance.clearList).toHaveBeenCalledTimes(1);
818
- });
819
- });
820
- describe("clearListOnListIntentTriggered false", function () {
821
- it("on true", async function () {
822
- crudSubscribeResolvable[0].promise.cancel.mockImplementation(() => Promise.resolve(true));
823
- const listArgs = reactive({
824
- user: 1,
825
- });
826
- const retrieveArgs = reactive({
827
- fields: fields,
828
- });
829
- const listInstance = useListInstance({
830
- props: { pkKey: "id", listArgs, retrieveArgs },
831
- keepOldPages: false,
832
- });
833
- listInstance.clearList = vi.fn().mockImplementationOnce(() => undefined);
834
- const listSubscription = useListSubscription({
835
- listInstance,
836
- clearListOnListIntentTriggered: false,
837
- });
838
- listSubscription.subscribe();
839
- await nextTick();
840
- await flushPromises();
841
- listArgs.user = 2;
842
- expect(listInstance.clearList).toHaveBeenCalledTimes(0);
843
- });
844
- });
845
- });