|
| 1 | +import { ENVIRONMENT_INITIALIZER, inject, Injectable } from '@angular/core'; |
| 2 | +import { TestBed } from '@angular/core/testing'; |
| 3 | +import { map } from 'rxjs'; |
| 4 | +import { |
| 5 | + createAction, |
| 6 | + createFeatureSelector, |
| 7 | + createReducer, |
| 8 | + props, |
| 9 | + provideState, |
| 10 | + provideStore, |
| 11 | + Store, |
| 12 | +} from '@ngrx/store'; |
| 13 | +import { |
| 14 | + Actions, |
| 15 | + concatLatestFrom, |
| 16 | + createEffect, |
| 17 | + EffectsRunner, |
| 18 | + ofType, |
| 19 | + provideEffects, |
| 20 | + rootEffectsInit, |
| 21 | +} from '../src/index'; |
| 22 | + |
| 23 | +describe('provideEffects', () => { |
| 24 | + it('starts effects runner when called first time', () => { |
| 25 | + TestBed.configureTestingModule({ |
| 26 | + providers: [ |
| 27 | + { |
| 28 | + provide: ENVIRONMENT_INITIALIZER, |
| 29 | + multi: true, |
| 30 | + useValue: () => jest.spyOn(inject(EffectsRunner), 'start'), |
| 31 | + }, |
| 32 | + provideStore({}).ɵproviders, |
| 33 | + // provide effects twice |
| 34 | + provideEffects([]).ɵproviders, |
| 35 | + provideEffects([]).ɵproviders, |
| 36 | + ], |
| 37 | + }); |
| 38 | + |
| 39 | + const effectsRunner = TestBed.inject(EffectsRunner); |
| 40 | + expect(effectsRunner.start).toHaveBeenCalledTimes(1); |
| 41 | + }); |
| 42 | + |
| 43 | + it('dispatches effects init action when called first time', () => { |
| 44 | + TestBed.configureTestingModule({ |
| 45 | + providers: [ |
| 46 | + { |
| 47 | + provide: ENVIRONMENT_INITIALIZER, |
| 48 | + multi: true, |
| 49 | + useValue: () => jest.spyOn(inject(Store), 'dispatch'), |
| 50 | + }, |
| 51 | + provideStore().ɵproviders, |
| 52 | + // provide effects twice |
| 53 | + provideEffects([]).ɵproviders, |
| 54 | + provideEffects([]).ɵproviders, |
| 55 | + ], |
| 56 | + }); |
| 57 | + |
| 58 | + const store = TestBed.inject(Store); |
| 59 | + expect(store.dispatch).toHaveBeenCalledWith(rootEffectsInit()); |
| 60 | + expect(store.dispatch).toHaveBeenCalledTimes(1); |
| 61 | + }); |
| 62 | + |
| 63 | + it('throws an error when store is not provided', () => { |
| 64 | + TestBed.configureTestingModule({ |
| 65 | + // provide only effects |
| 66 | + providers: [provideEffects([TestEffects]).ɵproviders], |
| 67 | + }); |
| 68 | + |
| 69 | + expect(() => TestBed.inject(TestEffects)).toThrowError(); |
| 70 | + }); |
| 71 | + |
| 72 | + it('runs provided effects', (done) => { |
| 73 | + TestBed.configureTestingModule({ |
| 74 | + providers: [ |
| 75 | + provideStore().ɵproviders, |
| 76 | + provideEffects([TestEffects]).ɵproviders, |
| 77 | + ], |
| 78 | + }); |
| 79 | + |
| 80 | + const store = TestBed.inject(Store); |
| 81 | + const effects = TestBed.inject(TestEffects); |
| 82 | + |
| 83 | + effects.simpleEffect$.subscribe((action) => { |
| 84 | + expect(action).toEqual(simpleEffectDone()); |
| 85 | + done(); |
| 86 | + }); |
| 87 | + |
| 88 | + store.dispatch(simpleEffectTest()); |
| 89 | + }); |
| 90 | + |
| 91 | + it('runs provided effects after root state registration', (done) => { |
| 92 | + TestBed.configureTestingModule({ |
| 93 | + providers: [ |
| 94 | + provideEffects([TestEffects]).ɵproviders, |
| 95 | + // provide store after effects |
| 96 | + provideStore({ [rootSliceKey]: createReducer('ngrx') }).ɵproviders, |
| 97 | + ], |
| 98 | + }); |
| 99 | + |
| 100 | + const store = TestBed.inject(Store); |
| 101 | + const effects = TestBed.inject(TestEffects); |
| 102 | + |
| 103 | + effects.effectWithRootState$.subscribe((action) => { |
| 104 | + expect(action).toEqual( |
| 105 | + effectWithRootStateDone({ [rootSliceKey]: 'ngrx' }) |
| 106 | + ); |
| 107 | + done(); |
| 108 | + }); |
| 109 | + |
| 110 | + store.dispatch(effectWithRootStateTest()); |
| 111 | + }); |
| 112 | + |
| 113 | + it('runs provided effects after feature state registration', (done) => { |
| 114 | + TestBed.configureTestingModule({ |
| 115 | + providers: [ |
| 116 | + provideStore().ɵproviders, |
| 117 | + provideEffects([TestEffects]).ɵproviders, |
| 118 | + // provide feature state after effects |
| 119 | + provideState(featureSliceKey, createReducer('effects')).ɵproviders, |
| 120 | + ], |
| 121 | + }); |
| 122 | + |
| 123 | + const store = TestBed.inject(Store); |
| 124 | + const effects = TestBed.inject(TestEffects); |
| 125 | + |
| 126 | + effects.effectWithFeatureState$.subscribe((action) => { |
| 127 | + expect(action).toEqual( |
| 128 | + effectWithFeatureStateDone({ [featureSliceKey]: 'effects' }) |
| 129 | + ); |
| 130 | + done(); |
| 131 | + }); |
| 132 | + |
| 133 | + store.dispatch(effectWithFeatureStateTest()); |
| 134 | + }); |
| 135 | +}); |
| 136 | + |
| 137 | +const rootSliceKey = 'rootSlice'; |
| 138 | +const featureSliceKey = 'featureSlice'; |
| 139 | +const selectRootSlice = createFeatureSelector<string>(rootSliceKey); |
| 140 | +const selectFeatureSlice = createFeatureSelector<string>(featureSliceKey); |
| 141 | + |
| 142 | +const simpleEffectTest = createAction('simpleEffectTest'); |
| 143 | +const simpleEffectDone = createAction('simpleEffectDone'); |
| 144 | +const effectWithRootStateTest = createAction('effectWithRootStateTest'); |
| 145 | +const effectWithRootStateDone = createAction( |
| 146 | + 'effectWithRootStateDone', |
| 147 | + props<{ [rootSliceKey]: string }>() |
| 148 | +); |
| 149 | +const effectWithFeatureStateTest = createAction('effectWithFeatureStateTest'); |
| 150 | +const effectWithFeatureStateDone = createAction( |
| 151 | + 'effectWithFeatureStateDone', |
| 152 | + props<{ [featureSliceKey]: string }>() |
| 153 | +); |
| 154 | + |
| 155 | +@Injectable() |
| 156 | +class TestEffects { |
| 157 | + constructor( |
| 158 | + private readonly actions$: Actions, |
| 159 | + private readonly store: Store |
| 160 | + ) {} |
| 161 | + |
| 162 | + readonly simpleEffect$ = createEffect(() => { |
| 163 | + return this.actions$.pipe( |
| 164 | + ofType(simpleEffectTest), |
| 165 | + map(() => simpleEffectDone()) |
| 166 | + ); |
| 167 | + }); |
| 168 | + |
| 169 | + readonly effectWithRootState$ = createEffect(() => { |
| 170 | + return this.actions$.pipe( |
| 171 | + ofType(effectWithRootStateTest), |
| 172 | + concatLatestFrom(() => this.store.select(selectRootSlice)), |
| 173 | + map(([, rootSlice]) => effectWithRootStateDone({ rootSlice })) |
| 174 | + ); |
| 175 | + }); |
| 176 | + |
| 177 | + readonly effectWithFeatureState$ = createEffect(() => { |
| 178 | + return this.actions$.pipe( |
| 179 | + ofType(effectWithFeatureStateTest), |
| 180 | + concatLatestFrom(() => this.store.select(selectFeatureSlice)), |
| 181 | + map(([, featureSlice]) => effectWithFeatureStateDone({ featureSlice })) |
| 182 | + ); |
| 183 | + }); |
| 184 | +} |
0 commit comments