Skip to content

Commit

Permalink
fix(fromEvent): throw if passed invalid target
Browse files Browse the repository at this point in the history
  • Loading branch information
cartant committed Mar 14, 2021
1 parent b509262 commit ac45743
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 ac45743

Please sign in to comment.