Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(utils): meta reducers conflicting with initial state #252

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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