Skip to content

Commit 2aabe0f

Browse files
itayodbrandonroberts
authored andcommitted
fix(store): call metareducer with the user's config initial state (#1498)
Closes #1464
1 parent ac4fb88 commit 2aabe0f

File tree

3 files changed

+59
-19
lines changed

3 files changed

+59
-19
lines changed

modules/store/spec/modules.spec.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,13 @@ describe(`Store Modules`, () => {
9595
});
9696

9797
it(`should accept configurations`, () => {
98-
expect(featureAReducerFactory).toHaveBeenCalledWith(
99-
{ a: featureAReducer },
100-
featureAInitial()
101-
);
102-
expect(rootReducerFactory).toHaveBeenCalledWith(
103-
{ fruit: rootFruitReducer },
104-
rootInitial
105-
);
98+
expect(featureAReducerFactory).toHaveBeenCalledWith({
99+
a: featureAReducer,
100+
});
101+
102+
expect(rootReducerFactory).toHaveBeenCalledWith({
103+
fruit: rootFruitReducer,
104+
});
106105
});
107106

108107
it(`should should use config.reducerFactory`, () => {

modules/store/spec/store.spec.ts

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
Action,
1515
} from '../';
1616
import { StoreConfig } from '../src/store_module';
17+
import { combineReducers } from '../src/utils';
1718
import {
1819
counterReducer,
1920
INCREMENT,
@@ -373,11 +374,11 @@ describe('ngRx Store', () => {
373374
});
374375

375376
it('should dispatch an update reducers action when a feature is added', () => {
376-
reducerManager.addFeature(
377-
createFeature({
378-
key: 'feature1',
379-
})
380-
);
377+
reducerManager.addFeature({
378+
key: 'feature1',
379+
reducers: {},
380+
reducerFactory: <any>combineReducers,
381+
});
381382

382383
expect(reducerManagerDispatcherSpy).toHaveBeenCalledWith({
383384
type: UPDATE,
@@ -387,12 +388,16 @@ describe('ngRx Store', () => {
387388

388389
it('should dispatch an update reducers action when multiple features are added', () => {
389390
reducerManager.addFeatures([
390-
createFeature({
391+
{
391392
key: 'feature1',
392-
}),
393-
createFeature({
393+
reducers: {},
394+
reducerFactory: <any>combineReducers,
395+
},
396+
{
394397
key: 'feature2',
395-
}),
398+
reducers: {},
399+
reducerFactory: <any>combineReducers,
400+
},
396401
]);
397402

398403
expect(reducerManagerDispatcherSpy).toHaveBeenCalledTimes(1);
@@ -546,13 +551,42 @@ describe('ngRx Store', () => {
546551
}),
547552
],
548553
});
554+
549555
const mockStore = TestBed.get(Store);
550556
const action = { type: INCREMENT };
557+
551558
mockStore.dispatch(action);
552559

553560
expect(metaReducerSpy1).toHaveBeenCalledWith(counterReducer);
554561
expect(metaReducerSpy2).toHaveBeenCalledWith(counterReducer2);
555562
});
563+
564+
it('should initial state with value', (done: DoneFn) => {
565+
const counterInitialState = 2;
566+
TestBed.configureTestingModule({
567+
imports: [
568+
StoreModule.forRoot({}),
569+
StoreModule.forFeature(
570+
'counterState',
571+
{ counter: counterReducer },
572+
{
573+
initialState: { counter: counterInitialState },
574+
metaReducers: [metaReducerContainer.metaReducer1],
575+
}
576+
),
577+
],
578+
});
579+
580+
const mockStore = TestBed.get(Store);
581+
582+
mockStore.pipe(take(1)).subscribe({
583+
next(val: any) {
584+
expect(val['counterState'].counter).toEqual(counterInitialState);
585+
},
586+
error: done,
587+
complete: done,
588+
});
589+
});
556590
});
557591

558592
describe('Feature config token', () => {

modules/store/src/utils.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
ActionReducerFactory,
55
ActionReducerMap,
66
MetaReducer,
7+
InitialState,
78
} from './models';
89

910
export function combineReducers<T, V extends Action = Action>(
@@ -92,10 +93,16 @@ export function createReducerFactory<T, V extends Action = Action>(
9293
metaReducers?: MetaReducer<T, V>[]
9394
): ActionReducerFactory<T, V> {
9495
if (Array.isArray(metaReducers) && metaReducers.length > 0) {
95-
return compose.apply(null, [...metaReducers, reducerFactory]);
96+
reducerFactory = compose.apply(null, [...metaReducers, reducerFactory]);
9697
}
9798

98-
return reducerFactory;
99+
return (reducers: ActionReducerMap<T, V>, initialState?: InitialState<T>) => {
100+
const reducer = reducerFactory(reducers);
101+
return (state: T | undefined, action: V) => {
102+
state = state === undefined ? (initialState as T) : state;
103+
return reducer(state, action);
104+
};
105+
};
99106
}
100107

101108
export function createFeatureReducerFactory<T, V extends Action = Action>(

0 commit comments

Comments
 (0)