From 37b8e1bbc0c69218c24827b6023c995c2f69cc20 Mon Sep 17 00:00:00 2001 From: Jorgen Evens Date: Tue, 26 Apr 2016 23:18:33 +0200 Subject: [PATCH] Fix state after returning new state object from reduce. --- src/store/AltStore.js | 2 +- test/functional-test.js | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/store/AltStore.js b/src/store/AltStore.js index c2e70cdd..9557d0c6 100644 --- a/src/store/AltStore.js +++ b/src/store/AltStore.js @@ -72,7 +72,7 @@ class AltStore { if (model.reduce) { handleDispatch(() => { const value = model.reduce(this.state, payload) - if (value !== undefined) this.state = value + if (value !== undefined) this.state = model.state = value }, payload) if (!this.preventDefault) this.emitChange() } diff --git a/test/functional-test.js b/test/functional-test.js index d766c999..9b0c41fb 100644 --- a/test/functional-test.js +++ b/test/functional-test.js @@ -171,5 +171,24 @@ export default { assert(store.reduce(store.state).x === 0) assert(store2.reduce(store2.state).x === 1) }, + + 'state returned from reduce is set correctly'() { + const alt = new Alt() + const actions = alt.generateActions('test'); + + const store = alt.createStore({ + displayName: 'store', + + state: { x: 0 }, + + reduce(state) { + assert(state === this.state, 'state matches this.state'); + return { ...state }; + } + }) + + actions.test(); + actions.test(); + }, } }