|
| 1 | +import { TestBed } from '@angular/core/testing'; |
| 2 | +import { Store, Action } from '@ngrx/store'; |
| 3 | +import { |
| 4 | + EffectsModule, |
| 5 | + OnInitEffects, |
| 6 | + ROOT_EFFECTS_INIT, |
| 7 | + OnIdentifyEffects, |
| 8 | + EffectSources, |
| 9 | +} from '..'; |
| 10 | + |
| 11 | +describe('NgRx Effects Integration spec', () => { |
| 12 | + let dispatch: jasmine.Spy; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + TestBed.configureTestingModule({ |
| 16 | + imports: [ |
| 17 | + EffectsModule.forRoot([ |
| 18 | + RootEffectWithInitAction, |
| 19 | + RootEffectWithoutLifecycle, |
| 20 | + RootEffectWithInitActionWithPayload, |
| 21 | + ]), |
| 22 | + EffectsModule.forFeature([FeatEffectWithInitAction]), |
| 23 | + ], |
| 24 | + providers: [ |
| 25 | + { |
| 26 | + provide: Store, |
| 27 | + useValue: { |
| 28 | + dispatch: jasmine.createSpy('dispatch'), |
| 29 | + }, |
| 30 | + }, |
| 31 | + ], |
| 32 | + }); |
| 33 | + |
| 34 | + const store = TestBed.get(Store) as Store<any>; |
| 35 | + |
| 36 | + const effectSources = TestBed.get(EffectSources) as EffectSources; |
| 37 | + effectSources.addEffects(new FeatEffectWithIdentifierAndInitAction('one')); |
| 38 | + effectSources.addEffects(new FeatEffectWithIdentifierAndInitAction('two')); |
| 39 | + effectSources.addEffects(new FeatEffectWithIdentifierAndInitAction('one')); |
| 40 | + |
| 41 | + dispatch = store.dispatch as jasmine.Spy; |
| 42 | + }); |
| 43 | + |
| 44 | + it('should dispatch init actions in the correct order', () => { |
| 45 | + expect(dispatch.calls.count()).toBe(7); |
| 46 | + |
| 47 | + // All of the root effects init actions are dispatched first |
| 48 | + expect(dispatch.calls.argsFor(0)).toEqual([ |
| 49 | + { type: '[RootEffectWithInitAction]: INIT' }, |
| 50 | + ]); |
| 51 | + |
| 52 | + expect(dispatch.calls.argsFor(1)).toEqual([new ActionWithPayload()]); |
| 53 | + |
| 54 | + // After all of the root effects are registered, the ROOT_EFFECTS_INIT action is dispatched |
| 55 | + expect(dispatch.calls.argsFor(2)).toEqual([{ type: ROOT_EFFECTS_INIT }]); |
| 56 | + |
| 57 | + // After the root effects init, the feature effects are dispatched |
| 58 | + expect(dispatch.calls.argsFor(3)).toEqual([ |
| 59 | + { type: '[FeatEffectWithInitAction]: INIT' }, |
| 60 | + ]); |
| 61 | + |
| 62 | + expect(dispatch.calls.argsFor(4)).toEqual([ |
| 63 | + { type: '[FeatEffectWithIdentifierAndInitAction]: INIT' }, |
| 64 | + ]); |
| 65 | + |
| 66 | + expect(dispatch.calls.argsFor(5)).toEqual([ |
| 67 | + { type: '[FeatEffectWithIdentifierAndInitAction]: INIT' }, |
| 68 | + ]); |
| 69 | + |
| 70 | + // While the effect has the same identifier the init effect action is still being dispatched |
| 71 | + expect(dispatch.calls.argsFor(6)).toEqual([ |
| 72 | + { type: '[FeatEffectWithIdentifierAndInitAction]: INIT' }, |
| 73 | + ]); |
| 74 | + }); |
| 75 | + |
| 76 | + class RootEffectWithInitAction implements OnInitEffects { |
| 77 | + ngrxOnInitEffects(): Action { |
| 78 | + return { type: '[RootEffectWithInitAction]: INIT' }; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + class ActionWithPayload implements Action { |
| 83 | + readonly type = '[RootEffectWithInitActionWithPayload]: INIT'; |
| 84 | + readonly payload = 47; |
| 85 | + } |
| 86 | + |
| 87 | + class RootEffectWithInitActionWithPayload implements OnInitEffects { |
| 88 | + ngrxOnInitEffects(): Action { |
| 89 | + return new ActionWithPayload(); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + class RootEffectWithoutLifecycle {} |
| 94 | + |
| 95 | + class FeatEffectWithInitAction implements OnInitEffects { |
| 96 | + ngrxOnInitEffects(): Action { |
| 97 | + return { type: '[FeatEffectWithInitAction]: INIT' }; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + class FeatEffectWithIdentifierAndInitAction |
| 102 | + implements OnInitEffects, OnIdentifyEffects { |
| 103 | + ngrxOnIdentifyEffects(): string { |
| 104 | + return this.effectIdentifier; |
| 105 | + } |
| 106 | + |
| 107 | + ngrxOnInitEffects(): Action { |
| 108 | + return { type: '[FeatEffectWithIdentifierAndInitAction]: INIT' }; |
| 109 | + } |
| 110 | + |
| 111 | + constructor(private effectIdentifier: string) {} |
| 112 | + } |
| 113 | +}); |
0 commit comments