diff --git a/modules/effects/spec/effects_root_module.spec.ts b/modules/effects/spec/effects_root_module.spec.ts new file mode 100644 index 0000000000..b5b3d84587 --- /dev/null +++ b/modules/effects/spec/effects_root_module.spec.ts @@ -0,0 +1,55 @@ +import { TestBed } from '@angular/core/testing'; +import { + Store, + StoreModule, + ActionReducer, + MetaReducer, + Action, + INIT, +} from '@ngrx/store'; +import { ROOT_EFFECTS_INIT } from '../src/effects_root_module'; +import { EffectsModule } from '../src/effects_module'; + +describe('Effects Root Module', () => { + const foo = 'foo'; + const reducer = jasmine.createSpy('reducer').and.returnValue(foo); + + beforeEach(() => { + reducer.calls.reset(); + }); + + it('dispatches the root effects init action', () => { + TestBed.configureTestingModule({ + imports: [ + StoreModule.forRoot({ reducer }, { initialState: { reducer: foo } }), + EffectsModule.forRoot([]), + ], + }); + + const store = TestBed.get(Store); + + expect(reducer).toHaveBeenCalledWith(foo, { + type: INIT, + }); + expect(reducer).toHaveBeenCalledWith(foo, { + type: ROOT_EFFECTS_INIT, + }); + }); + + it("doesn't dispatch the root effects init action when EffectsModule isn't used", () => { + TestBed.configureTestingModule({ + imports: [ + StoreModule.forRoot({ reducer }, { initialState: { reducer: foo } }), + ], + }); + + const store = TestBed.get(Store); + + expect(reducer).toHaveBeenCalledWith(foo, { + type: INIT, + }); + expect(reducer).not.toHaveBeenCalledWith(foo, { + type: ROOT_EFFECTS_INIT, + }); + }); +}); diff --git a/modules/effects/src/effects_root_module.ts b/modules/effects/src/effects_root_module.ts index 279d985b70..009a194d42 100644 --- a/modules/effects/src/effects_root_module.ts +++ b/modules/effects/src/effects_root_module.ts @@ -1,14 +1,17 @@ import { NgModule, Inject, Optional } from '@angular/core'; -import { StoreModule } from '@ngrx/store'; +import { StoreModule, Store } from '@ngrx/store'; import { EffectsRunner } from './effects_runner'; import { EffectSources } from './effect_sources'; import { ROOT_EFFECTS } from './tokens'; +export const ROOT_EFFECTS_INIT = '@ngrx/effects/init'; + @NgModule({}) export class EffectsRootModule { constructor( private sources: EffectSources, runner: EffectsRunner, + store: Store, @Inject(ROOT_EFFECTS) rootEffects: any[], @Optional() storeModule: StoreModule ) { @@ -17,6 +20,8 @@ export class EffectsRootModule { rootEffects.forEach(effectSourceInstance => sources.addEffects(effectSourceInstance) ); + + store.dispatch({ type: ROOT_EFFECTS_INIT }); } addEffects(effectSourceInstance: any) { diff --git a/modules/effects/src/index.ts b/modules/effects/src/index.ts index cd112564fb..691f22608a 100644 --- a/modules/effects/src/index.ts +++ b/modules/effects/src/index.ts @@ -6,3 +6,4 @@ export { EffectSources } from './effect_sources'; export { OnRunEffects } from './on_run_effects'; export { toPayload } from './util'; export { EffectNotification } from './effect_notification'; +export { ROOT_EFFECTS_INIT } from './effects_root_module';