Skip to content

Commit

Permalink
fix(Store): Set initial state for feature modules
Browse files Browse the repository at this point in the history
Closes #206, #233
  • Loading branch information
brandonroberts committed Aug 4, 2017
1 parent 2b1a076 commit b9ad9c7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
45 changes: 45 additions & 0 deletions modules/store/spec/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,49 @@ describe('ngRx Integration spec', () => {
expect(currentlyVisibleTodos.length).toBe(0);
});
});

describe('feature state', () => {
const initialState = {
todos: [
{
id: 1,
text: 'do things',
completed: false,
},
],
visibilityFilter: VisibilityFilters.SHOW_ALL,
};

const reducers: ActionReducerMap<TodoAppSchema, any> = {
todos: todos,
visibilityFilter: visibilityFilter,
};

const featureInitialState = [{ id: 1, completed: false, text: 'Item' }];

it('should initialize properly', () => {
TestBed.configureTestingModule({
imports: [
StoreModule.forRoot(reducers, { initialState }),
StoreModule.forFeature('items', todos, {
initialState: featureInitialState,
}),
],
});

const store: Store<any> = TestBed.get(Store);

let expected = [
{
todos: initialState.todos,
visibilityFilter: initialState.visibilityFilter,
items: featureInitialState,
},
];

store.select(state => state).subscribe(state => {
expect(state).toEqual(expected.shift());
});
});
});
});
5 changes: 5 additions & 0 deletions modules/store/src/reducer_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export class ReducerManager extends BehaviorSubject<ActionReducer<any, any>>
initialState
);

this.addInitialState(key, initialState);
this.addReducer(key, reducer);
}

Expand All @@ -65,6 +66,10 @@ export class ReducerManager extends BehaviorSubject<ActionReducer<any, any>>
this.updateReducers();
}

addInitialState(key: string, initialState: any) {
this.initialState = { ...this.initialState, [key]: initialState };
}

private updateReducers() {
this.next(this.reducerFactory(this.reducers, this.initialState));
this.dispatcher.next({ type: UPDATE });
Expand Down

0 comments on commit b9ad9c7

Please sign in to comment.