Skip to content

Commit

Permalink
Merge pull request #1 from JorgenEvens/bug/model-state-unaffected
Browse files Browse the repository at this point in the history
Fix state after returning new state object from reduce
  • Loading branch information
GertSallaerts authored Sep 18, 2023
2 parents e1bb8f6 + d266350 commit 4ee4a38
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ language: node_js
script: npm run lint && npm run coverage
after_script: cat ./coverage/lcov.info | coveralls
node_js:
- "5.11"
- "stable"
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"devDependencies": {
"alt-search-docs": "1.0.6",
"babel-cli": "6.6.5",
"babel-cli": "6.7.7",
"babel-core": "6.7.4",
"babel-eslint": "5.0.0",
"babel-loader": "6.2.4",
Expand Down
2 changes: 1 addition & 1 deletion src/store/AltStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
19 changes: 19 additions & 0 deletions test/functional-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
},
}
}

0 comments on commit 4ee4a38

Please sign in to comment.