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): Handling of @ngrx/store/update-reducers Action on Startup leads to strange behaviour for initialStates #909

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 1 addition & 1 deletion modules/store/spec/store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ describe('ngRx Store', () => {
store.removeReducer(key);
store.dispatch({ type: INCREMENT });
store.take(1).subscribe(val => {
expect(val.counter4).toBeUndefined();
expect(val.counter4).toBe(0);
});
});
});
Expand Down
12 changes: 12 additions & 0 deletions modules/store/spec/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ describe(`Store utils`, () => {
it(`should return original state if nothing changed`, () => {
expect(combination(initialState, { type: '' })).toBe(initialState);
});

it(`should return full initial state of the root when action '@ngrx/store/update-reducers' is running with no initial state`, () => {
const initialStateForReducer2Only = { reducer2: { y: 'bar' } };
const combinationForReducer1 = combineReducers(
{ reducer1 },
initialStateForReducer2Only as any
);
const updateAction1 = { type: 'state1', payload: 'foo' };
expect(combinationForReducer1(undefined, updateAction1)).toEqual(
initialState
);
});
});

describe(`omit()`, () => {
Expand Down
5 changes: 4 additions & 1 deletion modules/store/src/reducer_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ export class ReducerManager extends BehaviorSubject<ActionReducer<any, any>>

private updateReducers(key: string) {
this.next(this.reducerFactory(this.reducers, this.initialState));
this.dispatcher.next(<Action & {feature: string}>{ type: UPDATE, feature: key });
this.dispatcher.next(<Action & { feature: string }>{
type: UPDATE,
feature: key,
});
}

ngOnDestroy() {
Expand Down
2 changes: 1 addition & 1 deletion modules/store/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function combineReducers(
nextState[key] = nextStateForKey;
hasChanged = hasChanged || nextStateForKey !== previousStateForKey;
}
return hasChanged ? nextState : state;
return hasChanged ? { ...state, ...nextState } : state;
};
}

Expand Down