1
1
import { ErrorHandler } from '@angular/core' ;
2
2
import { TestBed } from '@angular/core/testing' ;
3
3
import { cold , hot , getTestScheduler } from 'jasmine-marbles' ;
4
- import { concat , NEVER , Observable , of , throwError , timer } from 'rxjs' ;
5
- import { concatMap , map } from 'rxjs/operators' ;
4
+ import {
5
+ concat ,
6
+ NEVER ,
7
+ Observable ,
8
+ of ,
9
+ throwError ,
10
+ timer ,
11
+ Subject ,
12
+ } from 'rxjs' ;
13
+ import { map , mapTo } from 'rxjs/operators' ;
6
14
7
15
import {
8
16
Effect ,
9
17
EffectSources ,
10
18
OnIdentifyEffects ,
11
19
OnInitEffects ,
12
20
createEffect ,
21
+ Actions ,
13
22
} from '../' ;
23
+ import { EffectsRunner } from '../src/effects_runner' ;
14
24
import { Store } from '@ngrx/store' ;
25
+ import { ofType } from '../src' ;
15
26
16
27
describe ( 'EffectSources' , ( ) => {
17
28
let mockErrorReporter : ErrorHandler ;
@@ -21,6 +32,7 @@ describe('EffectSources', () => {
21
32
TestBed . configureTestingModule ( {
22
33
providers : [
23
34
EffectSources ,
35
+ EffectsRunner ,
24
36
{
25
37
provide : Store ,
26
38
useValue : {
@@ -30,6 +42,9 @@ describe('EffectSources', () => {
30
42
] ,
31
43
} ) ;
32
44
45
+ const effectsRunner = TestBed . get ( EffectsRunner ) ;
46
+ effectsRunner . start ( ) ;
47
+
33
48
mockErrorReporter = TestBed . get ( ErrorHandler ) ;
34
49
effectSources = TestBed . get ( EffectSources ) ;
35
50
@@ -51,15 +66,83 @@ describe('EffectSources', () => {
51
66
return { type : '[EffectWithInitAction] Init' } ;
52
67
}
53
68
}
69
+ const store = TestBed . get ( Store ) ;
54
70
55
71
effectSources . addEffects ( new EffectWithInitAction ( ) ) ;
56
72
73
+ expect ( store . dispatch ) . toHaveBeenCalledTimes ( 1 ) ;
74
+ expect ( store . dispatch ) . toHaveBeenCalledWith ( {
75
+ type : '[EffectWithInitAction] Init' ,
76
+ } ) ;
77
+ } ) ;
78
+
79
+ it ( 'should dispatch an action on ngrxOnInitEffects after being registered (class has effects)' , ( ) => {
80
+ class EffectWithInitActionAndEffects implements OnInitEffects {
81
+ effectOne = createEffect ( ( ) => {
82
+ return this . actions$ . pipe (
83
+ ofType ( 'Action 1' ) ,
84
+ mapTo ( { type : 'Action 1 Response' } )
85
+ ) ;
86
+ } ) ;
87
+ effectTwo = createEffect ( ( ) => {
88
+ return this . actions$ . pipe (
89
+ ofType ( 'Action 2' ) ,
90
+ mapTo ( { type : 'Action 2 Response' } )
91
+ ) ;
92
+ } ) ;
93
+
94
+ ngrxOnInitEffects ( ) {
95
+ return { type : '[EffectWithInitAction] Init' } ;
96
+ }
97
+
98
+ constructor ( private actions$ : Actions ) { }
99
+ }
57
100
const store = TestBed . get ( Store ) ;
101
+
102
+ effectSources . addEffects ( new EffectWithInitActionAndEffects ( new Subject ( ) ) ) ;
103
+
104
+ expect ( store . dispatch ) . toHaveBeenCalledTimes ( 1 ) ;
58
105
expect ( store . dispatch ) . toHaveBeenCalledWith ( {
59
106
type : '[EffectWithInitAction] Init' ,
60
107
} ) ;
61
108
} ) ;
62
109
110
+ it ( 'should only dispatch an action on ngrxOnInitEffects once after being registered' , ( ) => {
111
+ class EffectWithInitAction implements OnInitEffects {
112
+ ngrxOnInitEffects ( ) {
113
+ return { type : '[EffectWithInitAction] Init' } ;
114
+ }
115
+ }
116
+ const store = TestBed . get ( Store ) ;
117
+
118
+ effectSources . addEffects ( new EffectWithInitAction ( ) ) ;
119
+ effectSources . addEffects ( new EffectWithInitAction ( ) ) ;
120
+
121
+ expect ( store . dispatch ) . toHaveBeenCalledTimes ( 1 ) ;
122
+ } ) ;
123
+
124
+ it ( 'should dispatch an action on ngrxOnInitEffects multiple times after being registered with different identifiers' , ( ) => {
125
+ let id = 0 ;
126
+ class EffectWithInitAction implements OnInitEffects , OnIdentifyEffects {
127
+ effectId = '' ;
128
+ ngrxOnIdentifyEffects ( ) : string {
129
+ return this . effectId ;
130
+ }
131
+ ngrxOnInitEffects ( ) {
132
+ return { type : '[EffectWithInitAction] Init' } ;
133
+ }
134
+ constructor ( ) {
135
+ this . effectId = ( id ++ ) . toString ( ) ;
136
+ }
137
+ }
138
+ const store = TestBed . get ( Store ) ;
139
+
140
+ effectSources . addEffects ( new EffectWithInitAction ( ) ) ;
141
+ effectSources . addEffects ( new EffectWithInitAction ( ) ) ;
142
+
143
+ expect ( store . dispatch ) . toHaveBeenCalledTimes ( 2 ) ;
144
+ } ) ;
145
+
63
146
describe ( 'toActions() Operator' , ( ) => {
64
147
describe ( 'with @Effect()' , ( ) => {
65
148
const a = { type : 'From Source A' } ;
0 commit comments