Skip to content

Commit b9ad9c7

Browse files
fix(Store): Set initial state for feature modules
Closes #206, #233
1 parent 2b1a076 commit b9ad9c7

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

modules/store/spec/integration.spec.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,49 @@ describe('ngRx Integration spec', () => {
181181
expect(currentlyVisibleTodos.length).toBe(0);
182182
});
183183
});
184+
185+
describe('feature state', () => {
186+
const initialState = {
187+
todos: [
188+
{
189+
id: 1,
190+
text: 'do things',
191+
completed: false,
192+
},
193+
],
194+
visibilityFilter: VisibilityFilters.SHOW_ALL,
195+
};
196+
197+
const reducers: ActionReducerMap<TodoAppSchema, any> = {
198+
todos: todos,
199+
visibilityFilter: visibilityFilter,
200+
};
201+
202+
const featureInitialState = [{ id: 1, completed: false, text: 'Item' }];
203+
204+
it('should initialize properly', () => {
205+
TestBed.configureTestingModule({
206+
imports: [
207+
StoreModule.forRoot(reducers, { initialState }),
208+
StoreModule.forFeature('items', todos, {
209+
initialState: featureInitialState,
210+
}),
211+
],
212+
});
213+
214+
const store: Store<any> = TestBed.get(Store);
215+
216+
let expected = [
217+
{
218+
todos: initialState.todos,
219+
visibilityFilter: initialState.visibilityFilter,
220+
items: featureInitialState,
221+
},
222+
];
223+
224+
store.select(state => state).subscribe(state => {
225+
expect(state).toEqual(expected.shift());
226+
});
227+
});
228+
});
184229
});

modules/store/src/reducer_manager.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export class ReducerManager extends BehaviorSubject<ActionReducer<any, any>>
4646
initialState
4747
);
4848

49+
this.addInitialState(key, initialState);
4950
this.addReducer(key, reducer);
5051
}
5152

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

69+
addInitialState(key: string, initialState: any) {
70+
this.initialState = { ...this.initialState, [key]: initialState };
71+
}
72+
6873
private updateReducers() {
6974
this.next(this.reducerFactory(this.reducers, this.initialState));
7075
this.dispatcher.next({ type: UPDATE });

0 commit comments

Comments
 (0)