1
- import { ErrorHandler , Provider } from '@angular/core' ;
1
+ import { ErrorHandler , Provider , Type } from '@angular/core' ;
2
2
import { TestBed } from '@angular/core/testing' ;
3
3
import { Action , Store } from '@ngrx/store' ;
4
- import { Observable , of } from 'rxjs' ;
4
+ import { Observable , of , throwError } from 'rxjs' ;
5
5
import { catchError } from 'rxjs/operators' ;
6
6
import { createEffect , EFFECTS_ERROR_HANDLER , EffectsModule } from '..' ;
7
+ import * as effectsSrc from '../src/effects_error_handler' ;
7
8
8
9
describe ( 'Effects Error Handler' , ( ) => {
9
10
let subscriptionCount : number ;
10
11
let globalErrorHandler : jasmine . Spy ;
11
12
let storeNext : jasmine . Spy ;
12
13
13
- function makeEffectTestBed ( ...providers : Provider [ ] ) {
14
+ function makeEffectTestBed ( effect : Type < any > , ...providers : Provider [ ] ) {
14
15
subscriptionCount = 0 ;
15
16
16
17
TestBed . configureTestingModule ( {
17
- imports : [ EffectsModule . forRoot ( [ ErrorEffect ] ) ] ,
18
+ imports : [ EffectsModule . forRoot ( [ effect ] ) ] ,
18
19
providers : [
19
20
{
20
21
provide : Store ,
@@ -38,8 +39,14 @@ describe('Effects Error Handler', () => {
38
39
storeNext = store . next ;
39
40
}
40
41
42
+ it ( 'should retry on infinite error up to 10 times' , ( ) => {
43
+ makeEffectTestBed ( AlwaysErrorEffect ) ;
44
+
45
+ expect ( globalErrorHandler . calls . count ( ) ) . toBe ( 10 ) ;
46
+ } ) ;
47
+
41
48
it ( 'should retry and notify error handler when effect error handler is not provided' , ( ) => {
42
- makeEffectTestBed ( ) ;
49
+ makeEffectTestBed ( ErrorEffect ) ;
43
50
44
51
// two subscriptions expected:
45
52
// 1. Initial subscription to the effect (this will error)
@@ -62,7 +69,7 @@ describe('Effects Error Handler', () => {
62
69
) ;
63
70
} ) ;
64
71
65
- makeEffectTestBed ( {
72
+ makeEffectTestBed ( ErrorEffect , {
66
73
provide : EFFECTS_ERROR_HANDLER ,
67
74
useValue : effectsErrorHandlerSpy ,
68
75
} ) ;
@@ -84,6 +91,10 @@ describe('Effects Error Handler', () => {
84
91
} ) ;
85
92
}
86
93
94
+ class AlwaysErrorEffect {
95
+ effect$ = createEffect ( ( ) => throwError ( 'always an error' ) ) ;
96
+ }
97
+
87
98
/**
88
99
* This observable factory returns an observable that will never emit, but the first subscriber will get an immediate
89
100
* error. All subsequent subscribers will just get an observable that does not emit.
0 commit comments