forked from ngrx/platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(effects): make resubscription handler customizable
BREAKING CHANGE: `resubscribeOnError` renamed to `useEffectsErrorHandler` in `createEffect` metadata Closes ngrx#2294
- Loading branch information
1 parent
f17589f
commit 1970330
Showing
18 changed files
with
297 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { ErrorHandler, Type } from '@angular/core'; | ||
import { TestBed } from '@angular/core/testing'; | ||
import { Action, Store } from '@ngrx/store'; | ||
import { Observable, of } from 'rxjs'; | ||
import { | ||
createEffect, | ||
EFFECTS_ERROR_HANDLER, | ||
EffectsModule, | ||
EffectSources, | ||
} from '..'; | ||
|
||
describe('NgRx Effects Error Handler spec', () => { | ||
let subscriptionCount: number; | ||
let globalErrorHandler: jasmine.Spy; | ||
let dispatch: jasmine.Spy; | ||
let storeNext: jasmine.Spy; | ||
|
||
function makeEffectTestBed(...providers: any[]) { | ||
subscriptionCount = 0; | ||
|
||
TestBed.configureTestingModule({ | ||
imports: [EffectsModule.forRoot([ErrorEffect])], | ||
providers: [ | ||
{ | ||
provide: Store, | ||
useValue: { | ||
dispatch: jasmine.createSpy('dispatch'), | ||
next: jasmine.createSpy('storeNext'), | ||
}, | ||
}, | ||
{ | ||
provide: ErrorHandler, | ||
useValue: { | ||
handleError: jasmine.createSpy('globalErrorHandler'), | ||
}, | ||
}, | ||
...providers, | ||
], | ||
}); | ||
|
||
TestBed.get(EffectSources); | ||
globalErrorHandler = TestBed.get(ErrorHandler).handleError; | ||
const store = TestBed.get(Store); | ||
dispatch = store.dispatch; | ||
storeNext = store.next; | ||
} | ||
|
||
it('should retry and notify error handler when effect error handler is not provided', () => { | ||
makeEffectTestBed(); | ||
|
||
expect(subscriptionCount).toBe(2); | ||
expect(globalErrorHandler).toHaveBeenCalledWith(new Error('effectError')); | ||
}); | ||
|
||
it('should use custom error behavior when EFFECTS_ERROR_HANDLER is provided', () => { | ||
const effectsErrorHandlerSpy = jasmine.createSpy(); | ||
effectsErrorHandlerSpy.and.callFake((_$, errorHandler: ErrorHandler) => { | ||
errorHandler.handleError(new Error('inside custom handler')); | ||
|
||
return of({ type: 'custom action' }); | ||
}); | ||
|
||
makeEffectTestBed({ | ||
provide: EFFECTS_ERROR_HANDLER, | ||
useValue: effectsErrorHandlerSpy, | ||
}); | ||
|
||
expect(effectsErrorHandlerSpy).toHaveBeenCalledWith( | ||
jasmine.any(Observable), | ||
TestBed.get(ErrorHandler) | ||
); | ||
expect(globalErrorHandler).toHaveBeenCalledWith( | ||
new Error('inside custom handler') | ||
); | ||
expect(subscriptionCount).toBe(0); | ||
expect(storeNext).toHaveBeenCalledWith({ type: 'custom action' }); | ||
}); | ||
|
||
class ErrorEffect { | ||
effect$ = createEffect(errorFirstSubscriber, { | ||
useEffectsErrorHandler: true, | ||
}); | ||
} | ||
|
||
function errorFirstSubscriber(): Observable<Action> { | ||
return new Observable(observer => { | ||
subscriptionCount++; | ||
|
||
if (subscriptionCount === 1) { | ||
observer.error(new Error('effectError')); | ||
} | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.