|
1 | 1 | import { expect } from 'chai';
|
2 |
| -import { concat, Observable, of, throwError, EMPTY, from } from 'rxjs'; |
3 |
| -import { catchError, map, mergeMap } from 'rxjs/operators'; |
| 2 | +import { concat, defer, Observable, of, throwError, EMPTY, from } from 'rxjs'; |
| 3 | +import { catchError, map, mergeMap, takeWhile } from 'rxjs/operators'; |
4 | 4 | import { TestScheduler } from 'rxjs/testing';
|
5 | 5 | import * as sinon from 'sinon';
|
6 | 6 | import { createObservableInputs } from '../helpers/test-helper';
|
@@ -121,6 +121,31 @@ describe('catchError operator', () => {
|
121 | 121 | expectSubscriptions(e2.subscriptions).toBe(e2subs);
|
122 | 122 | });
|
123 | 123 |
|
| 124 | + it('should stop listening to a synchronous observable when unsubscribed', () => { |
| 125 | + const sideEffects: number[] = []; |
| 126 | + const synchronousObservable = concat( |
| 127 | + defer(() => { |
| 128 | + sideEffects.push(1); |
| 129 | + return of(1); |
| 130 | + }), |
| 131 | + defer(() => { |
| 132 | + sideEffects.push(2); |
| 133 | + return of(2); |
| 134 | + }), |
| 135 | + defer(() => { |
| 136 | + sideEffects.push(3); |
| 137 | + return of(3); |
| 138 | + }) |
| 139 | + ); |
| 140 | + |
| 141 | + throwError(new Error('Some error')).pipe( |
| 142 | + catchError(() => synchronousObservable), |
| 143 | + takeWhile((x) => x != 2) // unsubscribe at the second side-effect |
| 144 | + ).subscribe(() => { /* noop */ }); |
| 145 | + |
| 146 | + expect(sideEffects).to.deep.equal([1, 2]); |
| 147 | + }); |
| 148 | + |
124 | 149 | it('should catch error and replace it with a hot Observable', () => {
|
125 | 150 | const e1 = hot('--a--b--# ');
|
126 | 151 | const e1subs = '^ ! ';
|
|
0 commit comments