Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ErrorObservable): Added and fixed failing test for Observable.catch #2552

Merged
merged 2 commits into from
May 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 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,83 @@ describe('Observable.prototype.catch', () => {
done();
});
});

context('fromPromise', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is a nitpick... but everywhere else I think we use describe. I didn't even know context is a thing. Is there a reason this is better in this case than describe?

Copy link
Contributor Author

@trshafer trshafer May 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to change it to describe, didn't realize there are no other usages of context in the test suite. I personally have used context to mean "a different approach to call the same feature" where describe is a "different feature". There is no difference in functionality.

describe('functionA', () => {
  context('success', () => {...});
  context('error', () => {...});
});

Documentation: https://mochajs.org/#bdd

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's fine I think.

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

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

beforeEach(() => {
trueSetTimeout = global.setTimeout;
sandbox = sinon.sandbox.create();
timers = sandbox.useFakeTimers();
});

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

it('should chain a throw from a promise using throw', (done: MochaDone) => {
const subscribeSpy = sinon.spy();
const testError = new Error('BROKEN PROMISE');
Observable.fromPromise(Promise.reject(testError)).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();
const testError = new Error('BROKEN PROMISE');
Observable.fromPromise(Promise.reject(testError)).catch(err =>
Observable.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();
const errorSpy = sinon.spy();
const thrownError = new Error('BROKEN THROW');
const testError = new Error('BROKEN PROMISE');
Observable.fromPromise(Promise.reject(testError)).catch(err =>
Observable.throw(thrownError)
).subscribe(subscribeSpy, errorSpy);

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

});
5 changes: 4 additions & 1 deletion src/observable/ErrorObservable.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { IScheduler } from '../Scheduler';
import { Observable } from '../Observable';
import { TeardownLogic } from '../Subscription';
import { Subscriber } from '../Subscriber';

export interface DispatchArg {
error: any;
Expand Down Expand Up @@ -67,10 +68,12 @@ export class ErrorObservable extends Observable<any> {
super();
}

protected _subscribe(subscriber: any): TeardownLogic {
protected _subscribe(subscriber: Subscriber<any>): TeardownLogic {
const error = this.error;
const scheduler = this.scheduler;

subscriber.syncErrorThrowable = true;

if (scheduler) {
return scheduler.schedule(ErrorObservable.dispatch, 0, {
error, subscriber
Expand Down