diff --git a/modules/effects/testing/spec/mock_actions.spec.ts b/modules/effects/testing/spec/mock_actions.spec.ts new file mode 100644 index 0000000000..46a582fa69 --- /dev/null +++ b/modules/effects/testing/spec/mock_actions.spec.ts @@ -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(); + }); + }); + }); +}); diff --git a/modules/effects/testing/src/testing.ts b/modules/effects/testing/src/testing.ts index eb65f8bee4..b1faf8019f 100644 --- a/modules/effects/testing/src/testing.ts +++ b/modules/effects/testing/src/testing.ts @@ -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): Provider; -export function provideMockActions(factory: () => Observable): Provider; +/** + * @description + * Creates mock actions provider. + * + * @param source Actions' source + */ +export function provideMockActions(source: Observable): FactoryProvider; +/** + * @description + * Creates mock actions provider. + * + * @param factory Actions' source creation function + * + * @usageNotes + * + * **With `TestBed.configureTestingModule`** + * + * ```ts + * describe('Books Effects', () => { + * let actions$: Observable; + * 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; + * let effects: CounterEffects; + * + * beforeEach(() => { + * injector = Injector.create({ + * providers: [ + * provideMockActions(() => actions$), + * CounterEffects, + * ], + * }); + * + * actions$ = injector.get(Actions); + * effects = injector.get(CounterEffects); + * }); + * }); + * ``` + */ +export function provideMockActions( + factory: () => Observable +): FactoryProvider; export function provideMockActions( factoryOrSource: (() => Observable) | Observable -): Provider { +): FactoryProvider { return { provide: Actions, useFactory: (): Observable => { @@ -16,5 +75,6 @@ export function provideMockActions( return new Actions(factoryOrSource); }, + deps: [], }; }