Skip to content

Commit

Permalink
feat(Effects): Add root effects init action (#473)
Browse files Browse the repository at this point in the history
Closes #246
  • Loading branch information
timdeschryver authored and MikeRyanDev committed Oct 13, 2017
1 parent d5640ec commit 838ba17
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
55 changes: 55 additions & 0 deletions modules/effects/spec/effects_root_module.spec.ts
Original file line number Diff line number Diff line change
@@ -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,
});
});
});
7 changes: 6 additions & 1 deletion modules/effects/src/effects_root_module.ts
Original file line number Diff line number Diff line change
@@ -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<any>,
@Inject(ROOT_EFFECTS) rootEffects: any[],
@Optional() storeModule: StoreModule
) {
Expand All @@ -17,6 +20,8 @@ export class EffectsRootModule {
rootEffects.forEach(effectSourceInstance =>
sources.addEffects(effectSourceInstance)
);

store.dispatch({ type: ROOT_EFFECTS_INIT });
}

addEffects(effectSourceInstance: any) {
Expand Down
1 change: 1 addition & 0 deletions modules/effects/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

0 comments on commit 838ba17

Please sign in to comment.