Skip to content

Commit

Permalink
test(Observable.catch): Added failing test when errors bubble up from…
Browse files Browse the repository at this point in the history
… a PromiseObservable.
  • Loading branch information
trshafer committed Apr 13, 2017
1 parent e387113 commit 49dae47
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions spec/operators/catch-spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {expect} from 'chai';
import * as Rx from '../../dist/cjs/Rx';
import * as sinon from 'sinon';
import {createObservableInputs} from '../helpers/test-helper';
import marbleTestingSignature = require('../helpers/marble-testing'); // tslint:disable-line:no-require-imports

Expand Down Expand Up @@ -278,4 +279,66 @@ describe('Observable.prototype.catch', () => {
done();
});
});

context('fromPromise', () => {
type SetTimeout = (callback: (...args: any[]) => void, ms: number, ...args: any[]) => NodeJS.Timer;

let trueSetTimeout: SetTimeout;
let sandbox: sinon.SinonSandbox;
let timers: sinon.SinonFakeTimers;

let source: Rx.Observable<any>;
beforeEach(() => {
trueSetTimeout = global.setTimeout;
sandbox = sinon.sandbox.create();
timers = sandbox.useFakeTimers();

const testError = new Error('BROKEN PROMISE');
source = Observable.fromPromise(Promise.reject(testError));
});

afterEach(() => {
sandbox.restore();
});

it.only('should chain a throw from a promise using throw', (done: MochaDone) => {
const subscribeSpy = sinon.spy();
source.catch(err => {
throw new Error('BROKEN THROW');
}).subscribe(subscribeSpy);

trueSetTimeout(() => {
try {
timers.tick(1);
} catch (e) {
expect(subscribeSpy).not.to.be.called;
expect(e.message).to.equal('BROKEN THROW');
return done();
}
done(new Error('This should have thrown an error'));
}, 0);
});

it('should chain a throw from a promise using Observable.throw', (done: MochaDone) => {
const subscribeSpy = sinon.spy();
source.catch(err =>
Observable.throw(new Error('BROKEN THROW'))
).subscribe(subscribeSpy);

trueSetTimeout(() => {
try {
console.log('before tick');
timers.tick(1);
console.log('after tick');
} catch (e) {
expect(subscribeSpy).not.to.be.called;
expect(e.message).to.equal('BROKEN THROW');
return done();
}
console.log('calling done');
done(new Error('This should have thrown an error'));
}, 0);
});
});

});

0 comments on commit 49dae47

Please sign in to comment.