Skip to content

Commit

Permalink
test(catch): add comprehensive marbles tests for catch
Browse files Browse the repository at this point in the history
  • Loading branch information
staltz committed Nov 22, 2015
1 parent 3f85538 commit a134d15
Showing 1 changed file with 144 additions and 68 deletions.
212 changes: 144 additions & 68 deletions spec/operators/catch-spec.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
});
});

0 comments on commit a134d15

Please sign in to comment.