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

Update buildMouseEvent to use MouseEvent constructor if it is present #387

Merged
merged 3 commits into from
Jul 31, 2018
Merged
Changes from 2 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
59 changes: 37 additions & 22 deletions addon-test-support/@ember/test-helpers/dom/fire-event.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { merge } from '@ember/polyfills';

// eslint-disable-next-line require-jsdoc
function checkMouseEventExistence() {
try {
Copy link
Contributor

Choose a reason for hiding this comment

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

can you memoize this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you have an example of where else Memoization it is done in this repo? Or another example I can emulate?

Copy link
Member

Choose a reason for hiding this comment

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

Something like:

const MOUSE_EVENT_CONSTRUCTOR = (() => {
  try {
    new MouseEvent(‘test’);
    return true;
  } catch(e) {
    return false;
  }
})();

new MouseEvent('test');
return true;
} catch (e) {
return false;
}
}
const MOUSE_EVENT_EXISTS = checkMouseEventExistence();
const DEFAULT_EVENT_OPTIONS = { bubbles: true, cancelable: true };
export const KEYBOARD_EVENT_TYPES = Object.freeze(['keydown', 'keypress', 'keyup']);
const MOUSE_EVENT_TYPES = [
Expand Down Expand Up @@ -84,29 +94,34 @@ function buildBasicEvent(type, options = {}) {
// eslint-disable-next-line require-jsdoc
function buildMouseEvent(type, options = {}) {
let event;
try {
event = document.createEvent('MouseEvents');
let eventOpts = merge(merge({}, DEFAULT_EVENT_OPTIONS), options);
event.initMouseEvent(
type,
eventOpts.bubbles,
eventOpts.cancelable,
window,
eventOpts.detail,
eventOpts.screenX,
eventOpts.screenY,
eventOpts.clientX,
eventOpts.clientY,
eventOpts.ctrlKey,
eventOpts.altKey,
eventOpts.shiftKey,
eventOpts.metaKey,
eventOpts.button,
eventOpts.relatedTarget
);
} catch (e) {
event = buildBasicEvent(type, options);
let eventOpts = merge(merge({}, DEFAULT_EVENT_OPTIONS), options);
if (MOUSE_EVENT_EXISTS) {
event = new MouseEvent(type, eventOpts);
} else {
try {
event = document.createEvent('MouseEvents');
event.initMouseEvent(
type,
eventOpts.bubbles,
eventOpts.cancelable,
window,
eventOpts.detail,
eventOpts.screenX,
eventOpts.screenY,
eventOpts.clientX,
eventOpts.clientY,
eventOpts.ctrlKey,
eventOpts.altKey,
eventOpts.shiftKey,
eventOpts.metaKey,
eventOpts.button,
eventOpts.relatedTarget
);
} catch (e) {
event = buildBasicEvent(type, options);
}
}

return event;
}

Expand Down