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

Commit

Permalink
feat(testing): Add testing module for easily mocking out actions
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeRyanDev committed Sep 27, 2016
1 parent 70b2395 commit 4c912d2
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
40 changes: 39 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,42 @@ To help you compose new action sources, @ngrx/effects exports an `Actions` obser


### Testing Effects
WIP

1. Declare the `EffectsTestingModule` in your testing module:
```ts
import { EffectsTestingModule, EffectsTestRunner } from '@ngrx/effects';

describe('My Effect', () => {
beforeEach(() => TestBed.configureTestingModule({
imports: [
EffectsTestingModule
],
declarations: [
AuthEffects
]
}));
});
```

2. Inject the `EffectsTestRunner` and use it queue actions:
```ts
let runner: EffectsTestRunner;

beforeEach(inject([
EffectsTestRunner,
(_runner) => {
runner = _runner;
}
]));
```

3. Queue up actions then subscribe to the effect you want to test, asserting on the result:
```ts
it('should return a LOGIN_SUCCESS action after logging in', () => {
runner.queue({ type: 'LOGIN' });

authEffects.login$.subscribe(result => {
expect(result).toEqual({ type: 'LOGIN_SUCCESS' });
});
});
```
2 changes: 2 additions & 0 deletions testing/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './runner';
export * from './testing.module';
14 changes: 14 additions & 0 deletions testing/runner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Injectable } from '@angular/core';
import { ReplaySubject } from 'rxjs/ReplaySubject';


@Injectable()
export class EffectsTestRunner extends ReplaySubject<any> {
constructor() {
super();
}

queue(action: any) {
this.next(action);
}
}
16 changes: 16 additions & 0 deletions testing/testing.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { NgModule } from '@angular/core';
import { Actions } from '../src/actions';
import { EffectsTestRunner } from './runner';


export function _createActions(runner: EffectsTestRunner): Actions {
return new Actions(runner);
}

@NgModule({
providers: [
EffectsTestRunner,
{ provide: Actions, deps: [ EffectsTestRunner ], useFactory: _createActions }
]
})
export class EffectsTestingModule { }

0 comments on commit 4c912d2

Please sign in to comment.