Skip to content

Commit b557526

Browse files
committed
test case for shared getter evaluation
1 parent 0c01cb7 commit b557526

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

test/unit/test.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,4 +324,66 @@ describe('Vuex', () => {
324324
expect(vm.a).to.equal(6)
325325
expect(store.state.a).to.equal(6)
326326
})
327+
328+
it('shared getters should evaluate only once', function (done) {
329+
const store = new Vuex.Store({
330+
state: {
331+
a: 1
332+
},
333+
mutations: {
334+
[TEST] (state) {
335+
state.a++
336+
}
337+
}
338+
})
339+
340+
let getterCalls = 0
341+
let watcherCalls = 0
342+
const getter = state => {
343+
getterCalls++
344+
return state.a
345+
}
346+
347+
const vm1 = new Vue({
348+
store,
349+
vuex: {
350+
getters: {
351+
a: getter
352+
}
353+
},
354+
watch: {
355+
a: () => {
356+
watcherCalls++
357+
}
358+
}
359+
})
360+
361+
const vm2 = new Vue({
362+
store,
363+
vuex: {
364+
getters: {
365+
a: getter
366+
}
367+
},
368+
watch: {
369+
a: () => {
370+
watcherCalls++
371+
}
372+
}
373+
})
374+
375+
expect(vm1.a).to.equal(1)
376+
expect(vm2.a).to.equal(1)
377+
expect(getterCalls).to.equal(1)
378+
expect(watcherCalls).to.equal(0)
379+
380+
store.dispatch('TEST')
381+
Vue.nextTick(() => {
382+
expect(vm1.a).to.equal(2)
383+
expect(vm2.a).to.equal(2)
384+
expect(getterCalls).to.equal(2)
385+
expect(watcherCalls).to.equal(2)
386+
done()
387+
})
388+
})
327389
})

0 commit comments

Comments
 (0)