Skip to content

Commit 683013c

Browse files
rjokelaibrandonroberts
authored andcommitted
fix(Store): Update usage of compose for reducer factory (#252)
Closes #247
1 parent bd968fa commit 683013c

File tree

3 files changed

+101
-2
lines changed

3 files changed

+101
-2
lines changed

modules/store/spec/modules.spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,39 @@ describe(`Store Modules`, () => {
119119
});
120120
});
121121

122+
describe(`: With initial state`, () => {
123+
const initialState: RootState = { fruit: 'banana' };
124+
const reducerMap: ActionReducerMap<RootState> = { fruit: rootFruitReducer };
125+
const noopMetaReducer = (r: Function) => (state: any, action: any) => {
126+
return r(state, action);
127+
};
128+
129+
const testWithMetaReducers = (metaReducers: any[]) => () => {
130+
beforeEach(() => {
131+
TestBed.configureTestingModule({
132+
imports: [
133+
StoreModule.forRoot(reducerMap, { initialState, metaReducers }),
134+
],
135+
});
136+
store = TestBed.get(Store);
137+
});
138+
it('should have initial state', () => {
139+
store.take(1).subscribe((s: any) => {
140+
expect(s).toEqual(initialState);
141+
});
142+
});
143+
};
144+
145+
describe(
146+
'should add initial state with no meta reducers',
147+
testWithMetaReducers([])
148+
);
149+
describe(
150+
'should add initial state with a simple no-op meta reducer',
151+
testWithMetaReducers([noopMetaReducer])
152+
);
153+
});
154+
122155
describe(`: Nested`, () => {
123156
@NgModule({
124157
imports: [StoreModule.forFeature('a', featureAReducer)],

modules/store/spec/utils.spec.ts

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { omit } from '../src/utils';
2-
import { combineReducers, compose } from '@ngrx/store';
2+
import {
3+
ActionReducer,
4+
ActionReducerMap,
5+
combineReducers,
6+
compose,
7+
createReducerFactory,
8+
} from '@ngrx/store';
39

410
describe(`Store utils`, () => {
511
describe(`combineReducers()`, () => {
@@ -73,4 +79,61 @@ describe(`Store utils`, () => {
7379
expect(id(1)).toBe(1);
7480
});
7581
});
82+
83+
describe(`createReducerFactory()`, () => {
84+
const fruitReducer = (state: string = 'banana', action: any) =>
85+
action.type === 'fruit' ? action.payload : state;
86+
type FruitState = { fruit: string };
87+
const reducerMap: ActionReducerMap<FruitState> = { fruit: fruitReducer };
88+
const initialState: FruitState = { fruit: 'apple' };
89+
90+
const runWithExpectations = (
91+
metaReducers: any[],
92+
initialState: any,
93+
expectedState: any
94+
) => () => {
95+
let spiedFactory: jasmine.Spy;
96+
let reducer: ActionReducer<FruitState>;
97+
beforeEach(() => {
98+
spiedFactory = jasmine
99+
.createSpy('spied factory')
100+
.and.callFake(combineReducers);
101+
reducer = createReducerFactory(spiedFactory, metaReducers)(
102+
reducerMap,
103+
initialState
104+
);
105+
});
106+
it(`should pass the reducers and initialState to the factory method`, () => {
107+
expect(spiedFactory).toHaveBeenCalledWith(reducerMap, initialState);
108+
});
109+
it(`should return the expected initialState`, () => {
110+
expect(reducer(undefined, { type: 'init' })).toEqual(expectedState);
111+
});
112+
};
113+
114+
describe(`without meta reducers`, () => {
115+
const metaReducers: any[] = [];
116+
describe(
117+
`with initial state`,
118+
runWithExpectations(metaReducers, initialState, initialState)
119+
);
120+
describe(
121+
`without initial state`,
122+
runWithExpectations(metaReducers, undefined, { fruit: 'banana' })
123+
);
124+
});
125+
126+
describe(`with meta reducers`, () => {
127+
const noopMetaReducer = (r: any) => r;
128+
const metaReducers: any[] = [noopMetaReducer];
129+
describe(
130+
`with initial state`,
131+
runWithExpectations(metaReducers, initialState, initialState)
132+
);
133+
describe(
134+
`without initial state`,
135+
runWithExpectations(metaReducers, undefined, { fruit: 'banana' })
136+
);
137+
});
138+
});
76139
});

modules/store/src/utils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ export function createReducerFactory(
8989
metaReducers?: ActionReducer<any, any>[]
9090
): ActionReducerFactory<any, any> {
9191
if (Array.isArray(metaReducers) && metaReducers.length > 0) {
92-
return compose.apply(null, [...metaReducers, reducerFactory]);
92+
return compose(...metaReducers)(reducerFactory) as ActionReducerFactory<
93+
any,
94+
any
95+
>;
9396
}
9497

9598
return reducerFactory;

0 commit comments

Comments
 (0)