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(operators): add
mapResponse
ngrx#4230
- Loading branch information
Showing
3 changed files
with
97 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { noop, Observable, of, throwError } from 'rxjs'; | ||
import { mapResponse } from '..'; | ||
import { concatMap, finalize } from 'rxjs/operators'; | ||
|
||
describe('mapResponse', () => { | ||
it('should invoke next callback on next', () => { | ||
const nextCallback = jest.fn<void, [number]>(); | ||
|
||
of(1, 2, 3) | ||
.pipe( | ||
mapResponse({ | ||
next: nextCallback, | ||
error: noop, | ||
}) | ||
) | ||
.subscribe(); | ||
|
||
expect(nextCallback.mock.calls).toEqual([[1], [2], [3]]); | ||
}); | ||
|
||
it('should invoke error callback on error', () => { | ||
const errorCallback = jest.fn<void, [{ message: string }]>(); | ||
const error = { message: 'error' }; | ||
|
||
throwError(() => error) | ||
.pipe( | ||
mapResponse({ | ||
next: noop, | ||
error: errorCallback, | ||
}) | ||
) | ||
.subscribe(); | ||
|
||
expect(errorCallback).toHaveBeenCalledWith(error); | ||
}); | ||
|
||
it('should invoke error callback on the exception thrown in next', () => { | ||
const errorCallback = jest.fn<void, [{ message: string }]>(); | ||
const error = { message: 'error' }; | ||
|
||
function producesError() { | ||
throw error; | ||
} | ||
|
||
of(1) | ||
.pipe( | ||
mapResponse({ | ||
next: producesError, | ||
error: errorCallback, | ||
}) | ||
) | ||
.subscribe(); | ||
|
||
expect(errorCallback).toHaveBeenCalledWith(error); | ||
}); | ||
|
||
it('should not unsubscribe from outer observable on inner observable error', () => { | ||
const innerCompleteCallback = jest.fn<void, []>(); | ||
const outerCompleteCallback = jest.fn<void, []>(); | ||
|
||
new Observable((subscriber) => subscriber.next(1)) | ||
.pipe( | ||
concatMap(() => | ||
throwError(() => 'error').pipe( | ||
mapResponse({ | ||
next: noop, | ||
error: noop, | ||
}), | ||
finalize(innerCompleteCallback) | ||
) | ||
), | ||
finalize(outerCompleteCallback) | ||
) | ||
.subscribe(); | ||
|
||
expect(innerCompleteCallback).toHaveBeenCalled(); | ||
expect(outerCompleteCallback).not.toHaveBeenCalled(); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { concatLatestFrom } from './concat_latest_from'; | ||
export { mapResponse } from './map-response'; | ||
export { tapResponse } from './tap-response'; |
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,17 @@ | ||
import { Observable, of } from 'rxjs'; | ||
import { catchError, map } from 'rxjs/operators'; | ||
|
||
export function mapResponse<T, E, R1, R2>(observerOrNext: { | ||
next: (value: T) => R1; | ||
error: (error: E) => R2; | ||
}): (source$: Observable<T>) => Observable<R1 | R2> { | ||
return (source$) => | ||
source$.pipe( | ||
map((value) => { | ||
return observerOrNext.next(value); | ||
}), | ||
catchError((error) => { | ||
return of(observerOrNext.error(error)); | ||
}) | ||
); | ||
} |