Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(fromEvent): throw if passed invalid target #6136

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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