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 1f1a0af
Showing
17 changed files
with
308 additions
and
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { ErrorHandler, Provider } from '@angular/core'; | ||
import { TestBed } from '@angular/core/testing'; | ||
import { Action, Store } from '@ngrx/store'; | ||
import { Observable, of } from 'rxjs'; | ||
import { catchError } from 'rxjs/operators'; | ||
import { | ||
createEffect, | ||
EFFECTS_ERROR_HANDLER, | ||
EffectsModule, | ||
EffectSources, | ||
} from '..'; | ||
|
||
describe('NgRx Effects Error Handler spec', () => { | ||
let subscriptionCount: number; | ||
let globalErrorHandler: jasmine.Spy; | ||
let storeNext: jasmine.Spy; | ||
|
||
function makeEffectTestBed(...providers: Provider[]) { | ||
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, | ||
], | ||
}); | ||
|
||
globalErrorHandler = TestBed.get(ErrorHandler).handleError; | ||
const store = TestBed.get(Store); | ||
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')); | ||
}); | ||
|
||
fit('should use custom error behavior when EFFECTS_ERROR_HANDLER is provided', () => { | ||
const effectsErrorHandlerSpy = jasmine | ||
.createSpy() | ||
.and.callFake((effect$: Observable<any>, errorHandler: ErrorHandler) => { | ||
return effect$.pipe( | ||
catchError(err => { | ||
errorHandler.handleError( | ||
new Error('inside custom handler: ' + err.message) | ||
); | ||
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: effectError') | ||
); | ||
expect(subscriptionCount).toBe(1); | ||
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.