-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathindex.js
987 lines (919 loc) · 22.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
import { afterEach, beforeEach, expect, test, vi } from 'vitest'
import AsyncComputed from "../src"
import { createApp, defineComponent } from 'vue'
function newVue (component, pluginOpts = {}) {
// Provide default template to silence warnings
app = createApp(Object.assign({ template: '<div/>' }, component))
app.use(AsyncComputed, pluginOpts)
return app.mount(document.body)
}
let app = null
beforeEach(() => {
vi.useFakeTimers()
})
afterEach(() => {
vi.restoreAllMocks()
app.unmount()
})
test("Async computed values are computed", async () => {
const vm = newVue({
asyncComputed: {
a () {
return new Promise(resolve => {
setTimeout(() => resolve('done'), 10)
})
},
b () {
return new Promise(resolve => {
setTimeout(() => resolve(1337), 20)
})
}
}
})
expect(vm.a).toBeNull()
expect(vm.b).toBeNull()
await vi.runAllTimersAsync()
expect(vm.a).toBe('done')
expect(vm.b).toBe(1337)
})
test("An async computed value which is an pre-resolved promise updates at the next tick", async () => {
const vm = newVue({
asyncComputed: {
a () {
return Promise.resolve('done')
}
}
})
expect(vm.a).toBeNull()
await vm.$nextTick()
expect(vm.a).toBe('done')
})
test("Sync and async computed data work together", async () => {
const vm = newVue({
asyncComputed: {
a () {
return new Promise(resolve => {
setTimeout(() => resolve('done'), 10)
})
}
},
computed: {
b () {
return 0
}
}
})
expect(vm.a).toBeNull()
expect(vm.b).toBe(0)
await vi.runAllTimersAsync()
expect(vm.a).toBe('done')
expect(vm.b).toBe(0)
})
test("Async values are properly recalculated", async () => {
const vm = newVue({
asyncComputed: {
a () {
const data = this.x
return new Promise(resolve => {
setTimeout(() => resolve(data), 10)
})
},
b () {
return new Promise(resolve => {
setTimeout(() => resolve('done'), 40)
})
}
},
data: {
x: 0
}
})
expect(vm.a).toBeNull()
expect(vm.b).toBeNull()
expect(vm.x).toBe(0)
await vi.advanceTimersByTimeAsync(10)
expect(vm.a).toBe(0)
expect(vm.b).toBeNull()
expect(vm.x).toBe(0)
vm.x = 1
expect(vm.a).toBe(0)
await vi.advanceTimersByTimeAsync(10)
expect(vm.a).toBe(1)
await vi.advanceTimersByTimeAsync(20)
expect(vm.b).toBe('done')
})
test("Old async values are properly invalidated", async () => {
const vm = newVue({
asyncComputed: {
a () {
return new Promise(resolve => {
setTimeout(() => resolve(this.waitTime), this.waitTime)
})
}
},
data: {
waitTime: 40
}
})
expect(vm.a).toBeNull()
await vi.advanceTimersByTimeAsync(10)
vm.waitTime = 10
expect(vm.a).toBeNull()
await vi.advanceTimersByTimeAsync(10)
expect(vm.a).toBe(10)
await vi.runAllTimersAsync()
expect(vm.a).toBe(10) // Not 40, even though the promise was created with 40
})
test("Having only sync computed data still works", async () => {
const vm = newVue({
computed: {
a () {
return this.x
}
},
data: {
x: 2
}
})
expect(vm.a).toBe(2)
const watchListener = vi.fn()
vm.$watch('a', watchListener)
vm.x++
expect(vm.a).toBe(3)
await vm.$nextTick()
expect(watchListener).toHaveBeenCalledTimes(1)
expect(watchListener).toHaveBeenCalledWith(3, 2, expect.anything())
})
test("Errors in computed properties are handled", async () => {
const errorHandler = vi.fn()
const vm = newVue({
asyncComputed: {
a () {
return Promise.reject(new Error('error'))
}
}
}, { errorHandler })
expect(vm.a).toBeNull()
await vm.$nextTick() // Triggers the asyncComputed body
await vm.$nextTick() // Triggers the reject
expect(vm.a).toBeNull()
expect(errorHandler).toHaveBeenCalledTimes(1)
expect(errorHandler.mock.lastCall[0].slice(0, 13)).toBe('Error: error\n')
})
test("Errors in computed properties are handled, with useRawError", async () => {
const errorHandler = vi.fn()
const vm = newVue({
asyncComputed: {
a () {
// eslint-disable-next-line prefer-promise-reject-errors
return Promise.reject('error')
}
}
}, { errorHandler, useRawError: true })
expect(vm.a).toBeNull()
await vm.$nextTick() // Triggers the asyncComputed body
await vm.$nextTick() // Triggers the reject
expect(vm.a).toBeNull()
expect(errorHandler).toHaveBeenCalledTimes(1)
expect(errorHandler.mock.lastCall[0]).toBe('error')
})
test("Multiple asyncComputed objects are handled the same as normal computed property objects", async () => {
const vm = newVue({
mixins: [{
asyncComputed: {
a () {
return Promise.resolve('mixin-a')
},
b () {
return Promise.resolve('mixin-b')
}
}
}],
asyncComputed: {
a () {
return Promise.resolve('vm-a')
},
c () {
return Promise.resolve('vm-c')
}
}
})
await vm.$nextTick()
expect(vm.a).toBe('vm-a')
expect(vm.b).toBe('mixin-b')
expect(vm.c).toBe('vm-c')
})
test("Async computed values can have defaults", async () => {
const xWatcher = vi.fn()
const computedFromX = vi.fn(function () { return this.x })
const vm = newVue({
asyncComputed: {
x: {
default: false,
get () {
return Promise.resolve(true)
}
},
y () {
return Promise.resolve(true)
},
z: {
get () {
return Promise.resolve(true)
}
}
},
watch: {
x: {
deep: true,
immediate: true,
handler: xWatcher,
},
},
computed: {
computedFromX,
},
})
expect(vm.x).toBe(false) // x should default to false
expect(vm.y).toBeNull() // y doesn't have a default
expect(vm.z).toBeNull() // z doesn't have a default despite being defined with an object
expect(xWatcher).toHaveBeenCalledTimes(1)
expect(xWatcher).toHaveBeenCalledWith(false, undefined, expect.anything())
expect(computedFromX).toHaveBeenCalledTimes(0)
const computed = vm.computedFromX // Force computed execution
expect(computed).toBe(false)
expect(xWatcher).toHaveBeenCalledTimes(1)
expect(computedFromX).toHaveBeenCalledTimes(1)
await vm.$nextTick()
expect(vm.x).toBe(true) // x resolves to true
expect(vm.y).toBe(true) // y resolves to true
expect(vm.z).toBe(true) // z resolves to true
})
test("Default values can be functions", async () => {
const vm = newVue({
data: {
x: 1
},
asyncComputed: {
y: {
default () { return 2 },
get () {
return Promise.resolve(3)
}
},
z: {
default () { return this.x },
get () {
return Promise.resolve(4)
}
}
}
})
expect(vm.y).toBe(2)
expect(vm.z).toBe(1)
await vm.$nextTick()
expect(vm.y).toBe(3)
expect(vm.z).toBe(4)
})
test("Async computed values can be written to, and then will be properly overridden", async () => {
const vm = newVue({
data: {
x: 1
},
asyncComputed: {
y () {
this.y = this.x + 1
return new Promise(resolve => {
setTimeout(() => resolve(this.x), 10)
})
}
}
})
expect(vm.y).toBe(2)
await vi.advanceTimersByTimeAsync(10)
expect(vm.y).toBe(1)
vm.x = 4
expect(vm.y).toBe(1)
await vm.$nextTick()
expect(vm.y).toBe(5)
await vi.advanceTimersByTimeAsync(10)
expect(vm.y).toBe(4)
})
test("Watchers rerun the computation when a value changes", async () => {
let i = 0
const vm = newVue({
data: {
x: 0,
y: 2,
},
asyncComputed: {
z: {
get () {
return Promise.resolve(i + this.y)
},
watch () {
// eslint-disable-next-line no-unused-expressions
this.x
}
}
}
})
expect(vm.z).toBeNull()
await vm.$nextTick()
expect(vm.z).toBe(2)
i++
vm.x--
await vm.$nextTick()
// This tick, Vue registers the change
// in the watcher, and reevaluates
// the getter function
// And since we 'await', the promise chain
// is finished and z is 3
expect(vm.z).toBe(3)
})
test("shouldUpdate controls when to rerun the computation when a value changes", async () => {
let i = 0
let getCallCount = 0
const vm = newVue({
data: {
x: 0,
y: 2,
},
asyncComputed: {
z: {
get () {
getCallCount++
return Promise.resolve(i + this.y)
},
shouldUpdate () {
return this.x % 2 === 0
}
}
}
})
expect(getCallCount).toBe(1)
expect(vm.z).toBeNull()
await vm.$nextTick()
expect(getCallCount).toBe(1)
expect(vm.z).toBe(2)
i++
// update x so it will be 1
// should update returns false now
vm.x++
expect(getCallCount).toBe(1)
await vm.$nextTick()
// This tick, Vue registers the change
// in the watcher, and reevaluates
// the getter function
expect(vm.z).toBe(2)
await vm.$nextTick()
// Now in this tick the promise has
// resolved, and z is 2 since should update returned false.
expect(vm.z).toBe(2)
// update x so it will be 2
// should update returns true now
expect(getCallCount).toBe(1)
vm.x++
expect(getCallCount).toBe(1)
await vm.$nextTick()
expect(getCallCount).toBe(2)
// This tick, Vue registers the change
// in the watcher, and reevaluates
// the getter function
// And since we 'await', the promise chain
// is finished and z is 3
expect(vm.z).toBe(3)
})
test("Watchers trigger but shouldUpdate can still block their updates", async () => {
let i = 0
const vm = newVue({
data: {
canUpdate: true,
x: 0,
y: 2,
},
asyncComputed: {
z: {
get () {
return Promise.resolve(i + this.y)
},
watch () {
// eslint-disable-next-line no-unused-expressions
this.x
},
shouldUpdate () {
return this.canUpdate
}
}
}
})
expect(vm.z).toBeNull()
await vm.$nextTick()
expect(vm.z).toBe(2)
i++
vm.x--
await vm.$nextTick()
// This tick, Vue registers the change
// in the watcher, and reevaluates
// the getter function
// And since we 'await', the promise chain
// is finished
expect(vm.z).toBe(3)
// We stop all updates from now on
vm.canUpdate = false
i++
vm.x--
await vm.$nextTick()
// This tick, Vue registers the change
// in the watcher, and reevaluates
// the getter function but no update
expect(vm.z).toBe(3)
await vm.$nextTick()
// Now in this tick the promise has
// resolved, and z is still 3.
expect(vm.z).toBe(3)
})
test("The default default value can be set in the plugin options", async () => {
const vm = newVue({
asyncComputed: {
x () {
return Promise.resolve(0)
}
}
}, { default: 53 })
expect(vm.x).toBe(53)
await vm.$nextTick()
expect(vm.x).toBe(0)
})
test("The default default value can be set to undefined in the plugin options", async () => {
const vm = newVue({
asyncComputed: {
x () {
return Promise.resolve(0)
}
}
}, { default: undefined })
expect(vm.x).toBeUndefined()
await vm.$nextTick()
expect(vm.x).toBe(0)
})
test("Handle an async computed value returning synchronously", async () => {
const vm = newVue({
asyncComputed: {
x () {
return 1
}
}
})
expect(vm.x).toBeNull()
await vm.$nextTick()
expect(vm.x).toBe(1)
})
test("Work correctly with Vue.extend", async () => {
const SubVue = defineComponent({
asyncComputed: {
x () {
return Promise.resolve(1)
}
}
})
const vm = newVue({ extends: SubVue })
expect(vm.x).toBeNull()
await vm.$nextTick()
expect(vm.x).toBe(1)
})
test("Async computed values can be calculated lazily", async () => {
let called = false
const vm = newVue({
asyncComputed: {
a: {
lazy: true,
get () {
called = true
return Promise.resolve(10)
}
}
}
})
expect(called).toBe(false)
await vm.$nextTick()
expect(called).toBe(false)
expect(vm.a).toBe(null)
expect(vm.a).toBe(null)
expect(called).toBe(false)
await vm.$nextTick()
expect(called).toBe(true)
expect(vm.a).toBe(10)
})
test("Async computed values aren't lazy with { lazy: false }", async () => {
let called = false
const vm = newVue({
asyncComputed: {
a: {
lazy: false,
get () {
called = true
return Promise.resolve(10)
}
}
}
})
expect(called).toBe(true)
expect(vm.a).toBeNull()
await vm.$nextTick()
expect(called).toBe(true)
expect(vm.a).toBe(10)
})
test("Async computed values can be calculated lazily with a default", async () => {
let called = false
const vm = newVue({
asyncComputed: {
a: {
lazy: true,
default: 3,
get () {
called = true
return Promise.resolve(4)
}
}
}
})
expect(called).toBe(false)
await vm.$nextTick()
expect(called).toBe(false)
expect(vm.a).toBe(3)
expect(vm.a).toBe(3)
expect(called).toBe(false)
await vm.$nextTick()
expect(called).toBe(true)
expect(vm.a).toBe(4)
})
test("Underscore prefixes work (issue #33)", async () => {
const vm = newVue({
computed: {
sync_a () {
return 1
},
_sync_b () {
return 2
}
},
asyncComputed: {
_async_a () {
return new Promise(resolve => {
setTimeout(() => resolve(this.sync_a), 10)
})
},
async_b () {
return new Promise(resolve => {
setTimeout(() => resolve(this._sync_b), 10)
})
}
}
})
expect(vm._async_a).toBeNull()
expect(vm.async_b).toBeNull()
// _async_a is not reactive, because
// it begins with an underscore
await vi.advanceTimersByTimeAsync(10)
expect(vm._async_a).toBe(1)
expect(vm.async_b).toBe(2)
})
test("shouldUpdate works with lazy", async () => {
const vm = newVue({
data: {
a: 0,
x: true,
y: false,
},
asyncComputed: {
b: {
lazy: true,
get () {
return Promise.resolve(this.a)
},
shouldUpdate () {
return this.x
}
},
c: {
lazy: true,
get () {
return Promise.resolve(this.a)
},
shouldUpdate () {
return this.y
}
}
}
})
await vm.$nextTick()
expect(vm.b).toBe(null)
expect(vm.c).toBe(null)
await vm.$nextTick()
expect(vm.b).toBe(0)
expect(vm.c).toBe(null)
vm.a++
await vm.$nextTick()
expect(vm.b).toBe(1)
expect(vm.c).toBe(null)
vm.x = false
vm.y = true
vm.a++
await vm.$nextTick()
expect(vm.b).toBe(1)
expect(vm.c).toBe(2)
})
test("$asyncComputed is empty if there are no async computed properties", () => {
const vm = newVue({
})
expect(vm.$asyncComputed).toStrictEqual({})
})
test("$asyncComputed[name] is created for all async computed properties", async () => {
const vm = newVue({
asyncComputed: {
a () {
return Promise.resolve(1)
},
b () {
return Promise.resolve(2)
}
}
})
expect(Object.keys(vm.$asyncComputed)).toStrictEqual(['a', 'b'])
expect(vm.$asyncComputed.a.state).toBe('updating')
expect(vm.$asyncComputed.b.state).toBe('updating')
expect(vm.$asyncComputed.a.updating).toBe(true)
expect(vm.$asyncComputed.a.success).toBe(false)
expect(vm.$asyncComputed.a.error).toBe(false)
expect(vm.$asyncComputed.a.exception).toBe(null)
await vm.$nextTick()
expect(vm.a).toBe(1)
expect(vm.b).toBe(2)
expect(vm.$asyncComputed.a.state).toBe('success')
expect(vm.$asyncComputed.b.state).toBe('success')
expect(vm.$asyncComputed.a.updating).toBe(false)
expect(vm.$asyncComputed.a.success).toBe(true)
expect(vm.$asyncComputed.a.error).toBe(false)
expect(vm.$asyncComputed.a.exception).toBe(null)
})
test("$asyncComputed[name] handles errors and captures exceptions", async () => {
const errorHandler = vi.fn()
const vm = newVue({
asyncComputed: {
a () {
// eslint-disable-next-line prefer-promise-reject-errors
return Promise.reject('error-message')
}
}
}, { errorHandler })
expect(vm.$asyncComputed.a.state).toBe('updating')
await vm.$nextTick()
await vm.$nextTick()
expect(errorHandler).toHaveBeenCalledTimes(1)
expect(vm.a).toBe(null)
expect(vm.$asyncComputed.a.state).toBe('error')
expect(vm.$asyncComputed.a.updating).toBe(false)
expect(vm.$asyncComputed.a.success).toBe(false)
expect(vm.$asyncComputed.a.error).toBe(true)
expect(vm.$asyncComputed.a.exception).toBe('error-message')
})
test("$asyncComputed[name].update triggers re-evaluation", async () => {
let valueToReturn = 1
const vm = newVue({
asyncComputed: {
a () {
return new Promise(resolve => {
resolve(valueToReturn)
})
}
}
})
await vm.$nextTick()
expect(vm.a).toBe(1)
valueToReturn = 2
expect(vm.$asyncComputed.a.state).toBe('success')
vm.$asyncComputed.a.update()
expect(vm.$asyncComputed.a.state).toBe('updating')
await vm.$nextTick()
expect(vm.a).toBe(2)
valueToReturn = 3
expect(vm.a).toBe(2)
})
test("$asyncComputed[name].update has the correct execution context", async () => {
let addedValue = 1
const vm = newVue({
data () {
return {
valueToReturn: 1,
}
},
asyncComputed: {
a () {
return new Promise(resolve => {
resolve(this.valueToReturn + addedValue)
})
},
b: {
get () {
return new Promise(resolve => {
resolve(this.valueToReturn + addedValue)
})
},
},
},
})
await vm.$nextTick()
// case 1: a is a function
expect(vm.a).toBe(2)
expect(vm.$asyncComputed.a.state).toBe('success')
// case 2: b is an object with a getter function
expect(vm.b).toBe(2)
expect(vm.$asyncComputed.b.state).toBe('success')
addedValue = 4
vm.$asyncComputed.a.update()
expect(vm.$asyncComputed.a.state).toBe('updating')
vm.$asyncComputed.b.update()
expect(vm.$asyncComputed.b.state).toBe('updating')
await vm.$nextTick()
expect(vm.a).toBe(5)
expect(vm.b).toBe(5)
})
test("Plain components with neither `data` nor `asyncComputed` still work (issue #50)", async () => {
const vm = newVue({
computed: {
a () {
return 1
}
}
})
expect(vm.a).toBe(1)
})
test('Data of component still work as function and got vm', async () => {
let _vmContext = null
const vm = newVue({
data (vmContext) {
_vmContext = vmContext
},
asyncComputed: {
async a () {
return Promise.resolve(1)
},
},
})
expect(vm).toBe(_vmContext)
})
test("Watch as a function", async () => {
let i = 0
const vm = newVue({
data: {
y: 2,
obj: {
t: 0
}
},
asyncComputed: {
z: {
get () {
return Promise.resolve(i + this.y)
},
watch () {
// eslint-disable-next-line no-unused-expressions
this.obj.t
}
}
}
})
expect(vm.z).toBeNull()
await vm.$nextTick()
expect(vm.z).toBe(2)
i++
vm.obj.t--
await vm.$nextTick()
// This tick, Vue registers the change
// in the watcher, and reevaluates
// the getter function
// And since we 'await', the promise chain
// is finished and z is 3
expect(vm.z).toBe(3)
})
test("Watchers as array with nested path rerun the computation when a value changes", async () => {
let i = 0
const vm = newVue({
data: {
y: 2,
obj: {
t: 0
}
},
asyncComputed: {
z: {
get () {
return Promise.resolve(i + this.y)
},
watch: ['obj.t']
}
}
})
expect(vm.z).toBeNull()
await vm.$nextTick()
expect(vm.z).toBe(2)
i++
vm.obj.t--
await vm.$nextTick()
// This tick, Vue registers the change
// in the watcher, and reevaluates
// the getter function
// And since we 'await', the promise chain
// is finished and z is 3
expect(vm.z).toBe(3)
})
test("Watch as array with more then one value", async () => {
let i = 0
const vm = newVue({
data: {
y: 2,
obj: {
t: 0
},
r: 0
},
asyncComputed: {
z: {
get () {
return Promise.resolve(i + this.y)
},
watch: ['obj.t', 'r']
}
}
})
expect(vm.z).toBeNull()
await vm.$nextTick()
expect(vm.z).toBe(2)
i++
// checking for nested property
vm.obj.t--
await vm.$nextTick()
// This tick, Vue registers the change
// in the watcher, and reevaluates
// the getter function
// And since we 'await', the promise chain
// is finished and z is 3
expect(vm.z).toBe(3)
i++
// one level and multiple watchers
vm.r--
await vm.$nextTick()
expect(vm.z).toBe(4)
})
test("$asyncComputed[name].state resolves to 'success' even if the computed value is 0 (issue #75)", async () => {
const vm = newVue({
computed: {
isUpdating () {
return this.$asyncComputed.a.updating
}
},
asyncComputed: {
a: {
async get () {
return 0
},
default: null
}
}
})
expect(vm.$asyncComputed.a.state).toBe('updating')
expect(vm.$asyncComputed.a.updating).toBe(true)
expect(vm.$asyncComputed.a.success).toBe(false)
expect(vm.$asyncComputed.a.error).toBe(false)
expect(vm.$asyncComputed.a.exception).toBe(null)
expect(vm.isUpdating).toBe(true)
await vm.$nextTick()
expect(vm.a).toBe(0)
expect(vm.$asyncComputed.a.state).toBe('success')
expect(vm.$asyncComputed.a.updating).toBe(false)
expect(vm.$asyncComputed.a.success).toBe(true)
expect(vm.$asyncComputed.a.error).toBe(false)
expect(vm.$asyncComputed.a.exception).toBe(null)
expect(vm.isUpdating).toBe(false)
})
test("$asyncComputed[name].update does nothing if called after the component is destroyed", async () => {
let i = 0
const vm = newVue({
asyncComputed: {
a: {
async get () {
return ++i
}
}
}
})
expect(vm.a).toBeNull()
await vm.$nextTick()
expect(vm.a).toBe(1)
app.unmount(vm)
vm.$asyncComputed.a.update()
await vm.$nextTick()
expect(i).toBe(1)
expect(vm.a).toBe(1)
})