From 4aec80c7f750cc325e9f898f1152618d78971b68 Mon Sep 17 00:00:00 2001 From: Brandon Date: Fri, 4 Aug 2017 09:15:18 -0500 Subject: [PATCH] fix(Store): Set initial state for feature modules (#235) Closes #206, #233 --- modules/store/spec/integration.spec.ts | 45 ++++++++++++++++++++++++++ modules/store/src/reducer_manager.ts | 2 +- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/modules/store/spec/integration.spec.ts b/modules/store/spec/integration.spec.ts index 16694a80dd..590c748e79 100644 --- a/modules/store/spec/integration.spec.ts +++ b/modules/store/spec/integration.spec.ts @@ -181,4 +181,49 @@ describe('ngRx Integration spec', () => { expect(currentlyVisibleTodos.length).toBe(0); }); }); + + describe('feature state', () => { + const initialState = { + todos: [ + { + id: 1, + text: 'do things', + completed: false, + }, + ], + visibilityFilter: VisibilityFilters.SHOW_ALL, + }; + + const reducers: ActionReducerMap = { + todos: todos, + visibilityFilter: visibilityFilter, + }; + + const featureInitialState = [{ id: 1, completed: false, text: 'Item' }]; + + it('should initialize properly', () => { + TestBed.configureTestingModule({ + imports: [ + StoreModule.forRoot(reducers, { initialState }), + StoreModule.forFeature('items', todos, { + initialState: featureInitialState, + }), + ], + }); + + const store: Store = TestBed.get(Store); + + let expected = [ + { + todos: initialState.todos, + visibilityFilter: initialState.visibilityFilter, + items: featureInitialState, + }, + ]; + + store.select(state => state).subscribe(state => { + expect(state).toEqual(expected.shift()); + }); + }); + }); }); diff --git a/modules/store/src/reducer_manager.ts b/modules/store/src/reducer_manager.ts index a3f0ee58f8..16054875a1 100644 --- a/modules/store/src/reducer_manager.ts +++ b/modules/store/src/reducer_manager.ts @@ -40,7 +40,7 @@ export class ReducerManager extends BehaviorSubject> }: StoreFeature) { const reducer = typeof reducers === 'function' - ? reducers + ? (state = initialState, action: any) => reducers(state, action) : createReducerFactory(reducerFactory, metaReducers)( reducers, initialState