Skip to content

Commit 51a1547

Browse files
jbedardMikeRyanDev
authored andcommitted
fix(Store): only default to initialValue when store value is undefined (#886)
1 parent cb173f6 commit 51a1547

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

modules/store/spec/store.spec.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,76 @@ describe('ngRx Store', () => {
6868
complete: done,
6969
});
7070
});
71+
72+
function testInitialState(feature?: string) {
73+
store = TestBed.get(Store);
74+
dispatcher = TestBed.get(ActionsSubject);
75+
76+
const actionSequence = '--a--b--c--d--e--f--g';
77+
const stateSequence = 'i-w-----x-----y--z---';
78+
const actionValues = {
79+
a: { type: INCREMENT },
80+
b: { type: 'OTHER' },
81+
c: { type: RESET },
82+
d: { type: 'OTHER' }, //reproduces https://github.com/ngrx/platform/issues/880 because state is falsey
83+
e: { type: INCREMENT },
84+
f: { type: INCREMENT },
85+
g: { type: 'OTHER' },
86+
};
87+
const counterSteps = hot(actionSequence, actionValues);
88+
counterSteps.subscribe(action => store.dispatch(action));
89+
90+
const counterStateWithString = feature
91+
? (store as any).select(feature, 'counter1')
92+
: store.select('counter1');
93+
94+
const counter1Values = { i: 1, w: 2, x: 0, y: 1, z: 2 };
95+
96+
expect(counterStateWithString).toBeObservable(
97+
hot(stateSequence, counter1Values)
98+
);
99+
}
100+
101+
it('should reset to initial state when undefined (root ActionReducerMap)', () => {
102+
TestBed.configureTestingModule({
103+
imports: [
104+
StoreModule.forRoot(
105+
{ counter1: counterReducer },
106+
{ initialState: { counter1: 1 } }
107+
),
108+
],
109+
});
110+
111+
testInitialState();
112+
});
113+
114+
it('should reset to initial state when undefined (feature ActionReducer)', () => {
115+
TestBed.configureTestingModule({
116+
imports: [
117+
StoreModule.forRoot({}),
118+
StoreModule.forFeature('counter1', counterReducer, {
119+
initialState: 1,
120+
}),
121+
],
122+
});
123+
124+
testInitialState();
125+
});
126+
127+
it('should reset to initial state when undefined (feature ActionReducerMap)', () => {
128+
TestBed.configureTestingModule({
129+
imports: [
130+
StoreModule.forRoot({}),
131+
StoreModule.forFeature(
132+
'feature1',
133+
{ counter1: counterReducer },
134+
{ initialState: { counter1: 1 } }
135+
),
136+
],
137+
});
138+
139+
testInitialState('feature1');
140+
});
71141
});
72142

73143
describe('basic store actions', () => {

modules/store/src/reducer_manager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export class ReducerManager extends BehaviorSubject<ActionReducer<any, any>>
4343
typeof reducers === 'function'
4444
? (state: any, action: any) =>
4545
createFeatureReducer(reducers, metaReducers)(
46-
state || initialState,
46+
state === undefined ? initialState : state,
4747
action
4848
)
4949
: createReducerFactory(reducerFactory, metaReducers)(

modules/store/src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function combineReducers(
2727
const finalReducerKeys = Object.keys(finalReducers);
2828

2929
return function combination(state, action) {
30-
state = state || initialState;
30+
state = state === undefined ? initialState : state;
3131
let hasChanged = false;
3232
const nextState: any = {};
3333
for (let i = 0; i < finalReducerKeys.length; i++) {

0 commit comments

Comments
 (0)