Skip to content

Commit 04ae660

Browse files
Merge branch 'tjallingt-master'
2 parents ac8fd36 + f2fa3d5 commit 04ae660

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

test/index.js

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,64 @@ test("shouldUpdate controls when to rerun the computation when a value changes",
404404
})
405405
})
406406

407+
test("Watchers trigger but shouldUpdate can still block their updates", t => {
408+
t.plan(6)
409+
let i = 0
410+
const vm = new Vue({
411+
data: {
412+
canUpdate: true,
413+
x: 0,
414+
y: 2,
415+
},
416+
asyncComputed: {
417+
z: {
418+
get () {
419+
return Promise.resolve(i + this.y)
420+
},
421+
watch () {
422+
// eslint-disable-next-line no-unused-expressions
423+
this.x
424+
},
425+
shouldUpdate () {
426+
return this.canUpdate
427+
}
428+
}
429+
}
430+
})
431+
t.equal(vm.z, null)
432+
Vue.nextTick(() => {
433+
t.equal(vm.z, 2)
434+
i++
435+
vm.x--
436+
Vue.nextTick(() => {
437+
// This tick, Vue registers the change
438+
// in the watcher, and reevaluates
439+
// the getter function
440+
t.equal(vm.z, 2)
441+
Vue.nextTick(() => {
442+
// Now in this tick the promise has
443+
// resolved, and z is 3.
444+
t.equal(vm.z, 3)
445+
// We stop all updates from now on
446+
vm.canUpdate = false
447+
i++
448+
vm.x--
449+
Vue.nextTick(() => {
450+
// This tick, Vue registers the change
451+
// in the watcher, and reevaluates
452+
// the getter function but no update
453+
t.equal(vm.z, 3)
454+
Vue.nextTick(() => {
455+
// Now in this tick the promise has
456+
// resolved, and z is still 3.
457+
t.equal(vm.z, 3)
458+
})
459+
})
460+
})
461+
})
462+
})
463+
})
464+
407465
test("The default default value can be set in the plugin options", t => {
408466
t.plan(2)
409467
pluginOptions.default = 53
@@ -658,4 +716,3 @@ test("shouldUpdate works with lazy", t => {
658716
})
659717
})
660718
})
661-

0 commit comments

Comments
 (0)