Skip to content

Commit 4aec80c

Browse files
brandonrobertsMikeRyanDev
authored andcommitted
fix(Store): Set initial state for feature modules (#235)
Closes #206, #233
1 parent 2b1a076 commit 4aec80c

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export class ReducerManager extends BehaviorSubject<ActionReducer<any, any>>
4040
}: StoreFeature<any, any>) {
4141
const reducer =
4242
typeof reducers === 'function'
43-
? reducers
43+
? (state = initialState, action: any) => reducers(state, action)
4444
: createReducerFactory(reducerFactory, metaReducers)(
4545
reducers,
4646
initialState

0 commit comments

Comments
 (0)