@arrai-innovations/reactive-helpers 1.2.3 → 2.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.
- package/.lintstagedrc +3 -0
- package/README.md +21 -16
- package/package.json +1 -1
- package/tests/unit/crudPromise.js +23 -0
- package/tests/unit/poll.js +50 -0
- package/tests/unit/use/listInstance.spec.js +47 -89
- package/tests/unit/use/listSort.spec.js +1 -1
- package/tests/unit/use/listSubscription.spec.js +184 -104
- package/tests/unit/use/objectInstance.spec.js +156 -147
- package/tests/unit/use/objectSubscription.spec.js +144 -120
- package/tests/unit/use/watches.spec.js +2 -2
- package/use/listInstance.js +39 -28
- package/use/listSubscription.js +80 -34
- package/use/objectInstance.js +34 -43
- package/use/objectSubscription.js +66 -71
- package/use/paginatedListInstance.js +2 -2
- package/utils/cancellableIntent.js +93 -0
- package/utils/index.js +1 -0
|
@@ -4,6 +4,9 @@ import { nextTick } from "vue";
|
|
|
4
4
|
import { expectErrorToBeNull } from "../expectHelpers";
|
|
5
5
|
import { getMockOnUnmounted } from "../mockOnUnmounted";
|
|
6
6
|
import { assignReactiveObject } from "../../../utils/assignReactiveObject";
|
|
7
|
+
import { cloneDeep } from "lodash";
|
|
8
|
+
import { doAwaitTimeout } from "../../../utils";
|
|
9
|
+
import { poll } from "../poll";
|
|
7
10
|
|
|
8
11
|
getMockOnUnmounted();
|
|
9
12
|
|
|
@@ -12,30 +15,44 @@ afterAll(() => {
|
|
|
12
15
|
});
|
|
13
16
|
|
|
14
17
|
describe("use/objectSubscription.js", function () {
|
|
15
|
-
let useObjectSubscription,
|
|
18
|
+
let useObjectSubscription,
|
|
19
|
+
useObjectSubscriptions,
|
|
20
|
+
globalSubscribe,
|
|
21
|
+
globalUnsubscribe,
|
|
22
|
+
globalRetrieve,
|
|
23
|
+
objectSubscription;
|
|
16
24
|
beforeEach(async () => {
|
|
17
25
|
const objectInstanceModule = await import("../../../use/objectInstance");
|
|
26
|
+
globalRetrieve = jest.fn();
|
|
18
27
|
objectInstanceModule.setObjectInstanceCrud({
|
|
19
|
-
retrieve:
|
|
28
|
+
retrieve: globalRetrieve,
|
|
20
29
|
create: jest.fn(),
|
|
21
30
|
update: jest.fn(),
|
|
22
31
|
patch: jest.fn(),
|
|
23
32
|
delete: jest.fn(),
|
|
24
|
-
});
|
|
25
|
-
globalRetrieve = jest.fn();
|
|
26
|
-
objectInstanceModule.setObjectInstanceCrud({
|
|
27
|
-
retrieve: globalRetrieve,
|
|
28
33
|
args: { stream: "test_stream" },
|
|
29
34
|
});
|
|
30
|
-
|
|
31
|
-
useObjectSubscription = objectSubscriptionModule.useObjectSubscription;
|
|
32
|
-
ObjectSubscriptionError = objectSubscriptionModule.ObjectSubscriptionError;
|
|
35
|
+
globalUnsubscribe = jest.fn();
|
|
33
36
|
globalSubscribe = jest.fn();
|
|
34
|
-
|
|
37
|
+
globalSubscribe.mockImplementation(() => {
|
|
38
|
+
return Promise.resolve(globalUnsubscribe);
|
|
39
|
+
});
|
|
40
|
+
globalUnsubscribe.mockImplementation(() => {
|
|
41
|
+
return true;
|
|
42
|
+
});
|
|
43
|
+
const imported = await import("../../../use/objectSubscription");
|
|
44
|
+
imported.setObjectSubscriptionCrud({
|
|
35
45
|
subscribe: globalSubscribe,
|
|
36
46
|
args: { stream: "test_stream" },
|
|
37
47
|
});
|
|
38
|
-
|
|
48
|
+
|
|
49
|
+
useObjectSubscription = imported.useObjectSubscription;
|
|
50
|
+
useObjectSubscriptions = imported.useObjectSubscriptions;
|
|
51
|
+
|
|
52
|
+
objectSubscription = useObjectSubscription({
|
|
53
|
+
id: 1,
|
|
54
|
+
retrieveArgs: cloneDeep({ fields }),
|
|
55
|
+
});
|
|
39
56
|
});
|
|
40
57
|
afterEach(function () {
|
|
41
58
|
jest.resetAllMocks();
|
|
@@ -61,15 +78,6 @@ describe("use/objectSubscription.js", function () {
|
|
|
61
78
|
request_id: "60799141-959a-4ff7-80bc-1ad6b805a8fd",
|
|
62
79
|
};
|
|
63
80
|
const fields = ["id", "__str__", "name"];
|
|
64
|
-
let objectSubscription;
|
|
65
|
-
beforeEach(() => {
|
|
66
|
-
objectSubscription = useObjectSubscription({
|
|
67
|
-
id: 1,
|
|
68
|
-
retrieveArgs: {
|
|
69
|
-
fields,
|
|
70
|
-
},
|
|
71
|
-
});
|
|
72
|
-
});
|
|
73
81
|
describe("subscribe", function () {
|
|
74
82
|
let crudRetrieveResolve, crudRetrieveReject, crudSubscribeResolve, crudSubscribeReject, crudSubscribePromise;
|
|
75
83
|
beforeEach(() => {
|
|
@@ -90,22 +98,24 @@ describe("use/objectSubscription.js", function () {
|
|
|
90
98
|
expect(objectSubscription.state.loading).toBeUndefined();
|
|
91
99
|
expect(objectSubscription.state.errored).toBe(false);
|
|
92
100
|
expectErrorToBeNull(objectSubscription.state.error);
|
|
93
|
-
expect(objectSubscription.
|
|
94
|
-
expect(objectSubscription.state.subscribed).toBe(
|
|
101
|
+
expect(objectSubscription.state.deleted).toBe(false);
|
|
102
|
+
expect(objectSubscription.state.subscribed).toBe(undefined);
|
|
95
103
|
expect(objectSubscription.state.intendToSubscribe).toBe(false);
|
|
96
104
|
expect(objectSubscription.state.intendToRetrieve).toBe(false);
|
|
97
|
-
expect(objectSubscription.
|
|
105
|
+
expect(objectSubscription.state.object).toEqual({});
|
|
98
106
|
|
|
99
|
-
|
|
107
|
+
objectSubscription.subscribe();
|
|
108
|
+
|
|
109
|
+
await nextTick();
|
|
100
110
|
|
|
101
111
|
expect(objectSubscription.state.loading).toBe(true);
|
|
102
112
|
expect(objectSubscription.state.errored).toBe(false);
|
|
103
113
|
expectErrorToBeNull(objectSubscription.state.error);
|
|
104
|
-
expect(objectSubscription.
|
|
105
|
-
expect(objectSubscription.state.subscribed).toBe(
|
|
114
|
+
expect(objectSubscription.state.deleted).toBe(false);
|
|
115
|
+
expect(objectSubscription.state.subscribed).toBe(undefined);
|
|
106
116
|
expect(objectSubscription.state.intendToSubscribe).toBe(true);
|
|
107
117
|
expect(objectSubscription.state.intendToRetrieve).toBe(true);
|
|
108
|
-
expect(objectSubscription.
|
|
118
|
+
expect(objectSubscription.state.object).toEqual({});
|
|
109
119
|
await nextTick();
|
|
110
120
|
|
|
111
121
|
crudRetrieveResolve(crudRetrieveResolved);
|
|
@@ -115,13 +125,13 @@ describe("use/objectSubscription.js", function () {
|
|
|
115
125
|
expect(objectSubscription.state.loading).toBe(false);
|
|
116
126
|
expect(objectSubscription.state.errored).toBe(false);
|
|
117
127
|
expectErrorToBeNull(objectSubscription.state.error);
|
|
118
|
-
expect(objectSubscription.
|
|
128
|
+
expect(objectSubscription.state.deleted).toBe(false);
|
|
119
129
|
expect(objectSubscription.state.subscribed).toBe(true);
|
|
120
130
|
expect(objectSubscription.state.intendToSubscribe).toBe(true);
|
|
121
131
|
expect(objectSubscription.state.intendToRetrieve).toBe(true);
|
|
122
|
-
expect(objectSubscription.
|
|
132
|
+
expect(objectSubscription.state.object).toEqual(crudRetrieveResolved);
|
|
123
133
|
await nextTick();
|
|
124
|
-
await
|
|
134
|
+
await flushPromises();
|
|
125
135
|
|
|
126
136
|
expect(globalRetrieve).toHaveBeenCalledWith({
|
|
127
137
|
crudArgs: { stream: "test_stream" },
|
|
@@ -139,6 +149,7 @@ describe("use/objectSubscription.js", function () {
|
|
|
139
149
|
},
|
|
140
150
|
callback: expect.any(Function),
|
|
141
151
|
});
|
|
152
|
+
await doAwaitTimeout(1000);
|
|
142
153
|
expect(globalSubscribe).toHaveBeenCalledTimes(1);
|
|
143
154
|
});
|
|
144
155
|
it("subscribe callback", async function () {
|
|
@@ -154,10 +165,12 @@ describe("use/objectSubscription.js", function () {
|
|
|
154
165
|
objectSubscription.objectInstance.updateFromSubscription = jest.fn();
|
|
155
166
|
objectSubscription.objectInstance.deleteFromSubscription = jest.fn();
|
|
156
167
|
|
|
157
|
-
const
|
|
168
|
+
const subscribeResult = objectSubscription.subscribe();
|
|
158
169
|
crudRetrieveResolve(crudRetrieveResolved);
|
|
159
170
|
crudSubscribeResolve(crudSubscribeResolved);
|
|
160
|
-
|
|
171
|
+
expect(subscribeResult).toBe(true);
|
|
172
|
+
|
|
173
|
+
await poll(() => subscribeCallback);
|
|
161
174
|
|
|
162
175
|
subscribeCallback({ id: 1, __str__: "!asdf", name: "!zxcv" }, "update");
|
|
163
176
|
subscribeCallback({ id: 1, __str__: "asdf!", name: "zxcv!" }, "create");
|
|
@@ -177,18 +190,21 @@ describe("use/objectSubscription.js", function () {
|
|
|
177
190
|
expect(objectSubscription.objectInstance.deleteFromSubscription).toHaveBeenCalledTimes(1);
|
|
178
191
|
});
|
|
179
192
|
it("intendToSubscribe but dont intendToRetrieve", async function () {
|
|
180
|
-
const
|
|
193
|
+
const returnValue = objectSubscription.subscribe({ retrieve: false });
|
|
181
194
|
crudSubscribeResolve(crudSubscribeResolved);
|
|
182
|
-
|
|
195
|
+
expect(returnValue).toBe(true);
|
|
196
|
+
|
|
197
|
+
await nextTick();
|
|
198
|
+
await flushPromises();
|
|
183
199
|
|
|
184
200
|
expect(objectSubscription.state.loading).toBe(false);
|
|
185
201
|
expect(objectSubscription.state.errored).toBe(false);
|
|
186
202
|
expectErrorToBeNull(objectSubscription.state.error);
|
|
187
|
-
expect(objectSubscription.
|
|
203
|
+
expect(objectSubscription.state.deleted).toBe(false);
|
|
188
204
|
expect(objectSubscription.state.subscribed).toBe(true);
|
|
189
205
|
expect(objectSubscription.state.intendToSubscribe).toBe(true);
|
|
190
206
|
expect(objectSubscription.state.intendToRetrieve).toBe(false);
|
|
191
|
-
expect(objectSubscription.
|
|
207
|
+
expect(objectSubscription.state.object).toEqual({});
|
|
192
208
|
|
|
193
209
|
expect(globalRetrieve).toHaveBeenCalledTimes(0);
|
|
194
210
|
expect(globalSubscribe).toHaveBeenNthCalledWith(1, {
|
|
@@ -201,43 +217,43 @@ describe("use/objectSubscription.js", function () {
|
|
|
201
217
|
});
|
|
202
218
|
expect(globalSubscribe).toHaveBeenCalledTimes(1);
|
|
203
219
|
});
|
|
204
|
-
it("success
|
|
205
|
-
objectSubscription.state.retrieveArgs = false;
|
|
220
|
+
it("delayed success", async function () {
|
|
206
221
|
objectSubscription.objectInstance.state.retrieveArgs = false;
|
|
207
222
|
|
|
208
223
|
expect(objectSubscription.state.loading).toBeUndefined();
|
|
209
224
|
expect(objectSubscription.state.errored).toBe(false);
|
|
210
225
|
expectErrorToBeNull(objectSubscription.state.error);
|
|
211
|
-
expect(objectSubscription.
|
|
212
|
-
expect(objectSubscription.state.subscribed).toBe(
|
|
226
|
+
expect(objectSubscription.state.deleted).toBe(false);
|
|
227
|
+
expect(objectSubscription.state.subscribed).toBe(undefined);
|
|
213
228
|
expect(objectSubscription.state.intendToSubscribe).toBe(false);
|
|
214
229
|
expect(objectSubscription.state.intendToRetrieve).toBe(false);
|
|
215
|
-
expect(objectSubscription.
|
|
230
|
+
expect(objectSubscription.state.object).toEqual({});
|
|
216
231
|
|
|
217
|
-
const
|
|
218
|
-
|
|
232
|
+
const returnValue = objectSubscription.subscribe();
|
|
233
|
+
expect(returnValue).toBe(true);
|
|
234
|
+
await nextTick();
|
|
235
|
+
await flushPromises();
|
|
219
236
|
|
|
220
237
|
expect(objectSubscription.state.loading).toBeUndefined();
|
|
221
238
|
expect(objectSubscription.state.errored).toBe(false);
|
|
222
239
|
expectErrorToBeNull(objectSubscription.state.error);
|
|
223
|
-
expect(objectSubscription.
|
|
224
|
-
expect(objectSubscription.state.subscribed).toBe(
|
|
240
|
+
expect(objectSubscription.state.deleted).toBe(false);
|
|
241
|
+
expect(objectSubscription.state.subscribed).toBe(undefined);
|
|
225
242
|
expect(objectSubscription.state.intendToSubscribe).toBe(true);
|
|
226
243
|
expect(objectSubscription.state.intendToRetrieve).toBe(true);
|
|
227
|
-
expect(objectSubscription.
|
|
244
|
+
expect(objectSubscription.state.object).toEqual({});
|
|
228
245
|
|
|
229
|
-
objectSubscription.state.retrieveArgs = { fields };
|
|
230
246
|
objectSubscription.objectInstance.state.retrieveArgs = { fields };
|
|
231
247
|
await nextTick();
|
|
232
248
|
|
|
233
249
|
expect(objectSubscription.state.loading).toBe(true);
|
|
234
250
|
expect(objectSubscription.state.errored).toBe(false);
|
|
235
251
|
expectErrorToBeNull(objectSubscription.state.error);
|
|
236
|
-
expect(objectSubscription.
|
|
237
|
-
expect(objectSubscription.state.subscribed).toBe(
|
|
252
|
+
expect(objectSubscription.state.deleted).toBe(false);
|
|
253
|
+
expect(objectSubscription.state.subscribed).toBe(undefined);
|
|
238
254
|
expect(objectSubscription.state.intendToSubscribe).toBe(true);
|
|
239
255
|
expect(objectSubscription.state.intendToRetrieve).toBe(true);
|
|
240
|
-
expect(objectSubscription.
|
|
256
|
+
expect(objectSubscription.state.object).toEqual({});
|
|
241
257
|
|
|
242
258
|
crudRetrieveResolve(crudRetrieveResolved);
|
|
243
259
|
crudSubscribeResolve(crudSubscribeResolved);
|
|
@@ -246,11 +262,11 @@ describe("use/objectSubscription.js", function () {
|
|
|
246
262
|
expect(objectSubscription.state.loading).toBe(false);
|
|
247
263
|
expect(objectSubscription.state.errored).toBe(false);
|
|
248
264
|
expectErrorToBeNull(objectSubscription.state.error);
|
|
249
|
-
expect(objectSubscription.
|
|
265
|
+
expect(objectSubscription.state.deleted).toBe(false);
|
|
250
266
|
expect(objectSubscription.state.subscribed).toBe(true);
|
|
251
267
|
expect(objectSubscription.state.intendToSubscribe).toBe(true);
|
|
252
268
|
expect(objectSubscription.state.intendToRetrieve).toBe(true);
|
|
253
|
-
expect(objectSubscription.
|
|
269
|
+
expect(objectSubscription.state.object).toEqual(crudRetrieveResolved);
|
|
254
270
|
await nextTick();
|
|
255
271
|
|
|
256
272
|
expect(globalRetrieve).toHaveBeenNthCalledWith(1, {
|
|
@@ -271,26 +287,29 @@ describe("use/objectSubscription.js", function () {
|
|
|
271
287
|
});
|
|
272
288
|
expect(globalSubscribe).toHaveBeenCalledTimes(1);
|
|
273
289
|
});
|
|
274
|
-
it("errored
|
|
290
|
+
it("retrieve errored", async function () {
|
|
275
291
|
expect(objectSubscription.state.loading).toBeUndefined();
|
|
276
292
|
expect(objectSubscription.state.errored).toBe(false);
|
|
277
293
|
expectErrorToBeNull(objectSubscription.state.error);
|
|
278
|
-
expect(objectSubscription.
|
|
279
|
-
expect(objectSubscription.state.subscribed).toBe(
|
|
294
|
+
expect(objectSubscription.state.deleted).toBe(false);
|
|
295
|
+
expect(objectSubscription.state.subscribed).toBe(undefined);
|
|
280
296
|
expect(objectSubscription.state.intendToSubscribe).toBe(false);
|
|
281
297
|
expect(objectSubscription.state.intendToRetrieve).toBe(false);
|
|
282
|
-
expect(objectSubscription.
|
|
298
|
+
expect(objectSubscription.state.object).toEqual({});
|
|
283
299
|
|
|
284
|
-
const
|
|
300
|
+
const returnValue = objectSubscription.subscribe();
|
|
301
|
+
expect(returnValue).toBe(true);
|
|
302
|
+
await nextTick();
|
|
303
|
+
await flushPromises();
|
|
285
304
|
|
|
286
305
|
expect(objectSubscription.state.loading).toBe(true);
|
|
287
306
|
expect(objectSubscription.state.errored).toBe(false);
|
|
288
307
|
expectErrorToBeNull(objectSubscription.state.error);
|
|
289
|
-
expect(objectSubscription.
|
|
290
|
-
expect(objectSubscription.state.subscribed).toBe(
|
|
308
|
+
expect(objectSubscription.state.deleted).toBe(false);
|
|
309
|
+
expect(objectSubscription.state.subscribed).toBe(undefined);
|
|
291
310
|
expect(objectSubscription.state.intendToSubscribe).toBe(true);
|
|
292
311
|
expect(objectSubscription.state.intendToRetrieve).toBe(true);
|
|
293
|
-
expect(objectSubscription.
|
|
312
|
+
expect(objectSubscription.state.object).toEqual({});
|
|
294
313
|
await nextTick();
|
|
295
314
|
|
|
296
315
|
crudRetrieveReject(crudRetrieveRejected);
|
|
@@ -300,12 +319,11 @@ describe("use/objectSubscription.js", function () {
|
|
|
300
319
|
expect(objectSubscription.state.loading).toBe(false);
|
|
301
320
|
expect(objectSubscription.state.errored).toBe(true);
|
|
302
321
|
expect(objectSubscription.state.error).toEqual(crudRetrieveRejected);
|
|
303
|
-
expect(objectSubscription.
|
|
322
|
+
expect(objectSubscription.state.deleted).toBe(false);
|
|
304
323
|
expect(objectSubscription.state.subscribed).toBe(true);
|
|
305
324
|
expect(objectSubscription.state.intendToSubscribe).toBe(true);
|
|
306
325
|
expect(objectSubscription.state.intendToRetrieve).toBe(true);
|
|
307
|
-
expect(objectSubscription.
|
|
308
|
-
await expect(subscribePromise).resolves.toBe(true);
|
|
326
|
+
expect(objectSubscription.state.object).toEqual({});
|
|
309
327
|
await nextTick();
|
|
310
328
|
|
|
311
329
|
expect(globalRetrieve).toHaveBeenCalledWith({
|
|
@@ -326,26 +344,30 @@ describe("use/objectSubscription.js", function () {
|
|
|
326
344
|
});
|
|
327
345
|
expect(globalSubscribe).toHaveBeenCalledTimes(1);
|
|
328
346
|
});
|
|
329
|
-
it("errored
|
|
347
|
+
it("subscribe errored", async function () {
|
|
330
348
|
expect(objectSubscription.state.loading).toBeUndefined();
|
|
331
349
|
expect(objectSubscription.state.errored).toBe(false);
|
|
332
350
|
expectErrorToBeNull(objectSubscription.state.error);
|
|
333
|
-
expect(objectSubscription.
|
|
334
|
-
expect(objectSubscription.state.subscribed).toBe(
|
|
351
|
+
expect(objectSubscription.state.deleted).toBe(false);
|
|
352
|
+
expect(objectSubscription.state.subscribed).toBe(undefined);
|
|
335
353
|
expect(objectSubscription.state.intendToSubscribe).toBe(false);
|
|
336
354
|
expect(objectSubscription.state.intendToRetrieve).toBe(false);
|
|
337
|
-
expect(objectSubscription.
|
|
355
|
+
expect(objectSubscription.state.object).toEqual({});
|
|
338
356
|
|
|
339
|
-
const
|
|
357
|
+
const returnValue = objectSubscription.subscribe();
|
|
358
|
+
expect(returnValue).toBe(true);
|
|
359
|
+
|
|
360
|
+
await nextTick();
|
|
361
|
+
await flushPromises();
|
|
340
362
|
|
|
341
363
|
expect(objectSubscription.state.loading).toBe(true);
|
|
342
364
|
expect(objectSubscription.state.errored).toBe(false);
|
|
343
365
|
expectErrorToBeNull(objectSubscription.state.error);
|
|
344
|
-
expect(objectSubscription.
|
|
345
|
-
expect(objectSubscription.state.subscribed).toBe(
|
|
366
|
+
expect(objectSubscription.state.deleted).toBe(false);
|
|
367
|
+
expect(objectSubscription.state.subscribed).toBe(undefined);
|
|
346
368
|
expect(objectSubscription.state.intendToSubscribe).toBe(true);
|
|
347
369
|
expect(objectSubscription.state.intendToRetrieve).toBe(true);
|
|
348
|
-
expect(objectSubscription.
|
|
370
|
+
expect(objectSubscription.state.object).toEqual({});
|
|
349
371
|
await nextTick();
|
|
350
372
|
|
|
351
373
|
crudRetrieveResolve(crudRetrieveResolved);
|
|
@@ -355,12 +377,12 @@ describe("use/objectSubscription.js", function () {
|
|
|
355
377
|
expect(objectSubscription.state.loading).toBe(false);
|
|
356
378
|
expect(objectSubscription.state.errored).toBe(true);
|
|
357
379
|
expect(objectSubscription.state.error).toEqual(crudSubscribeRejected);
|
|
358
|
-
expect(objectSubscription.
|
|
380
|
+
expect(objectSubscription.state.deleted).toBe(false);
|
|
359
381
|
expect(objectSubscription.state.subscribed).toBe(false);
|
|
360
382
|
expect(objectSubscription.state.intendToSubscribe).toBe(true);
|
|
361
383
|
expect(objectSubscription.state.intendToRetrieve).toBe(true);
|
|
362
|
-
expect(objectSubscription.
|
|
363
|
-
|
|
384
|
+
expect(objectSubscription.state.object).toEqual(crudRetrieveResolved);
|
|
385
|
+
|
|
364
386
|
await nextTick();
|
|
365
387
|
|
|
366
388
|
expect(globalRetrieve).toHaveBeenCalledWith({
|
|
@@ -385,15 +407,15 @@ describe("use/objectSubscription.js", function () {
|
|
|
385
407
|
expect(objectSubscription.state.loading).toBeUndefined();
|
|
386
408
|
expect(objectSubscription.state.errored).toBe(false);
|
|
387
409
|
expectErrorToBeNull(objectSubscription.state.error);
|
|
388
|
-
expect(objectSubscription.
|
|
389
|
-
expect(objectSubscription.state.subscribed).toBe(
|
|
410
|
+
expect(objectSubscription.state.deleted).toBe(false);
|
|
411
|
+
expect(objectSubscription.state.subscribed).toBe(undefined);
|
|
390
412
|
expect(objectSubscription.state.intendToSubscribe).toBe(false);
|
|
391
413
|
expect(objectSubscription.state.intendToRetrieve).toBe(false);
|
|
392
|
-
expect(objectSubscription.
|
|
414
|
+
expect(objectSubscription.state.object).toEqual({});
|
|
393
415
|
|
|
394
416
|
objectSubscription.subscribe();
|
|
395
|
-
const
|
|
396
|
-
|
|
417
|
+
const returnValue = objectSubscription.subscribe();
|
|
418
|
+
expect(returnValue).toBe(false);
|
|
397
419
|
});
|
|
398
420
|
});
|
|
399
421
|
describe("unsubscribe", function () {
|
|
@@ -414,97 +436,100 @@ describe("use/objectSubscription.js", function () {
|
|
|
414
436
|
globalSubscribe.mockReturnValueOnce(crudSubscribePromise);
|
|
415
437
|
});
|
|
416
438
|
it("success", async function () {
|
|
417
|
-
|
|
439
|
+
expect(objectSubscription.subscribe()).toBe(true);
|
|
418
440
|
crudSubscribeResolve(crudSubscribeResolved);
|
|
419
441
|
crudRetrieveResolve(crudRetrieveResolved);
|
|
420
|
-
|
|
442
|
+
|
|
443
|
+
await nextTick();
|
|
444
|
+
await flushPromises();
|
|
421
445
|
|
|
422
446
|
expect(objectSubscription.state.loading).toBe(false);
|
|
423
447
|
expect(objectSubscription.state.errored).toBe(false);
|
|
424
448
|
expectErrorToBeNull(objectSubscription.state.error);
|
|
425
|
-
expect(objectSubscription.
|
|
449
|
+
expect(objectSubscription.state.deleted).toBe(false);
|
|
426
450
|
expect(objectSubscription.state.subscribed).toBe(true);
|
|
427
451
|
expect(objectSubscription.state.intendToSubscribe).toBe(true);
|
|
428
452
|
expect(objectSubscription.state.intendToRetrieve).toBe(true);
|
|
429
|
-
expect(objectSubscription.
|
|
453
|
+
expect(objectSubscription.state.object).toEqual(crudRetrieveResolved);
|
|
454
|
+
|
|
455
|
+
expect(objectSubscription.unsubscribe()).toBe(true);
|
|
430
456
|
|
|
431
|
-
|
|
457
|
+
await nextTick();
|
|
458
|
+
await flushPromises();
|
|
432
459
|
|
|
433
460
|
expect(objectSubscription.state.loading).toBe(false);
|
|
434
461
|
expect(objectSubscription.state.errored).toBe(false);
|
|
435
462
|
expectErrorToBeNull(objectSubscription.state.error);
|
|
436
|
-
expect(objectSubscription.
|
|
463
|
+
expect(objectSubscription.state.deleted).toBe(false);
|
|
437
464
|
expect(objectSubscription.state.subscribed).toBe(false);
|
|
438
465
|
expect(objectSubscription.state.intendToSubscribe).toBe(false);
|
|
439
466
|
expect(objectSubscription.state.intendToRetrieve).toBe(false);
|
|
440
|
-
expect(objectSubscription.
|
|
467
|
+
expect(objectSubscription.state.object).toEqual(crudRetrieveResolved);
|
|
441
468
|
|
|
442
469
|
crudCancelResolve(true);
|
|
470
|
+
await nextTick();
|
|
471
|
+
await flushPromises();
|
|
443
472
|
|
|
444
473
|
expect(objectSubscription.state.loading).toBe(false);
|
|
445
474
|
expect(objectSubscription.state.errored).toBe(false);
|
|
446
475
|
expectErrorToBeNull(objectSubscription.state.error);
|
|
447
|
-
expect(objectSubscription.
|
|
476
|
+
expect(objectSubscription.state.deleted).toBe(false);
|
|
448
477
|
expect(objectSubscription.state.subscribed).toBe(false);
|
|
449
478
|
expect(objectSubscription.state.intendToSubscribe).toBe(false);
|
|
450
479
|
expect(objectSubscription.state.intendToRetrieve).toBe(false);
|
|
451
|
-
expect(objectSubscription.
|
|
452
|
-
|
|
453
|
-
await expect(unsubscribePromise).resolves.toBe(true);
|
|
480
|
+
expect(objectSubscription.state.object).toEqual(crudRetrieveResolved);
|
|
454
481
|
});
|
|
455
482
|
it("errored", async function () {
|
|
456
|
-
|
|
483
|
+
expect(objectSubscription.subscribe()).toBe(true);
|
|
457
484
|
crudSubscribeResolve(crudSubscribeResolved);
|
|
458
485
|
crudRetrieveResolve(crudRetrieveResolved);
|
|
459
|
-
|
|
486
|
+
|
|
487
|
+
await nextTick();
|
|
488
|
+
await flushPromises();
|
|
460
489
|
|
|
461
490
|
expect(objectSubscription.state.loading).toBe(false);
|
|
462
491
|
expect(objectSubscription.state.errored).toBe(false);
|
|
463
492
|
expectErrorToBeNull(objectSubscription.state.error);
|
|
464
|
-
expect(objectSubscription.
|
|
493
|
+
expect(objectSubscription.state.deleted).toBe(false);
|
|
465
494
|
expect(objectSubscription.state.subscribed).toBe(true);
|
|
466
495
|
expect(objectSubscription.state.intendToSubscribe).toBe(true);
|
|
467
496
|
expect(objectSubscription.state.intendToRetrieve).toBe(true);
|
|
468
|
-
expect(objectSubscription.
|
|
497
|
+
expect(objectSubscription.state.object).toEqual(crudRetrieveResolved);
|
|
469
498
|
|
|
470
|
-
|
|
499
|
+
expect(objectSubscription.unsubscribe()).toBe(true);
|
|
500
|
+
await nextTick();
|
|
501
|
+
await flushPromises();
|
|
471
502
|
|
|
472
503
|
expect(objectSubscription.state.loading).toBe(false);
|
|
473
504
|
expect(objectSubscription.state.errored).toBe(false);
|
|
474
505
|
expectErrorToBeNull(objectSubscription.state.error);
|
|
475
|
-
expect(objectSubscription.
|
|
506
|
+
expect(objectSubscription.state.deleted).toBe(false);
|
|
476
507
|
expect(objectSubscription.state.subscribed).toBe(false);
|
|
477
508
|
expect(objectSubscription.state.intendToSubscribe).toBe(false);
|
|
478
509
|
expect(objectSubscription.state.intendToRetrieve).toBe(false);
|
|
479
|
-
expect(objectSubscription.
|
|
510
|
+
expect(objectSubscription.state.object).toEqual(crudRetrieveResolved);
|
|
480
511
|
|
|
481
512
|
crudCancelResolve(false);
|
|
482
513
|
|
|
483
514
|
expect(objectSubscription.state.loading).toBe(false);
|
|
484
515
|
expect(objectSubscription.state.errored).toBe(false);
|
|
485
516
|
expectErrorToBeNull(objectSubscription.state.error);
|
|
486
|
-
expect(objectSubscription.
|
|
517
|
+
expect(objectSubscription.state.deleted).toBe(false);
|
|
487
518
|
expect(objectSubscription.state.subscribed).toBe(false);
|
|
488
519
|
expect(objectSubscription.state.intendToSubscribe).toBe(false);
|
|
489
520
|
expect(objectSubscription.state.intendToRetrieve).toBe(false);
|
|
490
|
-
expect(objectSubscription.
|
|
491
|
-
|
|
492
|
-
await expect(unsubscribePromise).resolves.toBe(false);
|
|
521
|
+
expect(objectSubscription.state.object).toEqual(crudRetrieveResolved);
|
|
493
522
|
});
|
|
494
523
|
it("already unsubscribed", async function () {
|
|
495
|
-
|
|
524
|
+
expect(objectSubscription.subscribe()).toBe(true);
|
|
496
525
|
crudSubscribeResolve(crudSubscribeResolved);
|
|
497
526
|
crudRetrieveResolve(crudRetrieveResolved);
|
|
498
|
-
await expect(subscribePromise).resolves.toBe(true);
|
|
499
|
-
|
|
500
527
|
objectSubscription.unsubscribe();
|
|
501
|
-
|
|
502
|
-
await expect(unsubscribePromise).resolves.toBe(false);
|
|
528
|
+
expect(objectSubscription.unsubscribe()).toBe(false);
|
|
503
529
|
});
|
|
504
530
|
|
|
505
531
|
it("not subscribed", async function () {
|
|
506
|
-
|
|
507
|
-
await expect(unsubscribePromise).resolves.toBe(false);
|
|
532
|
+
expect(objectSubscription.unsubscribe()).toBe(false);
|
|
508
533
|
});
|
|
509
534
|
});
|
|
510
535
|
describe("custom crud", function () {
|
|
@@ -529,11 +554,10 @@ describe("use/objectSubscription.js", function () {
|
|
|
529
554
|
},
|
|
530
555
|
});
|
|
531
556
|
|
|
532
|
-
|
|
557
|
+
expect(objectSubscription.subscribe()).toBe(true);
|
|
533
558
|
crudRetrieveResolve(crudRetrieveResolved);
|
|
534
559
|
crudSubscribeResolve(crudSubscribeResolved);
|
|
535
560
|
await flushPromises();
|
|
536
|
-
await expect(subscribePromise).resolves.toBe(true);
|
|
537
561
|
|
|
538
562
|
expect(globalRetrieve).toHaveBeenCalledWith({
|
|
539
563
|
crudArgs: { stream: "test_stream2" },
|
|
@@ -576,14 +600,14 @@ describe("use/objectSubscription.js", function () {
|
|
|
576
600
|
fields,
|
|
577
601
|
},
|
|
578
602
|
});
|
|
579
|
-
objectSubscription.objectInstance.state.
|
|
580
|
-
objectSubscription.state.
|
|
603
|
+
objectSubscription.objectInstance.state.crud.retrieve = customCrudRetrieve;
|
|
604
|
+
objectSubscription.state.crud.subscribe = customCrudSubscribe;
|
|
581
605
|
|
|
582
606
|
const subscribePromise = objectSubscription.subscribe();
|
|
583
607
|
crudRetrieveResolve(crudRetrieveResolved);
|
|
584
608
|
crudSubscribeResolve(crudSubscribeResolved);
|
|
585
609
|
await flushPromises();
|
|
586
|
-
|
|
610
|
+
expect(subscribePromise).toBe(true);
|
|
587
611
|
|
|
588
612
|
expect(globalRetrieve).toHaveBeenCalledTimes(0);
|
|
589
613
|
expect(globalSubscribe).toHaveBeenCalledTimes(0);
|
|
@@ -644,28 +668,28 @@ describe("use/objectSubscription.js", function () {
|
|
|
644
668
|
const objectSubscription = useObjectSubscription({
|
|
645
669
|
stream: "test_stream",
|
|
646
670
|
});
|
|
647
|
-
assignReactiveObject(objectSubscription.
|
|
671
|
+
assignReactiveObject(objectSubscription.state.object, {
|
|
648
672
|
id: 1,
|
|
649
673
|
__str__: "asdf",
|
|
650
674
|
name: "zxcv",
|
|
651
675
|
});
|
|
652
676
|
objectSubscription.updateFromSubscription({ id: 1, name: "asdf" });
|
|
653
|
-
expect({ ...objectSubscription.
|
|
677
|
+
expect({ ...objectSubscription.state.object }).toEqual({ id: 1, name: "asdf" });
|
|
654
678
|
objectSubscription.updateFromSubscription({ id: 1, __str__: "zxcv" });
|
|
655
|
-
expect({ ...objectSubscription.
|
|
679
|
+
expect({ ...objectSubscription.state.object }).toEqual({ id: 1, __str__: "zxcv" });
|
|
656
680
|
});
|
|
657
681
|
it("deleteFromSubscription", function () {
|
|
658
682
|
const objectSubscription = useObjectSubscription({
|
|
659
683
|
stream: "test_stream",
|
|
660
684
|
});
|
|
661
|
-
assignReactiveObject(objectSubscription.
|
|
685
|
+
assignReactiveObject(objectSubscription.state.object, {
|
|
662
686
|
id: 1,
|
|
663
687
|
__str__: "asdf",
|
|
664
688
|
name: "zxcv",
|
|
665
689
|
});
|
|
666
|
-
expect(objectSubscription.
|
|
690
|
+
expect(objectSubscription.state.deleted).toBe(false);
|
|
667
691
|
objectSubscription.deleteFromSubscription();
|
|
668
|
-
expect(objectSubscription.
|
|
669
|
-
expect(objectSubscription.
|
|
692
|
+
expect(objectSubscription.state.object).toEqual({});
|
|
693
|
+
expect(objectSubscription.state.deleted).toBe(true);
|
|
670
694
|
});
|
|
671
695
|
});
|
|
@@ -25,14 +25,14 @@ describe("use/watches", () => {
|
|
|
25
25
|
});
|
|
26
26
|
describe("doAwaitTimeout", () => {
|
|
27
27
|
it("should resolve after the passed timeout", async () => {
|
|
28
|
-
timeout =
|
|
28
|
+
timeout = 1000;
|
|
29
29
|
const start = performance.now();
|
|
30
30
|
await doAwaitTimeout(timeout);
|
|
31
31
|
const end = performance.now();
|
|
32
32
|
expect(end - start).toBeLessThan(timeout * 1.1);
|
|
33
33
|
});
|
|
34
34
|
it("rejects the promise when stopped manually", async () => {
|
|
35
|
-
timeout =
|
|
35
|
+
timeout = 1000;
|
|
36
36
|
const awaitTimeout = new AwaitTimeout(timeout);
|
|
37
37
|
awaitTimeout.start();
|
|
38
38
|
setTimeout(() => awaitTimeout.stop(), timeout / 2);
|