Skip to content

fix(testing): defineReadonlyEventProperty only if its not yet on the … #21944

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

Closed
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
22 changes: 18 additions & 4 deletions src/cdk/testing/testbed/fake-events/event-objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,15 @@ export function createMouseEvent(

// IE won't set `defaultPrevented` on synthetic events so we need to do it manually.
event.preventDefault = function() {
defineReadonlyEventProperty(event, 'defaultPrevented', true);
return originalPreventDefault();
/*
Calling originalPreventDefault before the if check ensures
that non IE apps never call defineReadonlyEventProperty
*/
const result = originalPreventDefault();
if (!event.defaultPrevented) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this mean that it could still fail if IE calls preventDefault multiple times?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No - If preventDefault is called then defaultPrevented is true and the property will not be redefined. As you said it would also work if we call it with configurable: true. By default configurable is false.

defineReadonlyEventProperty(event, 'defaultPrevented', true);
}
return result;
};

return event;
Expand Down Expand Up @@ -157,8 +164,15 @@ export function createKeyboardEvent(type: string, keyCode: number = 0, key: stri

// IE won't set `defaultPrevented` on synthetic events so we need to do it manually.
event.preventDefault = function() {
defineReadonlyEventProperty(event, 'defaultPrevented', true);
return originalPreventDefault();
/*
Calling originalPreventDefault before the if check ensures
that non IE apps never call defineReadonlyEventProperty
*/
const result = originalPreventDefault();
if (!event.defaultPrevented) {
defineReadonlyEventProperty(event, 'defaultPrevented', true);
}
return result;
};

return event;
Expand Down