Skip to content

Commit

Permalink
fix(Store): Update usage of compose for reducer factory (#252)
Browse files Browse the repository at this point in the history
Closes #247
  • Loading branch information
rjokelai authored and brandonroberts committed Aug 11, 2017
1 parent bd968fa commit 683013c
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 2 deletions.
33 changes: 33 additions & 0 deletions modules/store/spec/modules.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,39 @@ describe(`Store Modules`, () => {
});
});

describe(`: With initial state`, () => {
const initialState: RootState = { fruit: 'banana' };
const reducerMap: ActionReducerMap<RootState> = { 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)],
Expand Down
65 changes: 64 additions & 1 deletion modules/store/spec/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -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()`, () => {
Expand Down Expand Up @@ -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<FruitState> = { fruit: fruitReducer };
const initialState: FruitState = { fruit: 'apple' };

const runWithExpectations = (
metaReducers: any[],
initialState: any,
expectedState: any
) => () => {
let spiedFactory: jasmine.Spy;
let reducer: ActionReducer<FruitState>;
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' })
);
});
});
});
5 changes: 4 additions & 1 deletion modules/store/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ export function createReducerFactory(
metaReducers?: ActionReducer<any, any>[]
): ActionReducerFactory<any, any> {
if (Array.isArray(metaReducers) && metaReducers.length > 0) {
return compose.apply(null, [...metaReducers, reducerFactory]);
return compose(...metaReducers)(reducerFactory) as ActionReducerFactory<
any,
any
>;
}

return reducerFactory;
Expand Down

0 comments on commit 683013c

Please sign in to comment.