@@ -3,9 +3,10 @@ import {
3
3
ErrorHandler ,
4
4
Input ,
5
5
Output ,
6
+ Type ,
6
7
ViewChild ,
7
8
} from '@angular/core' ;
8
- import { ComponentFixture , TestBed } from '@angular/core/testing' ;
9
+ import { TestBed } from '@angular/core/testing' ;
9
10
import { RxState } from '@rx-angular/state' ;
10
11
import { select } from '@rx-angular/state/selections' ;
11
12
import { PrimitiveState } from '@test-helpers' ;
@@ -105,21 +106,17 @@ export class RxStateGlueContainerComponent extends RxState<PrimitiveState> {
105
106
106
107
describe ( 'LocalProviderTestComponent' , ( ) => {
107
108
let component : RxStateInjectionComponent ;
108
- let fixture : ComponentFixture < RxStateInjectionComponent > ;
109
- let errorHandlerSpy : any ;
109
+ let errorHandlerSpy : jest . Mock ;
110
110
111
111
beforeEach ( ( ) => {
112
- const testBed = TestBed . configureTestingModule ( {
113
- declarations : [ RxStateInjectionComponent ] ,
112
+ const { component : c , errorHandler : e } = setupTestComponent ( {
113
+ testComponent : RxStateInjectionComponent ,
114
114
providers : [
115
115
{ provide : ErrorHandler , useValue : { handleError : jest . fn ( ) } } ,
116
116
] ,
117
- teardown : { destroyAfterEach : true } ,
118
117
} ) ;
119
- fixture = TestBed . createComponent ( RxStateInjectionComponent ) ;
120
- component = fixture . componentInstance ;
121
- errorHandlerSpy = testBed . inject ( ErrorHandler ) . handleError ;
122
- fixture . detectChanges ( ) ;
118
+ component = c ;
119
+ errorHandlerSpy = e . handleError as jest . Mock ;
123
120
} ) ;
124
121
125
122
it ( 'should create' , ( ) => {
@@ -137,21 +134,17 @@ describe('LocalProviderTestComponent', () => {
137
134
138
135
describe ( 'InheritanceTestComponent' , ( ) => {
139
136
let component : RxStateInheritanceComponent ;
140
- let fixture : ComponentFixture < RxStateInheritanceComponent > ;
141
- let errorHandlerSpy : any ;
137
+ let errorHandlerSpy : jest . Mock ;
142
138
143
139
beforeEach ( ( ) => {
144
- const testBed = TestBed . configureTestingModule ( {
145
- declarations : [ RxStateInheritanceComponent ] ,
146
- teardown : { destroyAfterEach : true } ,
140
+ const { component : c , errorHandler : e } = setupTestComponent ( {
141
+ testComponent : RxStateInheritanceComponent ,
147
142
providers : [
148
143
{ provide : ErrorHandler , useValue : { handleError : jest . fn ( ) } } ,
149
144
] ,
150
145
} ) ;
151
- fixture = TestBed . createComponent ( RxStateInheritanceComponent ) ;
152
- component = fixture . componentInstance ;
153
- errorHandlerSpy = testBed . inject ( ErrorHandler ) . handleError ;
154
- fixture . detectChanges ( ) ;
146
+ component = c ;
147
+ errorHandlerSpy = e . handleError as jest . Mock ;
155
148
} ) ;
156
149
157
150
it ( 'should create' , ( ) => {
@@ -168,3 +161,59 @@ describe('InheritanceTestComponent', () => {
168
161
} ) ;
169
162
} ) ;
170
163
} ) ;
164
+
165
+ describe ( 'CustomErrorHandler' , ( ) => {
166
+ let component : RxStateInheritanceComponent ;
167
+ let errorHandlerSpy : jest . SpyInstance ;
168
+
169
+ class CustomErrorHandler implements ErrorHandler {
170
+ private prod = true ;
171
+ handleError ( ) {
172
+ if ( this . prod ) {
173
+ throw new Error ( 'Prod error' ) ;
174
+ }
175
+ throw new Error ( 'Dev error' ) ;
176
+ }
177
+ }
178
+
179
+ beforeEach ( ( ) => {
180
+ const { component : c , errorHandler : e } = setupTestComponent ( {
181
+ testComponent : RxStateInheritanceComponent ,
182
+ providers : [
183
+ {
184
+ provide : ErrorHandler ,
185
+ useClass : CustomErrorHandler ,
186
+ } ,
187
+ ] ,
188
+ } ) ;
189
+ component = c ;
190
+ errorHandlerSpy = jest . spyOn ( e , 'handleError' ) ;
191
+ } ) ;
192
+
193
+ describe ( 'state.connect' , ( ) => {
194
+ it ( 'should handle error through CustomErrorHandler' , ( ) => {
195
+ const error$ = throwError ( ( ) => new Error ( 'whoops' ) ) ;
196
+ component . connect ( 'str' , error$ ) ;
197
+ expect ( errorHandlerSpy ) . toThrow ( new Error ( 'Prod error' ) ) ;
198
+ } ) ;
199
+ } ) ;
200
+ } ) ;
201
+
202
+ function setupTestComponent ( {
203
+ testComponent,
204
+ providers,
205
+ } : {
206
+ testComponent : Type < any > ;
207
+ providers : any [ ] ;
208
+ } ) {
209
+ const testBed = TestBed . configureTestingModule ( {
210
+ declarations : [ testComponent ] ,
211
+ providers : [ ...providers ] ,
212
+ teardown : { destroyAfterEach : true } ,
213
+ } ) ;
214
+ const fixture = TestBed . createComponent ( testComponent ) ;
215
+ const component = fixture . componentInstance ;
216
+ const errorHandler = testBed . inject ( ErrorHandler ) ;
217
+
218
+ return { fixture, component, errorHandler } ;
219
+ }
0 commit comments