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

Conversation

nivekcode
Copy link

We have a project that uses CDK for testing Angular components. Some components are wrappers around a third-party library. We have a case where we need to pass down an event to a third-party component and are out of control how many times preventDefault is called. Currently, our test suite fails due to the CDK if preventDefault is called multiple times.

On each preventDefault call the CDK tries to redefine the read-only Event Property defaultPrevented. In think that we should not fail on multiple preventDefault calls. From my point of view, it's not ideal to call preventDefault multiple times but also doesn't hurt.

This fix only tries to redefine defaultPrevented if it doesn't exist on the event.

@google-cla google-cla bot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Feb 18, 2021
@nivekcode nivekcode force-pushed the feature/multiplePreventDefaultsAllowed branch from d79a351 to 3ea0462 Compare February 18, 2021 11:20
@@ -46,7 +46,9 @@ 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);
if(!event.defaultPrevented) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it make sense to also move the call to originalPreventDefault up? That way non-IE browsers will never need to call defineReadonlyEventProperty

const result = originalPreventDefault();
if(!event.defaultPrevented) {
  defineReadonlyEventProperty(event, 'defaultPrevented', true);
}
return result;

Copy link
Author

Choose a reason for hiding this comment

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

@mmalerba yes. that sounds reasonable. will update the PR

Copy link
Member

@crisbeto crisbeto left a comment

Choose a reason for hiding this comment

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

How does your test fail if preventDefault is called multiple times? My assumption was that it should be possible, because we're passing in configurable into defineProperty.

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.

@devversion
Copy link
Member

Since we'll drop IE11 support for v13, we can remove this hack anyway. Closing in favor of #23394. Thanks for the contribution, and sorry it was on the backburner for some while.

@devversion devversion closed this Aug 18, 2021
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 18, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla: yes PR author has agreed to Google's Contributor License Agreement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants