|
| 1 | +import { TestBed } from '@angular/core/testing'; |
| 2 | +import { |
| 3 | + ActionReducer, |
| 4 | + MetaReducer, |
| 5 | + StoreConfig, |
| 6 | + StoreModule, |
| 7 | +} from '@ngrx/store'; |
| 8 | + |
| 9 | +import { daffComposeReducers } from './compose'; |
| 10 | +import { createInjectableReducersTokens } from './injectable-reducers.factory'; |
| 11 | +import { InjectableReducersTokens } from './injectable-reducers.type'; |
| 12 | + |
| 13 | +interface State { |
| 14 | + test: string; |
| 15 | +} |
| 16 | + |
| 17 | +describe('@daffodil/core/state | createInjectableReducersTokens', () => { |
| 18 | + let metaReducers: Array<MetaReducer<State>>; |
| 19 | + let config: StoreConfig<State>; |
| 20 | + let tokens: InjectableReducersTokens<State>; |
| 21 | + let reducers: ActionReducer<State>; |
| 22 | + let extraReducer: ActionReducer<State>; |
| 23 | + let reducer: ActionReducer<State>; |
| 24 | + let result: State; |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + const initialState: State = { |
| 28 | + test: 'test', |
| 29 | + }; |
| 30 | + extraReducer = (state, action) => ({ |
| 31 | + ...state, |
| 32 | + test: `${state?.test} extra`, |
| 33 | + }); |
| 34 | + metaReducers = [ |
| 35 | + r => r, |
| 36 | + r => r, |
| 37 | + ]; |
| 38 | + reducers = daffComposeReducers([ |
| 39 | + (state = initialState, action) => state, |
| 40 | + (state = initialState, action) => state, |
| 41 | + ]); |
| 42 | + tokens = createInjectableReducersTokens<State>('TEST', () => reducers); |
| 43 | + |
| 44 | + TestBed.configureTestingModule({ |
| 45 | + imports: [ |
| 46 | + StoreModule.forRoot(), |
| 47 | + StoreModule.forFeature('test', tokens.reducers.token, tokens.config.token), |
| 48 | + ], |
| 49 | + providers: [ |
| 50 | + ...tokens.meta.provider(...metaReducers), |
| 51 | + ...tokens.extra.provider(extraReducer), |
| 52 | + ], |
| 53 | + }); |
| 54 | + |
| 55 | + reducer = TestBed.inject(tokens.reducers.token); |
| 56 | + config = TestBed.inject(tokens.config.token); |
| 57 | + result = reducer(initialState, { type: '' }); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should return the meta-reducers', () => { |
| 61 | + expect(TestBed.inject(tokens.config.token).metaReducers).toEqual(jasmine.arrayContaining(metaReducers)); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should run the extra reducer after the daffodil reducers', () => { |
| 65 | + expect(result.test).toEqual('test extra'); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should provide the extra reducers to the token', () => { |
| 69 | + expect(TestBed.inject(tokens.extra.token)).toContain(extraReducer); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should provide the meta-reducers to the token', () => { |
| 73 | + metaReducers.forEach(metaReducer => { |
| 74 | + expect(TestBed.inject(tokens.meta.token)).toContain(metaReducer); |
| 75 | + }); |
| 76 | + }); |
| 77 | +}); |
0 commit comments