Skip to content

Commit 838ba17

Browse files
timdeschryverMikeRyanDev
authored andcommitted
feat(Effects): Add root effects init action (#473)
Closes #246
1 parent d5640ec commit 838ba17

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { TestBed } from '@angular/core/testing';
2+
import {
3+
Store,
4+
StoreModule,
5+
ActionReducer,
6+
MetaReducer,
7+
Action,
8+
INIT,
9+
} from '@ngrx/store';
10+
import { ROOT_EFFECTS_INIT } from '../src/effects_root_module';
11+
import { EffectsModule } from '../src/effects_module';
12+
13+
describe('Effects Root Module', () => {
14+
const foo = 'foo';
15+
const reducer = jasmine.createSpy('reducer').and.returnValue(foo);
16+
17+
beforeEach(() => {
18+
reducer.calls.reset();
19+
});
20+
21+
it('dispatches the root effects init action', () => {
22+
TestBed.configureTestingModule({
23+
imports: [
24+
StoreModule.forRoot({ reducer }, { initialState: { reducer: foo } }),
25+
EffectsModule.forRoot([]),
26+
],
27+
});
28+
29+
const store = TestBed.get(Store);
30+
31+
expect(reducer).toHaveBeenCalledWith(foo, {
32+
type: INIT,
33+
});
34+
expect(reducer).toHaveBeenCalledWith(foo, {
35+
type: ROOT_EFFECTS_INIT,
36+
});
37+
});
38+
39+
it("doesn't dispatch the root effects init action when EffectsModule isn't used", () => {
40+
TestBed.configureTestingModule({
41+
imports: [
42+
StoreModule.forRoot({ reducer }, { initialState: { reducer: foo } }),
43+
],
44+
});
45+
46+
const store = TestBed.get(Store);
47+
48+
expect(reducer).toHaveBeenCalledWith(foo, {
49+
type: INIT,
50+
});
51+
expect(reducer).not.toHaveBeenCalledWith(foo, {
52+
type: ROOT_EFFECTS_INIT,
53+
});
54+
});
55+
});

modules/effects/src/effects_root_module.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import { NgModule, Inject, Optional } from '@angular/core';
2-
import { StoreModule } from '@ngrx/store';
2+
import { StoreModule, Store } from '@ngrx/store';
33
import { EffectsRunner } from './effects_runner';
44
import { EffectSources } from './effect_sources';
55
import { ROOT_EFFECTS } from './tokens';
66

7+
export const ROOT_EFFECTS_INIT = '@ngrx/effects/init';
8+
79
@NgModule({})
810
export class EffectsRootModule {
911
constructor(
1012
private sources: EffectSources,
1113
runner: EffectsRunner,
14+
store: Store<any>,
1215
@Inject(ROOT_EFFECTS) rootEffects: any[],
1316
@Optional() storeModule: StoreModule
1417
) {
@@ -17,6 +20,8 @@ export class EffectsRootModule {
1720
rootEffects.forEach(effectSourceInstance =>
1821
sources.addEffects(effectSourceInstance)
1922
);
23+
24+
store.dispatch({ type: ROOT_EFFECTS_INIT });
2025
}
2126

2227
addEffects(effectSourceInstance: any) {

modules/effects/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ export { EffectSources } from './effect_sources';
66
export { OnRunEffects } from './on_run_effects';
77
export { toPayload } from './util';
88
export { EffectNotification } from './effect_notification';
9+
export { ROOT_EFFECTS_INIT } from './effects_root_module';

0 commit comments

Comments
 (0)