Skip to content

Commit 1b81363

Browse files
LinusBorgyyx990803
authored andcommitted
Add store getters as second argument to store.watch() computed fn (solve #460) (#515)
* Batman! * the first parameter of store.watch(), which is a function, now receives the store.getters object as its second argument. * added unit test Fullfills request #460 * remove unused constant * reverse changes to /dist
1 parent 18fe031 commit 1b81363

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class Store {
124124

125125
watch (getter, cb, options) {
126126
assert(typeof getter === 'function', `store.watch only accepts a function.`)
127-
return this._watcherVM.$watch(() => getter(this.state), cb, options)
127+
return this._watcherVM.$watch(() => getter(this.state, this.getters), cb, options)
128128
}
129129

130130
replaceState (state) {

test/unit/test.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import Vuex, { mapState, mapMutations, mapGetters, mapActions } from '../../dist
55
Vue.use(Vuex)
66

77
const TEST = 'TEST'
8-
const TEST2 = 'TEST2'
98

109
describe('Vuex', () => {
1110
it('committing mutations', () => {
@@ -1282,4 +1281,36 @@ describe('Vuex', () => {
12821281
})
12831282
})
12841283
})
1284+
1285+
it('watch: getter function has access to store\'s getters object', done => {
1286+
const store = new Vuex.Store({
1287+
state: {
1288+
count: 0
1289+
},
1290+
mutations: {
1291+
[TEST]: state => state.count++
1292+
},
1293+
getters: {
1294+
getCount: state => state.count
1295+
}
1296+
})
1297+
1298+
const getter = function getter (state, getters) {
1299+
return state.count
1300+
}
1301+
const spy = spyOn({ getter }, 'getter').and.callThrough()
1302+
const spyCb = jasmine.createSpy()
1303+
1304+
store.watch(spy, spyCb)
1305+
1306+
Vue.nextTick(() => {
1307+
store.commit(TEST)
1308+
expect(store.state.count).toBe(1)
1309+
1310+
Vue.nextTick(() => {
1311+
expect(spy).toHaveBeenCalledWith(store.state, store.getters)
1312+
done()
1313+
})
1314+
})
1315+
})
12851316
})

0 commit comments

Comments
 (0)