diff --git a/src/__tests__/issue217.test.js b/src/__tests__/issue217.test.js index 674f9c8c2..c32106103 100644 --- a/src/__tests__/issue217.test.js +++ b/src/__tests__/issue217.test.js @@ -2,7 +2,9 @@ import { action, computed, createStore } from '../index'; test('issue217', () => { const model = { - items: {}, + items: { + 1: 'foo', + }, nested: { numbers: [1, 2, 3], @@ -16,18 +18,17 @@ test('issue217', () => { // actions fetched: action((state, payload) => { - state.items = payload.reduce((acc, todo) => { - acc[todo.id] = todo; - return acc; - }, {}); + state.nested.numbers = payload; + state.items['1'] = 'bar'; }), }; const store = createStore(model); // act - store.getActions().fetched([{ id: 1, text: 'foo' }]); + store.getActions().fetched([4, 5, 6]); // assert - expect(store.getState().nested.filteredNumbers).toEqual([2, 3]); + expect(store.getState().nested.filteredNumbers).toEqual([4, 5, 6]); + expect(store.getState().list).toEqual(['bar']); }); diff --git a/src/create-store-internals.js b/src/create-store-internals.js index 2c5531e2d..35ad7ef1d 100644 --- a/src/create-store-internals.js +++ b/src/create-store-internals.js @@ -472,12 +472,20 @@ export default function createStoreInternals({ if (updatedState !== state) { const computedPropertyCreators = get(path, computedProperties); if (computedPropertyCreators) { - const updatedCurrent = get(path, updatedState); - Object.keys(computedPropertyCreators).forEach(key => { - if (typeof computedPropertyCreators[key] === 'function') { - computedPropertyCreators[key](updatedCurrent); - } - }); + const recursiveRebindComputedProperties = (currentPath, obj) => { + const updatedCurrent = get(currentPath, updatedState); + Object.keys(obj).forEach(key => { + if (typeof obj[key] === 'function') { + obj[key](updatedCurrent); + } else { + recursiveRebindComputedProperties( + [...currentPath, key], + obj[key], + ); + } + }); + }; + recursiveRebindComputedProperties(path, computedPropertyCreators); } }