From a134d15900527916f484303b3e0710dc5a53bcbb Mon Sep 17 00:00:00 2001 From: Andre Staltz Date: Sun, 22 Nov 2015 21:36:34 -0200 Subject: [PATCH] test(catch): add comprehensive marbles tests for catch --- spec/operators/catch-spec.js | 212 ++++++++++++++++++++++++----------- 1 file changed, 144 insertions(+), 68 deletions(-) diff --git a/spec/operators/catch-spec.js b/spec/operators/catch-spec.js index 45a8924bbf..4c96a90475 100644 --- a/spec/operators/catch-spec.js +++ b/spec/operators/catch-spec.js @@ -1,48 +1,68 @@ -/* globals describe, it, expect */ +/* globals describe, it, expect, hot, cold, expectObservable, expectSubscriptions, rxTestScheduler */ var Rx = require('../../dist/cjs/Rx'); var Observable = Rx.Observable; describe('Observable.prototype.catch()', function () { - it('should pass the error as the first argument', function (done) { - Observable.throw('bad') - .catch(function (err) { - expect(err).toBe('bad'); - return Observable.empty(); - }) - .subscribe(function () { }, - function (err) { - done.fail('should not be called'); - }, - done); - }); + it('should catch error and replace it with Observable.of()', function () { + var e1 = hot('--a--b--c--------|'); + var subs = '^ !'; + var expected = '--a--b--(XYZ|)'; - it('should catch the error and allow the return of a new observable to use', function (done) { - var expected = [1, 2, 'foo']; - Observable.of(1, 2, 3) + var result = e1 .map(function (n) { - if (n === 3) { + if (n === 'c') { throw 'bad'; } return n; }) .catch(function (err) { - return Observable.of('foo'); - }) - .subscribe(function (x) { - expect(x).toBe(expected.shift()); - }, function (err) { - done.fail('should not be called'); - }, function () { - done(); + return Observable.of('X', 'Y', 'Z'); }); + + expectObservable(result).toBe(expected); + expectSubscriptions(e1.subscriptions).toBe(subs); + }); + + it('should catch error and replace it with a cold Observable', function () { + var e1 = hot('--a--b--#----| '); + var e1subs = '^ !'; + var e2 = cold( '1-2-3-4-5-|'); + var e2subs = ' ^ !'; + var expected = '--a--b--1-2-3-4-5-|'; + + var result = e1.catch(function (err) { return e2; }); + + expectObservable(result).toBe(expected); + expectSubscriptions(e1.subscriptions).toBe(e1subs); + expectSubscriptions(e2.subscriptions).toBe(e2subs); }); - it('should catch and allow the observable to be repeated with the third (caught) argument', function (done) { - var expected = [1, 2, 1, 2, 1, 2]; + it('should catch error and replace it with a hot Observable', function () { + var e1 = hot('--a--b--#----| '); + var e1subs = '^ !'; + var e2 = hot('1-2-3-4-5-6-7-8-9-|'); + var e2subs = ' ^ !'; + var expected = '--a--b--5-6-7-8-9-|'; + + var result = e1.catch(function (err) { return e2; }); + + expectObservable(result).toBe(expected); + expectSubscriptions(e1.subscriptions).toBe(e1subs); + expectSubscriptions(e2.subscriptions).toBe(e2subs); + }); + + it('should catch and allow the cold observable to be repeated with the third ' + + '(caught) argument', function () { + var e1 = cold('--a--b--c--------| '); + var subs = ['^ !', + ' ^ !', + ' ^ !']; + var expected = '--a--b----a--b----a--b--#'; + var retries = 0; - Observable.of(1, 2, 3) + var result = e1 .map(function (n) { - if (n === 3) { + if (n === 'c') { throw 'bad'; } return n; @@ -52,57 +72,113 @@ describe('Observable.prototype.catch()', function () { throw 'done'; } return caught; - }) - .subscribe(function (x) { - expect(x).toBe(expected.shift()); - }, function (err) { - expect(err).toBe('done'); - done(); - }, function () { - done.fail('should not be called'); }); + + expectObservable(result).toBe(expected, undefined, 'done'); + expectSubscriptions(e1.subscriptions).toBe(subs); }); - it('should complete if you return Observable.empty()', function (done) { - var expected = [1, 2]; - Observable.of(1, 2, 3) + it('should catch and allow the hot observable to proceed with the third ' + + '(caught) argument', function () { + var e1 = hot('--a--b--c----d---|'); + var subs = ['^ !', + ' ^ !']; + var expected = '--a--b-------d---|'; + + var retries = 0; + var result = e1 .map(function (n) { - if (n === 3) { + if (n === 'c') { throw 'bad'; } return n; }) - .catch(function (err) { - return Observable.empty(); - }) - .subscribe(function (x) { - expect(x).toBe(expected.shift()); - }, function (err) { - done.fail('should not be called'); - }, function () { - done(); + .catch(function (err, caught) { + if (retries++ === 2) { + throw 'done'; + } + return caught; }); + + expectObservable(result).toBe(expected, undefined, 'done'); + expectSubscriptions(e1.subscriptions).toBe(subs); }); - it('should error if you return Observable.throw()', function (done) { - var expected = [1, 2]; - Observable.of(1, 2, 3) - .map(function (n) { - if (n === 3) { - throw 'bad'; - } - return n; - }) + it('should catch and replace a Observable.throw() as the source', function () { + var e1 = cold('#'); + var subs = '(^!)'; + var expected = '(abc|)'; + + var result = e1.catch(function (err) { + return Observable.of('a', 'b', 'c'); + }); + + expectObservable(result).toBe(expected); + expectSubscriptions(e1.subscriptions).toBe(subs); + }); + + it('should mirror the source if it does not raise errors', function () { + var e1 = cold('--a--b--c--|'); + var subs = '^ !'; + var expected = '--a--b--c--|'; + + var result = e1.catch(function (err) { + return Observable.of('x', 'y', 'z'); + }); + + expectObservable(result).toBe(expected); + expectSubscriptions(e1.subscriptions).toBe(subs); + }); + + it('should complete if you return Observable.empty()', function () { + var e1 = hot('--a--b--#'); + var subs = '^ !'; + var expected = '--a--b--|'; + + var result = e1.catch(function (err) { + return Observable.empty(); + }); + + expectObservable(result).toBe(expected); + expectSubscriptions(e1.subscriptions).toBe(subs); + }); + + it('should raise error if you return Observable.throw()', function () { + var e1 = hot('--a--b--#'); + var subs = '^ !'; + var expected = '--a--b--#'; + + var result = e1.catch(function (err) { + return Observable.throw('error'); + }); + + expectObservable(result).toBe(expected); + expectSubscriptions(e1.subscriptions).toBe(subs); + }); + + it('should never terminate if you return Observable.never()', function () { + var e1 = hot('--a--b--#'); + var subs = '^ '; + var expected = '--a--b---'; + + var result = e1.catch(function (err) { + return Observable.never(); + }); + + expectObservable(result).toBe(expected); + expectSubscriptions(e1.subscriptions).toBe(subs); + }); + + it('should pass the error as the first argument', function (done) { + Observable.throw('bad') .catch(function (err) { - return Observable.throw('haha'); + expect(err).toBe('bad'); + return Observable.empty(); }) - .subscribe(function (x) { - expect(x).toBe(expected.shift()); - }, function (err) { - expect(err).toBe('haha'); - done(); - }, function () { - done.fail('should not be called'); - }); + .subscribe(function () { }, + function (err) { + done.fail('should not be called'); + }, + done); }); });