-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(store): infer initial store state properly with metareducers (#3102)
Closes #3007 BREAKING CHANGES: `initialState` needs to match the interface of the store/feature. BEFORE: Missing properties were valid ```ts StoreModule.forRoot(reducers, { initialState: { notExisting: 3 }, metaReducers: [metaReducer] }); ``` AFTER: A type error is produced for initialState that does not match the store/feature ```ts StoreModule.forRoot(reducers, { initialState: { notExisting: 3 }, metaReducers: [metaReducer] }); ```
- Loading branch information
1 parent
548c72c
commit d003b85
Showing
2 changed files
with
74 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { expecter } from 'ts-snippet'; | ||
import { compilerOptions } from './utils'; | ||
|
||
describe('StoreModule', () => { | ||
const expectSnippet = expecter( | ||
(code) => ` | ||
import {StoreModule,ActionReducerMap,Action} from '@ngrx/store'; | ||
interface State { | ||
featureA: object; | ||
featureB: object; | ||
} | ||
const reducers: ActionReducerMap<State, Action> = {} as ActionReducerMap< | ||
State, | ||
Action | ||
>; | ||
function metaReducer(reducer) { | ||
return (state, action) => { | ||
return reducer(state, action); | ||
}; | ||
} | ||
${code} | ||
`, | ||
compilerOptions() | ||
); | ||
|
||
describe('StoreModule.forRoot()', () => { | ||
it('accepts initialState and metaReducers with matching State', () => { | ||
expectSnippet(` | ||
StoreModule.forRoot(reducers, { | ||
initialState: { featureA: {} }, | ||
metaReducers: [metaReducer] | ||
}); | ||
`).toSucceed(); | ||
}); | ||
|
||
it("throws when initial state don't with store state", () => { | ||
expectSnippet(` | ||
StoreModule.forRoot(reducers, { | ||
initialState: { notExisting: 3 }, | ||
metaReducers: [metaReducer] | ||
}); | ||
`).toFail( | ||
/Type '{ notExisting: number; }' is not assignable to type 'InitialState<State>/ | ||
); | ||
}); | ||
}); | ||
|
||
describe('StoreModule.forFeature()', () => { | ||
it('accepts initialState and metaReducers with matching State', () => { | ||
expectSnippet(` | ||
StoreModule.forFeature('feature', reducers, { | ||
initialState: { featureA: {} }, | ||
metaReducers: [metaReducer] | ||
}); | ||
`).toSucceed(); | ||
}); | ||
|
||
it("throws when initial state don't with store state", () => { | ||
expectSnippet(` | ||
StoreModule.forFeature('feature', reducers, { | ||
initialState: { notExisting: 3 }, | ||
metaReducers: [metaReducer] | ||
}); | ||
`).toFail( | ||
/Type '{ notExisting: number; }' is not assignable to type 'InitialState<State>/ | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters