diff --git a/modules/store/spec/modules.spec.ts b/modules/store/spec/modules.spec.ts index 5a6690c691..019896fbb0 100644 --- a/modules/store/spec/modules.spec.ts +++ b/modules/store/spec/modules.spec.ts @@ -119,6 +119,39 @@ 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 a simple no-op meta reducer', + testWithMetaReducers([noopMetaReducer]) + ); + }); + describe(`: Nested`, () => { @NgModule({ imports: [StoreModule.forFeature('a', featureAReducer)], diff --git a/modules/store/spec/utils.spec.ts b/modules/store/spec/utils.spec.ts index 9b2cd7ea28..63d6407198 100644 --- a/modules/store/spec/utils.spec.ts +++ b/modules/store/spec/utils.spec.ts @@ -1,5 +1,11 @@ import { omit } from '../src/utils'; -import { combineReducers, compose } from '@ngrx/store'; +import { + ActionReducer, + ActionReducerMap, + combineReducers, + compose, + createReducerFactory, +} from '@ngrx/store'; describe(`Store utils`, () => { describe(`combineReducers()`, () => { @@ -73,4 +79,61 @@ describe(`Store utils`, () => { expect(id(1)).toBe(1); }); }); + + describe(`createReducerFactory()`, () => { + const fruitReducer = (state: string = 'banana', action: any) => + action.type === 'fruit' ? action.payload : state; + type FruitState = { fruit: string }; + const reducerMap: ActionReducerMap = { fruit: fruitReducer }; + const initialState: FruitState = { fruit: 'apple' }; + + const runWithExpectations = ( + metaReducers: any[], + initialState: any, + expectedState: any + ) => () => { + let spiedFactory: jasmine.Spy; + let reducer: ActionReducer; + beforeEach(() => { + spiedFactory = jasmine + .createSpy('spied factory') + .and.callFake(combineReducers); + reducer = createReducerFactory(spiedFactory, metaReducers)( + reducerMap, + initialState + ); + }); + it(`should pass the reducers and initialState to the factory method`, () => { + expect(spiedFactory).toHaveBeenCalledWith(reducerMap, initialState); + }); + it(`should return the expected initialState`, () => { + expect(reducer(undefined, { type: 'init' })).toEqual(expectedState); + }); + }; + + describe(`without meta reducers`, () => { + const metaReducers: any[] = []; + describe( + `with initial state`, + runWithExpectations(metaReducers, initialState, initialState) + ); + describe( + `without initial state`, + runWithExpectations(metaReducers, undefined, { fruit: 'banana' }) + ); + }); + + describe(`with meta reducers`, () => { + const noopMetaReducer = (r: any) => r; + const metaReducers: any[] = [noopMetaReducer]; + describe( + `with initial state`, + runWithExpectations(metaReducers, initialState, initialState) + ); + describe( + `without initial state`, + runWithExpectations(metaReducers, undefined, { fruit: 'banana' }) + ); + }); + }); }); diff --git a/modules/store/src/utils.ts b/modules/store/src/utils.ts index a8a30c7642..856a79b705 100644 --- a/modules/store/src/utils.ts +++ b/modules/store/src/utils.ts @@ -89,7 +89,7 @@ export function createReducerFactory( metaReducers?: ActionReducer[] ): ActionReducerFactory { if (Array.isArray(metaReducers) && metaReducers.length > 0) { - return compose.apply(null, [...metaReducers, reducerFactory]); + return compose(...metaReducers)(reducerFactory) as any; } return reducerFactory;