Skip to content

Commit c47114c

Browse files
feat(effects): add support for provideMockActions outside of the TestBed (#2762)
1 parent 8906423 commit c47114c

File tree

2 files changed

+123
-4
lines changed

2 files changed

+123
-4
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { of } from 'rxjs';
2+
import { TestBed } from '@angular/core/testing';
3+
import { provideMockActions } from '@ngrx/effects/testing';
4+
import { Actions } from '@ngrx/effects';
5+
import { Injector } from '@angular/core';
6+
7+
describe('Mock Actions', () => {
8+
describe('with TestBed', () => {
9+
it('should provide Actions from source', (done) => {
10+
TestBed.configureTestingModule({
11+
providers: [provideMockActions(of({ type: 'foo' }))],
12+
});
13+
14+
const actions$ = TestBed.inject(Actions);
15+
actions$.subscribe((action) => {
16+
expect(action.type).toBe('foo');
17+
done();
18+
});
19+
});
20+
21+
it('should provide Actions from factory', (done) => {
22+
TestBed.configureTestingModule({
23+
providers: [provideMockActions(() => of({ type: 'bar' }))],
24+
});
25+
26+
const actions$ = TestBed.inject(Actions);
27+
actions$.subscribe((action) => {
28+
expect(action.type).toBe('bar');
29+
done();
30+
});
31+
});
32+
});
33+
34+
describe('with Injector', () => {
35+
it('should provide Actions from source', (done) => {
36+
const injector = Injector.create({
37+
providers: [provideMockActions(of({ type: 'foo' }))],
38+
});
39+
40+
const actions$ = injector.get(Actions);
41+
actions$.subscribe((action) => {
42+
expect(action.type).toBe('foo');
43+
done();
44+
});
45+
});
46+
47+
it('should provide Actions from factory', (done) => {
48+
const injector = Injector.create({
49+
providers: [provideMockActions(() => of({ type: 'bar' }))],
50+
});
51+
52+
const actions$ = injector.get(Actions);
53+
actions$.subscribe((action) => {
54+
expect(action.type).toBe('bar');
55+
done();
56+
});
57+
});
58+
});
59+
});
Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,71 @@
1-
import { Provider } from '@angular/core';
1+
import { FactoryProvider } from '@angular/core';
22
import { Actions } from '@ngrx/effects';
33
import { defer, Observable } from 'rxjs';
44

5-
export function provideMockActions(source: Observable<any>): Provider;
6-
export function provideMockActions(factory: () => Observable<any>): Provider;
5+
/**
6+
* @description
7+
* Creates mock actions provider.
8+
*
9+
* @param source Actions' source
10+
*/
11+
export function provideMockActions(source: Observable<any>): FactoryProvider;
12+
/**
13+
* @description
14+
* Creates mock actions provider.
15+
*
16+
* @param factory Actions' source creation function
17+
*
18+
* @usageNotes
19+
*
20+
* **With `TestBed.configureTestingModule`**
21+
*
22+
* ```ts
23+
* describe('Books Effects', () => {
24+
* let actions$: Observable<any>;
25+
* let effects: BooksEffects;
26+
*
27+
* beforeEach(() => {
28+
* TestBed.configureTestingModule({
29+
* providers: [
30+
* provideMockActions(() => actions$),
31+
* BooksEffects,
32+
* ],
33+
* });
34+
*
35+
* actions$ = TestBed.inject(Actions);
36+
* effects = TestBed.inject(BooksEffects);
37+
* });
38+
* });
39+
* ```
40+
*
41+
* **With `Injector.create`**
42+
*
43+
* ```ts
44+
* describe('Counter Effects', () => {
45+
* let injector: Injector;
46+
* let actions$: Observable<any>;
47+
* let effects: CounterEffects;
48+
*
49+
* beforeEach(() => {
50+
* injector = Injector.create({
51+
* providers: [
52+
* provideMockActions(() => actions$),
53+
* CounterEffects,
54+
* ],
55+
* });
56+
*
57+
* actions$ = injector.get(Actions);
58+
* effects = injector.get(CounterEffects);
59+
* });
60+
* });
61+
* ```
62+
*/
63+
export function provideMockActions(
64+
factory: () => Observable<any>
65+
): FactoryProvider;
766
export function provideMockActions(
867
factoryOrSource: (() => Observable<any>) | Observable<any>
9-
): Provider {
68+
): FactoryProvider {
1069
return {
1170
provide: Actions,
1271
useFactory: (): Observable<any> => {
@@ -16,5 +75,6 @@ export function provideMockActions(
1675

1776
return new Actions(factoryOrSource);
1877
},
78+
deps: [],
1979
};
2080
}

0 commit comments

Comments
 (0)