forked from ngrx/platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting.ts
More file actions
80 lines (78 loc) · 1.8 KB
/
testing.ts
File metadata and controls
80 lines (78 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { FactoryProvider } from '@angular/core';
import { Actions } from '@ngrx/effects';
import { defer, Observable } from 'rxjs';
/**
* @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>
): FactoryProvider {
return {
provide: Actions,
useFactory: (): Observable<any> => {
if (typeof factoryOrSource === 'function') {
return new Actions(defer(factoryOrSource));
}
return new Actions(factoryOrSource);
},
deps: [],
};
}