Skip to content

Commit

Permalink
fix(fromEvent): Throw if event target is invalid (#2107)
Browse files Browse the repository at this point in the history
  • Loading branch information
jayphelps authored and benlesh committed Nov 5, 2016
1 parent 39214f2 commit 147ce3e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .markdown-doctest-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ module.exports = {
globals: {
document: {
querySelector: function () {
return {addEventListener: function () {}}
return {
addEventListener: function () {},
removeEventListener: function () {}
}
}
},
hot: marbleTesting.hot,
Expand Down
11 changes: 11 additions & 0 deletions spec/observables/fromEvent-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,17 @@ describe('Observable.fromEvent', () => {
expect(offHandler).to.equal(onHandler);
});

it('should error on invalid event targets', () => {
const obj = {
addListener: () => {
//noop
}
};

const subscribe = () => Observable.fromEvent(<any>obj, 'click').subscribe();
expect(subscribe).to.throw(TypeError, 'Invalid event target');
});

it('should pass through options to addEventListener', () => {
let actualOptions;
const expectedOptions = { capture: true, passive: true };
Expand Down
2 changes: 2 additions & 0 deletions src/observable/FromEventObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ export class FromEventObservable<T> extends Observable<T> {
const source = sourceObj;
sourceObj.addListener(eventName, handler);
unsubscribe = () => source.removeListener(eventName, handler);
} else {
throw new TypeError('Invalid event target');
}

subscriber.add(new Subscription(unsubscribe));
Expand Down

0 comments on commit 147ce3e

Please sign in to comment.