diff --git a/.markdown-doctest-setup.js b/.markdown-doctest-setup.js index 075223da95..d532c05291 100644 --- a/.markdown-doctest-setup.js +++ b/.markdown-doctest-setup.js @@ -23,7 +23,10 @@ module.exports = { globals: { document: { querySelector: function () { - return {addEventListener: function () {}} + return { + addEventListener: function () {}, + removeEventListener: function () {} + } } }, hot: marbleTesting.hot, diff --git a/spec/observables/fromEvent-spec.ts b/spec/observables/fromEvent-spec.ts index 4790e24fa1..c608321f53 100644 --- a/spec/observables/fromEvent-spec.ts +++ b/spec/observables/fromEvent-spec.ts @@ -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(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 }; diff --git a/src/observable/FromEventObservable.ts b/src/observable/FromEventObservable.ts index edd5942617..1b1bb67bf5 100644 --- a/src/observable/FromEventObservable.ts +++ b/src/observable/FromEventObservable.ts @@ -133,6 +133,8 @@ export class FromEventObservable extends Observable { const source = sourceObj; sourceObj.addListener(eventName, handler); unsubscribe = () => source.removeListener(eventName, handler); + } else { + throw new TypeError('Invalid event target'); } subscriber.add(new Subscription(unsubscribe));