Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(effects): use provideMockActions outside of the TestBed #2762

Merged
merged 1 commit into from
Nov 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions modules/effects/testing/spec/mock_actions.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { of } from 'rxjs';
import { TestBed } from '@angular/core/testing';
import { provideMockActions } from '@ngrx/effects/testing';
import { Actions } from '@ngrx/effects';
import { Injector } from '@angular/core';

describe('Mock Actions', () => {
describe('with TestBed', () => {
it('should provide Actions from source', (done) => {
TestBed.configureTestingModule({
providers: [provideMockActions(of({ type: 'foo' }))],
});

const actions$ = TestBed.inject(Actions);
actions$.subscribe((action) => {
expect(action.type).toBe('foo');
done();
});
});

it('should provide Actions from factory', (done) => {
TestBed.configureTestingModule({
providers: [provideMockActions(() => of({ type: 'bar' }))],
});

const actions$ = TestBed.inject(Actions);
actions$.subscribe((action) => {
expect(action.type).toBe('bar');
done();
});
});
});

describe('with Injector', () => {
it('should provide Actions from source', (done) => {
const injector = Injector.create({
providers: [provideMockActions(of({ type: 'foo' }))],
});

const actions$ = injector.get(Actions);
actions$.subscribe((action) => {
expect(action.type).toBe('foo');
done();
});
});

it('should provide Actions from factory', (done) => {
const injector = Injector.create({
providers: [provideMockActions(() => of({ type: 'bar' }))],
});

const actions$ = injector.get(Actions);
actions$.subscribe((action) => {
expect(action.type).toBe('bar');
done();
});
});
});
});
68 changes: 64 additions & 4 deletions modules/effects/testing/src/testing.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,71 @@
import { Provider } from '@angular/core';
import { FactoryProvider } from '@angular/core';
import { Actions } from '@ngrx/effects';
import { defer, Observable } from 'rxjs';

export function provideMockActions(source: Observable<any>): Provider;
export function provideMockActions(factory: () => Observable<any>): Provider;
/**
* @description
* Creates mock actions provider.
*
* @param source Actions' source
*/
export function provideMockActions(source: Observable<any>): FactoryProvider;
/**
* @description
* Creates mock actions provider.
*
* @param factory Actions' source creation function
*
* @usageNotes
*
* **With `TestBed.configureTestingModule`**
*
* ```ts
* describe('Books Effects', () => {
* let actions$: Observable<any>;
* let effects: BooksEffects;
*
* beforeEach(() => {
* TestBed.configureTestingModule({
* providers: [
* provideMockActions(() => actions$),
* BooksEffects,
* ],
* });
*
* actions$ = TestBed.inject(Actions);
* effects = TestBed.inject(BooksEffects);
* });
* });
* ```
*
* **With `Injector.create`**
*
* ```ts
* describe('Counter Effects', () => {
* let injector: Injector;
* let actions$: Observable<any>;
* let effects: CounterEffects;
*
* beforeEach(() => {
* injector = Injector.create({
* providers: [
* provideMockActions(() => actions$),
* CounterEffects,
* ],
* });
*
* actions$ = injector.get(Actions);
* effects = injector.get(CounterEffects);
* });
* });
* ```
*/
export function provideMockActions(
factory: () => Observable<any>
): FactoryProvider;
export function provideMockActions(
factoryOrSource: (() => Observable<any>) | Observable<any>
): Provider {
): FactoryProvider {
return {
provide: Actions,
useFactory: (): Observable<any> => {
Expand All @@ -16,5 +75,6 @@ export function provideMockActions(

return new Actions(factoryOrSource);
},
deps: [],
};
}