Skip to content
This repository was archived by the owner on Dec 26, 2017. It is now read-only.

Commit 4c912d2

Browse files
committed
feat(testing): Add testing module for easily mocking out actions
1 parent 70b2395 commit 4c912d2

4 files changed

Lines changed: 71 additions & 1 deletion

File tree

README.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,42 @@ To help you compose new action sources, @ngrx/effects exports an `Actions` obser
6262

6363

6464
### Testing Effects
65-
WIP
65+
66+
1. Declare the `EffectsTestingModule` in your testing module:
67+
```ts
68+
import { EffectsTestingModule, EffectsTestRunner } from '@ngrx/effects';
69+
70+
describe('My Effect', () => {
71+
beforeEach(() => TestBed.configureTestingModule({
72+
imports: [
73+
EffectsTestingModule
74+
],
75+
declarations: [
76+
AuthEffects
77+
]
78+
}));
79+
});
80+
```
81+
82+
2. Inject the `EffectsTestRunner` and use it queue actions:
83+
```ts
84+
let runner: EffectsTestRunner;
85+
86+
beforeEach(inject([
87+
EffectsTestRunner,
88+
(_runner) => {
89+
runner = _runner;
90+
}
91+
]));
92+
```
93+
94+
3. Queue up actions then subscribe to the effect you want to test, asserting on the result:
95+
```ts
96+
it('should return a LOGIN_SUCCESS action after logging in', () => {
97+
runner.queue({ type: 'LOGIN' });
98+
99+
authEffects.login$.subscribe(result => {
100+
expect(result).toEqual({ type: 'LOGIN_SUCCESS' });
101+
});
102+
});
103+
```

testing/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './runner';
2+
export * from './testing.module';

testing/runner.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Injectable } from '@angular/core';
2+
import { ReplaySubject } from 'rxjs/ReplaySubject';
3+
4+
5+
@Injectable()
6+
export class EffectsTestRunner extends ReplaySubject<any> {
7+
constructor() {
8+
super();
9+
}
10+
11+
queue(action: any) {
12+
this.next(action);
13+
}
14+
}

testing/testing.module.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { NgModule } from '@angular/core';
2+
import { Actions } from '../src/actions';
3+
import { EffectsTestRunner } from './runner';
4+
5+
6+
export function _createActions(runner: EffectsTestRunner): Actions {
7+
return new Actions(runner);
8+
}
9+
10+
@NgModule({
11+
providers: [
12+
EffectsTestRunner,
13+
{ provide: Actions, deps: [ EffectsTestRunner ], useFactory: _createActions }
14+
]
15+
})
16+
export class EffectsTestingModule { }

0 commit comments

Comments
 (0)