|
1 | 1 | import { omit } from '../src/utils';
|
2 |
| -import { combineReducers, compose } from '@ngrx/store'; |
| 2 | +import { |
| 3 | + ActionReducer, |
| 4 | + ActionReducerMap, |
| 5 | + combineReducers, |
| 6 | + compose, |
| 7 | + createReducerFactory, |
| 8 | +} from '@ngrx/store'; |
3 | 9 |
|
4 | 10 | describe(`Store utils`, () => {
|
5 | 11 | describe(`combineReducers()`, () => {
|
@@ -73,4 +79,61 @@ describe(`Store utils`, () => {
|
73 | 79 | expect(id(1)).toBe(1);
|
74 | 80 | });
|
75 | 81 | });
|
| 82 | + |
| 83 | + describe(`createReducerFactory()`, () => { |
| 84 | + const fruitReducer = (state: string = 'banana', action: any) => |
| 85 | + action.type === 'fruit' ? action.payload : state; |
| 86 | + type FruitState = { fruit: string }; |
| 87 | + const reducerMap: ActionReducerMap<FruitState> = { fruit: fruitReducer }; |
| 88 | + const initialState: FruitState = { fruit: 'apple' }; |
| 89 | + |
| 90 | + const runWithExpectations = ( |
| 91 | + metaReducers: any[], |
| 92 | + initialState: any, |
| 93 | + expectedState: any |
| 94 | + ) => () => { |
| 95 | + let spiedFactory: jasmine.Spy; |
| 96 | + let reducer: ActionReducer<FruitState>; |
| 97 | + beforeEach(() => { |
| 98 | + spiedFactory = jasmine |
| 99 | + .createSpy('spied factory') |
| 100 | + .and.callFake(combineReducers); |
| 101 | + reducer = createReducerFactory(spiedFactory, metaReducers)( |
| 102 | + reducerMap, |
| 103 | + initialState |
| 104 | + ); |
| 105 | + }); |
| 106 | + it(`should pass the reducers and initialState to the factory method`, () => { |
| 107 | + expect(spiedFactory).toHaveBeenCalledWith(reducerMap, initialState); |
| 108 | + }); |
| 109 | + it(`should return the expected initialState`, () => { |
| 110 | + expect(reducer(undefined, { type: 'init' })).toEqual(expectedState); |
| 111 | + }); |
| 112 | + }; |
| 113 | + |
| 114 | + describe(`without meta reducers`, () => { |
| 115 | + const metaReducers: any[] = []; |
| 116 | + describe( |
| 117 | + `with initial state`, |
| 118 | + runWithExpectations(metaReducers, initialState, initialState) |
| 119 | + ); |
| 120 | + describe( |
| 121 | + `without initial state`, |
| 122 | + runWithExpectations(metaReducers, undefined, { fruit: 'banana' }) |
| 123 | + ); |
| 124 | + }); |
| 125 | + |
| 126 | + describe(`with meta reducers`, () => { |
| 127 | + const noopMetaReducer = (r: any) => r; |
| 128 | + const metaReducers: any[] = [noopMetaReducer]; |
| 129 | + describe( |
| 130 | + `with initial state`, |
| 131 | + runWithExpectations(metaReducers, initialState, initialState) |
| 132 | + ); |
| 133 | + describe( |
| 134 | + `without initial state`, |
| 135 | + runWithExpectations(metaReducers, undefined, { fruit: 'banana' }) |
| 136 | + ); |
| 137 | + }); |
| 138 | + }); |
76 | 139 | });
|
0 commit comments