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(store): call metareducer with the user's config initial state #1498

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
15 changes: 7 additions & 8 deletions modules/store/spec/modules.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,13 @@ describe(`Store Modules`, () => {
});

it(`should accept configurations`, () => {
expect(featureAReducerFactory).toHaveBeenCalledWith(
{ a: featureAReducer },
featureAInitial()
);
expect(rootReducerFactory).toHaveBeenCalledWith(
{ fruit: rootFruitReducer },
rootInitial
);
expect(featureAReducerFactory).toHaveBeenCalledWith({
a: featureAReducer,
});

expect(rootReducerFactory).toHaveBeenCalledWith({
fruit: rootFruitReducer,
});
});

it(`should should use config.reducerFactory`, () => {
Expand Down
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,
itayod marked this conversation as resolved.
Show resolved Hide resolved
});

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);
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