Skip to content

Commit

Permalink
fix(fromEvent): throw if passed invalid target (#6136)
Browse files Browse the repository at this point in the history
Closes #5823
  • Loading branch information
cartant authored Mar 15, 2021
1 parent 81edb79 commit 317ba0c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
13 changes: 4 additions & 9 deletions spec/observables/fromEvent-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,20 +181,15 @@ describe('fromEvent', () => {
expect(offHandler).to.equal(onHandler);
});

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

fromEvent(obj as any, 'click').subscribe({
error(err: any) {
expect(err).to.exist
.and.be.instanceof(Error)
.and.have.property('message', 'Invalid event target');
}
});
expect(() => {
fromEvent(obj as any, 'click');
}).to.throw(/Invalid event target/)
});

it('should pass through options to addEventListener and removeEventListener', () => {
Expand Down
14 changes: 6 additions & 8 deletions src/internal/observable/fromEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,15 +245,13 @@ export function fromEvent<T>(
}
}

// If add is falsy and we made it here, it's because we didn't
// match any valid target objects above.
if (!add) {
throw new TypeError('Invalid event target');
}

return new Observable<T>((subscriber) => {
// If add is falsy and we made it here, it's because we didn't
// match any valid target objects above.
if (!add) {
// TODO: We should probably discuss if throwing this at subscription-time
// is appropriate. It seems like it would be better (and easier to debug)
// to throw this when `fromEvent()` is called.
throw new TypeError('Invalid event target');
}
// The handler we are going to register. Forwards the event object, by itself, or
// an array of arguments to the event handler, if there is more than one argument,
// to the consumer.
Expand Down

0 comments on commit 317ba0c

Please sign in to comment.