Skip to content

Commit

Permalink
fix(Store): invoke meta reducers before user defined meta reducers
Browse files Browse the repository at this point in the history
  • Loading branch information
timdeschryver committed Mar 10, 2019
1 parent 16ad0e6 commit 38e9cc1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
42 changes: 42 additions & 0 deletions modules/store/spec/runtime_checks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
RuntimeChecks,
Store,
StoreModule,
META_REDUCERS,
} from '..';

describe('Runtime checks:', () => {
Expand Down Expand Up @@ -42,6 +43,47 @@ describe('Runtime checks:', () => {
});
});

describe('Order of meta reducers:', () => {
it('should invoke meta reducers before user defined meta reducers', () => {
let logs: string[] = [];
function metaReducerFactory(logMessage: string) {
return function metaReducer(reducer: any) {
return function(state: any, action: any) {
logs.push(logMessage);
return reducer(state, action);
};
};
}

TestBed.configureTestingModule({
imports: [
StoreModule.forRoot(
{},
{
metaReducers: [metaReducerFactory('user')],
}
),
],
providers: [
{
provide: META_REDUCERS,
useValue: metaReducerFactory('ngrx'),
multi: true,
},
],
});

const store: Store<any> = TestBed.get(Store);
const expected = ['ngrx', 'user'];

expect(logs).toEqual(expected);
logs = [];

store.dispatch({ type: 'foo' });
expect(logs).toEqual(expected);
});
});

describe('State Serialization:', () => {
const invalidAction = () => ({ type: ErrorTypes.UnserializableState });

Expand Down
16 changes: 7 additions & 9 deletions modules/store/src/store_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export class StoreModule {
},
{
provide: _RESOLVED_META_REDUCERS,
deps: [USER_PROVIDED_META_REDUCERS, META_REDUCERS],
deps: [META_REDUCERS, USER_PROVIDED_META_REDUCERS],
useFactory: _concatMetaReducers,
},
{
Expand Down Expand Up @@ -234,8 +234,7 @@ export class StoreModule {

export function _createStoreReducers(
injector: Injector,
reducers: ActionReducerMap<any, any>,
tokenReducers: ActionReducerMap<any, any>
reducers: ActionReducerMap<any, any>
) {
return reducers instanceof InjectionToken ? injector.get(reducers) : reducers;
}
Expand Down Expand Up @@ -263,10 +262,9 @@ export function _createFeatureStore(

export function _createFeatureReducers(
injector: Injector,
reducerCollection: ActionReducerMap<any, any>[],
tokenReducerCollection: ActionReducerMap<any, any>[]
reducerCollection: ActionReducerMap<any, any>[]
) {
const reducers = reducerCollection.map((reducer, index) => {
const reducers = reducerCollection.map(reducer => {
return reducer instanceof InjectionToken ? injector.get(reducer) : reducer;
});

Expand All @@ -282,8 +280,8 @@ export function _initialStateFactory(initialState: any): any {
}

export function _concatMetaReducers(
userProvidedMetaReducers: MetaReducer[],
metaReducers: MetaReducer[]
metaReducers: MetaReducer[],
userProvidedMetaReducers: MetaReducer[]
): MetaReducer[] {
return userProvidedMetaReducers.concat(metaReducers);
return metaReducers.concat(userProvidedMetaReducers);
}

0 comments on commit 38e9cc1

Please sign in to comment.