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: [#1182] The handleEvent method should be called with the listener scope #1270

Merged
merged 2 commits into from
Feb 26, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/happy-dom/src/event/EventTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ export default abstract class EventTarget implements IEventTarget {
if ((<IEventListener>listener).handleEvent) {
WindowErrorUtility.captureError(
window,
(<IEventListener>listener).handleEvent.bind(this, event)
(<IEventListener>listener).handleEvent.bind(listener, event)
);
} else {
WindowErrorUtility.captureError(
Expand Down
13 changes: 13 additions & 0 deletions packages/happy-dom/test/event/EventTarget.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,19 @@ describe('EventTarget', () => {
eventTarget.dispatchEvent(dispatchedEvent);
expect(scope).toBe(eventTarget);
});

it('Event listener with handleEvent is called in the scope of the listener when calling dispatchEvent().', () => {
let scope = null;
const listener = {
handleEvent(): void {
scope = this;
}
};
const dispatchedEvent = new Event(EVENT_TYPE);
eventTarget.addEventListener(EVENT_TYPE, listener);
eventTarget.dispatchEvent(dispatchedEvent);
expect(scope).toBe(listener);
});
});

describe('removeEventListener()', () => {
Expand Down