Skip to content

Commit

Permalink
fix(Store): only default to initialValue when store value is undefined (
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard authored and MikeRyanDev committed Mar 3, 2018
1 parent cb173f6 commit 51a1547
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
70 changes: 70 additions & 0 deletions modules/store/spec/store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,76 @@ describe('ngRx Store', () => {
complete: done,
});
});

function testInitialState(feature?: string) {
store = TestBed.get(Store);
dispatcher = TestBed.get(ActionsSubject);

const actionSequence = '--a--b--c--d--e--f--g';
const stateSequence = 'i-w-----x-----y--z---';
const actionValues = {
a: { type: INCREMENT },
b: { type: 'OTHER' },
c: { type: RESET },
d: { type: 'OTHER' }, //reproduces https://github.com/ngrx/platform/issues/880 because state is falsey
e: { type: INCREMENT },
f: { type: INCREMENT },
g: { type: 'OTHER' },
};
const counterSteps = hot(actionSequence, actionValues);
counterSteps.subscribe(action => store.dispatch(action));

const counterStateWithString = feature
? (store as any).select(feature, 'counter1')
: store.select('counter1');

const counter1Values = { i: 1, w: 2, x: 0, y: 1, z: 2 };

expect(counterStateWithString).toBeObservable(
hot(stateSequence, counter1Values)
);
}

it('should reset to initial state when undefined (root ActionReducerMap)', () => {
TestBed.configureTestingModule({
imports: [
StoreModule.forRoot(
{ counter1: counterReducer },
{ initialState: { counter1: 1 } }
),
],
});

testInitialState();
});

it('should reset to initial state when undefined (feature ActionReducer)', () => {
TestBed.configureTestingModule({
imports: [
StoreModule.forRoot({}),
StoreModule.forFeature('counter1', counterReducer, {
initialState: 1,
}),
],
});

testInitialState();
});

it('should reset to initial state when undefined (feature ActionReducerMap)', () => {
TestBed.configureTestingModule({
imports: [
StoreModule.forRoot({}),
StoreModule.forFeature(
'feature1',
{ counter1: counterReducer },
{ initialState: { counter1: 1 } }
),
],
});

testInitialState('feature1');
});
});

describe('basic store actions', () => {
Expand Down
2 changes: 1 addition & 1 deletion modules/store/src/reducer_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class ReducerManager extends BehaviorSubject<ActionReducer<any, any>>
typeof reducers === 'function'
? (state: any, action: any) =>
createFeatureReducer(reducers, metaReducers)(
state || initialState,
state === undefined ? initialState : state,
action
)
: createReducerFactory(reducerFactory, metaReducers)(
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 @@ -27,7 +27,7 @@ export function combineReducers(
const finalReducerKeys = Object.keys(finalReducers);

return function combination(state, action) {
state = state || initialState;
state = state === undefined ? initialState : state;
let hasChanged = false;
const nextState: any = {};
for (let i = 0; i < finalReducerKeys.length; i++) {
Expand Down

0 comments on commit 51a1547

Please sign in to comment.