Skip to content

Commit b2f2514

Browse files
ktsnyyx990803
authored andcommitted
Expose getters into actions (vuejs#245)
* expose getters into actions (Fix vuejs#244) * improve getters test for hot reload
1 parent 17d80ff commit b2f2514

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ class Store {
119119
let res = handler({
120120
dispatch,
121121
commit,
122+
getters: store.getters,
122123
state: getNestedState(store.state, path)
123124
}, payload, cb)
124125
if (!isPromise(res)) {

test/unit/test.js

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,21 @@ describe('Vuex', () => {
184184
[TEST] (state, n) {
185185
state.a += n
186186
}
187+
},
188+
actions: {
189+
check ({ getters }, value) {
190+
// check for exposing getters into actions
191+
expect(getters.hasAny).to.equal(value)
192+
}
187193
}
188194
})
189195
expect(store.getters.hasAny).to.equal(false)
196+
store.dispatch('check', false)
197+
190198
store.commit(TEST, 1)
199+
191200
expect(store.getters.hasAny).to.equal(true)
201+
store.dispatch('check', true)
192202
})
193203

194204
it('dynamic module registration', () => {
@@ -799,23 +809,43 @@ describe('Vuex', () => {
799809
inc: state => state.count++
800810
},
801811
getters: {
802-
hasAny: state => state.count > 0
812+
count: state => state.count
813+
},
814+
actions: {
815+
check ({ getters }, value) {
816+
expect(getters.count).to.equal(value)
817+
}
803818
}
804819
})
805820

806821
const spy = sinon.spy()
807822
const vm = new Vue({
808823
computed: {
809-
a: () => store.getters.hasAny
824+
a: () => store.getters.count
810825
},
811826
watch: {
812827
a: spy
813828
}
814829
})
815830

816-
expect(vm.a).to.equal(false)
831+
expect(vm.a).to.equal(0)
832+
store.dispatch('check', 0)
833+
817834
store.commit('inc')
818-
expect(vm.a).to.equal(true)
835+
836+
expect(vm.a).to.equal(1)
837+
store.dispatch('check', 1)
838+
839+
// update getters
840+
store.hotUpdate({
841+
getters: {
842+
count: state => state.count * 10
843+
}
844+
})
845+
846+
expect(vm.a).to.equal(10)
847+
store.dispatch('check', 10)
848+
819849
Vue.nextTick(() => {
820850
expect(spy).to.have.been.called
821851
done()

0 commit comments

Comments
 (0)