From 100a8ef9c83d4eff4f1eb74800bf49d90773f5ab Mon Sep 17 00:00:00 2001 From: Brandon Date: Fri, 13 Oct 2017 22:42:47 -0500 Subject: [PATCH] fix(Store): Set initial value for state action pair to object (#480) Closes #477 --- modules/store/spec/modules.spec.ts | 36 ++++++++++++++++++++++++++++++ modules/store/src/state.ts | 2 +- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/modules/store/spec/modules.spec.ts b/modules/store/spec/modules.spec.ts index 5a6690c691..13256ce0a6 100644 --- a/modules/store/spec/modules.spec.ts +++ b/modules/store/spec/modules.spec.ts @@ -119,6 +119,42 @@ describe(`Store Modules`, () => { }); }); + describe(`: With initial state`, () => { + const initialState: RootState = { fruit: 'banana' }; + const reducerMap: ActionReducerMap = { fruit: rootFruitReducer }; + const noopMetaReducer = (r: Function) => (state: any, action: any) => { + return r(state, action); + }; + + const testWithMetaReducers = (metaReducers: any[]) => () => { + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [ + StoreModule.forRoot(reducerMap, { initialState, metaReducers }), + ], + }); + + store = TestBed.get(Store); + }); + + it('should have initial state', () => { + store.take(1).subscribe((s: any) => { + expect(s).toEqual(initialState); + }); + }); + }; + + describe( + 'should add initial state with no meta-reducers', + testWithMetaReducers([]) + ); + + describe( + 'should add initial state with registered meta-reducers', + testWithMetaReducers([noopMetaReducer]) + ); + }); + describe(`: Nested`, () => { @NgModule({ imports: [StoreModule.forFeature('a', featureAReducer)], diff --git a/modules/store/src/state.ts b/modules/store/src/state.ts index ccf9d9b74b..1e0a26fd25 100644 --- a/modules/store/src/state.ts +++ b/modules/store/src/state.ts @@ -35,7 +35,7 @@ export class State extends BehaviorSubject implements OnDestroy { const stateAndAction$: Observable<{ state: any; action: Action; - }> = scan.call(withLatestReducer$, reduceState, initialState); + }> = scan.call(withLatestReducer$, reduceState, { state: initialState }); this.stateSubscription = stateAndAction$.subscribe({ next: ({ state, action }) => {