|
| 1 | +import { expecter } from 'ts-snippet'; |
| 2 | +import { compilerOptions } from './utils'; |
| 3 | + |
| 4 | +describe('StoreModule', () => { |
| 5 | + const expectSnippet = expecter( |
| 6 | + (code) => ` |
| 7 | + import {StoreModule,ActionReducerMap,Action} from '@ngrx/store'; |
| 8 | +
|
| 9 | + interface State { |
| 10 | + featureA: object; |
| 11 | + featureB: object; |
| 12 | + } |
| 13 | +
|
| 14 | + const reducers: ActionReducerMap<State, Action> = {} as ActionReducerMap< |
| 15 | + State, |
| 16 | + Action |
| 17 | + >; |
| 18 | +
|
| 19 | + function metaReducer(reducer) { |
| 20 | + return (state, action) => { |
| 21 | + return reducer(state, action); |
| 22 | + }; |
| 23 | + } |
| 24 | +
|
| 25 | + ${code} |
| 26 | + `, |
| 27 | + compilerOptions() |
| 28 | + ); |
| 29 | + |
| 30 | + describe('StoreModule.forRoot()', () => { |
| 31 | + it('accepts initialState and metaReducers with matching State', () => { |
| 32 | + expectSnippet(` |
| 33 | + StoreModule.forRoot(reducers, { |
| 34 | + initialState: { featureA: {} }, |
| 35 | + metaReducers: [metaReducer] |
| 36 | + }); |
| 37 | + `).toSucceed(); |
| 38 | + }); |
| 39 | + |
| 40 | + it("throws when initial state don't with store state", () => { |
| 41 | + expectSnippet(` |
| 42 | + StoreModule.forRoot(reducers, { |
| 43 | + initialState: { notExisting: 3 }, |
| 44 | + metaReducers: [metaReducer] |
| 45 | + }); |
| 46 | + `).toFail( |
| 47 | + /Type '{ notExisting: number; }' is not assignable to type 'InitialState<State>/ |
| 48 | + ); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + describe('StoreModule.forFeature()', () => { |
| 53 | + it('accepts initialState and metaReducers with matching State', () => { |
| 54 | + expectSnippet(` |
| 55 | + StoreModule.forFeature('feature', reducers, { |
| 56 | + initialState: { featureA: {} }, |
| 57 | + metaReducers: [metaReducer] |
| 58 | + }); |
| 59 | + `).toSucceed(); |
| 60 | + }); |
| 61 | + |
| 62 | + it("throws when initial state don't with store state", () => { |
| 63 | + expectSnippet(` |
| 64 | + StoreModule.forFeature('feature', reducers, { |
| 65 | + initialState: { notExisting: 3 }, |
| 66 | + metaReducers: [metaReducer] |
| 67 | + }); |
| 68 | + `).toFail( |
| 69 | + /Type '{ notExisting: number; }' is not assignable to type 'InitialState<State>/ |
| 70 | + ); |
| 71 | + }); |
| 72 | + }); |
| 73 | +}); |
0 commit comments