Skip to content

Commit

Permalink
fix(store): call metareducer with the user's config initial state
Browse files Browse the repository at this point in the history
  • Loading branch information
itayod committed Jan 15, 2019
1 parent d155d68 commit 411da54
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 11 deletions.
52 changes: 43 additions & 9 deletions modules/store/spec/store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
Action,
} from '../';
import { StoreConfig } from '../src/store_module';
import { combineReducers } from '../src/utils';
import {
counterReducer,
INCREMENT,
Expand Down Expand Up @@ -373,11 +374,11 @@ describe('ngRx Store', () => {
});

it('should dispatch an update reducers action when a feature is added', () => {
reducerManager.addFeature(
createFeature({
key: 'feature1',
})
);
reducerManager.addFeature({
key: 'feature1',
reducers: {},
reducerFactory: <any>combineReducers,
});

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

it('should dispatch an update reducers action when multiple features are added', () => {
reducerManager.addFeatures([
createFeature({
{
key: 'feature1',
}),
createFeature({
reducers: {},
reducerFactory: <any>combineReducers,
},
{
key: 'feature2',
}),
reducers: {},
reducerFactory: <any>combineReducers,
},
]);

expect(reducerManagerDispatcherSpy).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -546,13 +551,42 @@ describe('ngRx Store', () => {
}),
],
});

const mockStore = TestBed.get(Store);
const action = { type: INCREMENT };

mockStore.dispatch(action);

expect(metaReducerSpy1).toHaveBeenCalledWith(counterReducer);
expect(metaReducerSpy2).toHaveBeenCalledWith(counterReducer2);
});

it('should initial state with value', (done: DoneFn) => {
const counterInitialState = 2;
TestBed.configureTestingModule({
imports: [
StoreModule.forRoot({}),
StoreModule.forFeature(
'counterState',
{ counter: counterReducer },
{
initialState: { counter: counterInitialState },
metaReducers: [metaReducerContainer.metaReducer1],
}
),
],
});

const mockStore = TestBed.get(Store);

mockStore.pipe(take(1)).subscribe({
next(val: any) {
expect(val['counterState'].counter).toEqual(counterInitialState);
},
error: done,
complete: done,
});
});
});

describe('Feature config token', () => {
Expand Down
11 changes: 9 additions & 2 deletions modules/store/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ActionReducerFactory,
ActionReducerMap,
MetaReducer,
InitialState,
} from './models';

export function combineReducers<T, V extends Action = Action>(
Expand Down Expand Up @@ -92,10 +93,16 @@ export function createReducerFactory<T, V extends Action = Action>(
metaReducers?: MetaReducer<T, V>[]
): ActionReducerFactory<T, V> {
if (Array.isArray(metaReducers) && metaReducers.length > 0) {
return compose.apply(null, [...metaReducers, reducerFactory]);
reducerFactory = compose.apply(null, [...metaReducers, reducerFactory]);
}

return reducerFactory;
return (reducers: ActionReducerMap<T, V>, initialState?: InitialState<T>) => {
const reducer = reducerFactory(reducers, initialState);
return (state: T | undefined, action: V) => {
state = state === undefined ? (initialState as T) : state;
return reducer(state, action);
};
};
}

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

0 comments on commit 411da54

Please sign in to comment.