Skip to content

Commit

Permalink
test(fromEventPattern): simplify test fixture using mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Jan 15, 2017
1 parent 6a55fc6 commit cb1be21
Showing 1 changed file with 18 additions and 37 deletions.
55 changes: 18 additions & 37 deletions spec/observables/fromEventPattern-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,28 @@ describe('Observable.fromEventPattern', () => {
.concat(Observable.never())
.subscribe(h);
}
const e1 = Observable.fromEventPattern(addHandler, () => void 0);
const e1 = Observable.fromEventPattern(addHandler);
const expected = '-----x-x---';
expectObservable(e1).toBe(expected, {x: 'ev'});
});

it('should call addHandler on subscription', () => {
let addHandlerCalledWith;
const addHandler = (h: any) => {
addHandlerCalledWith = h;
};

const removeHandler = () => {
//noop
};
const addHandler = sinon.spy();
Observable.fromEventPattern(addHandler, noop).subscribe(noop);

Observable.fromEventPattern(addHandler, removeHandler)
.subscribe(() => {
//noop
});

expect(addHandlerCalledWith).to.be.a('function');
const call = addHandler.getCall(0);
expect(addHandler).calledOnce;
expect(call.args[0]).to.be.a('function');
});

it('should call removeHandler on unsubscription', () => {
let removeHandlerCalledWith;
const addHandler = () => {
//noop
};
const removeHandler = (h: any) => {
removeHandlerCalledWith = h;
};

const subscription = Observable.fromEventPattern(addHandler, removeHandler)
.subscribe(() => {
//noop
});
const removeHandler = sinon.spy();

subscription.unsubscribe();
Observable.fromEventPattern(noop, removeHandler).subscribe(noop).unsubscribe();

expect(removeHandlerCalledWith).to.be.a('function');
const call = removeHandler.getCall(0);
expect(removeHandler).calledOnce;
expect(call.args[0]).to.be.a('function');
});

it('should work without optional removeHandler', () => {
Expand All @@ -77,16 +59,15 @@ describe('Observable.fromEventPattern', () => {
expect(call).calledWith(sinon.match.any, expected);
});

it('should send errors in addHandler down the error path', () => {
it('should send errors in addHandler down the error path', (done: MochaDone) => {
Observable.fromEventPattern((h: any) => {
throw 'bad';
}, () => {
//noop
}).subscribe(() => {
//noop
}, (err: any) => {
expect(err).to.equal('bad');
});
}, noop).subscribe(
() => done(new Error('should not be called')),
(err: any) => {
expect(err).to.equal('bad');
done();
}, () => done(new Error('should not be called')));
});

it('should accept a selector that maps outgoing values', (done: MochaDone) => {
Expand Down

0 comments on commit cb1be21

Please sign in to comment.