Skip to content

Commit

Permalink
Fixes nested computed properties issue
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrlplusb committed Jun 24, 2019
1 parent 261bc7b commit b6778b1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
33 changes: 33 additions & 0 deletions src/__tests__/issue217.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { action, computed, createStore } from '../index';

test('issue217', () => {
const model = {
items: {},

nested: {
numbers: [1, 2, 3],
filteredNumbers: computed(state => {
return state.numbers.filter(number => number > 1);
}),
},

// selectors
list: computed(items => Object.values(items), [state => state.items]),

// actions
fetched: action((state, payload) => {
state.items = payload.reduce((acc, todo) => {
acc[todo.id] = todo;
return acc;
}, {});
}),
};

const store = createStore(model);

// act
store.getActions().fetched([{ id: 1, text: 'foo' }]);

// assert
expect(store.getState().nested.filteredNumbers).toEqual([2, 3]);
});
4 changes: 3 additions & 1 deletion src/create-store-internals.js
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,9 @@ export default function createStoreInternals({
if (computedPropertyCreators) {
const updatedCurrent = get(path, updatedState);
Object.keys(computedPropertyCreators).forEach(key => {
computedPropertyCreators[key](updatedCurrent);
if (typeof computedPropertyCreators[key] === 'function') {
computedPropertyCreators[key](updatedCurrent);
}
});
}
}
Expand Down

0 comments on commit b6778b1

Please sign in to comment.