Skip to content

Commit

Permalink
Merge pull request #685 from evanfarina/master
Browse files Browse the repository at this point in the history
Default `click` and `doubleClick` to be left button clicks.
  • Loading branch information
rwjblue authored Apr 21, 2020
2 parents a78d297 + fea9875 commit 00618f4
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 18 deletions.
21 changes: 18 additions & 3 deletions addon-test-support/@ember/test-helpers/dom/click.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { assign } from '@ember/polyfills';
import getElement from './-get-element';
import fireEvent from './fire-event';
import { __focus__ } from './focus';
Expand All @@ -8,10 +9,22 @@ import isFormControl from './-is-form-control';
import Target from './-target';
import { log } from '@ember/test-helpers/dom/-logging';

const PRIMARY_BUTTON = 1;
const MAIN_BUTTON_PRESSED = 0;

/**
* Represent a particular mouse button being clicked.
* See https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/buttons for available options.
*/
export const DEFAULT_CLICK_OPTIONS = {
buttons: PRIMARY_BUTTON,
button: MAIN_BUTTON_PRESSED,
};

/**
@private
@param {Element} element the element to click on
@param {Object} options the options to be merged into the mouse events
@param {MouseEventInit} options the options to be merged into the mouse events
*/
export function __click__(element: Element | Document, options: MouseEventInit): void {
fireEvent(element, 'mousedown', options);
Expand Down Expand Up @@ -53,7 +66,7 @@ export function __click__(element: Element | Document, options: MouseEventInit):
@public
@param {string|Element} target the element or selector to click on
@param {Object} options the options to be merged into the mouse events
@param {MouseEventInit} _options the options to be merged into the mouse events.
@return {Promise<void>} resolves when settled
@example
Expand All @@ -69,9 +82,11 @@ export function __click__(element: Element | Document, options: MouseEventInit):
click('button', { shiftKey: true });
*/
export default function click(target: Target, options: MouseEventInit = {}): Promise<void> {
export default function click(target: Target, _options: MouseEventInit = {}): Promise<void> {
log('click', target);

let options = assign({}, DEFAULT_CLICK_OPTIONS, _options);

return nextTickPromise().then(() => {
if (!target) {
throw new Error('Must pass an element or selector to `click`.');
Expand Down
10 changes: 7 additions & 3 deletions addon-test-support/@ember/test-helpers/dom/double-click.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { assign } from '@ember/polyfills';
import getElement from './-get-element';
import fireEvent from './fire-event';
import { __focus__ } from './focus';
import settled from '../settled';
import isFocusable from './-is-focusable';
import { nextTickPromise } from '../-utils';
import { DEFAULT_CLICK_OPTIONS } from './click';
import Target from './-target';
import { log } from '@ember/test-helpers/dom/-logging';
import isFormControl from './-is-form-control';

/**
@private
@param {Element} element the element to double-click on
@param {Object} options the options to be merged into the mouse events
@param {MouseEventInit} options the options to be merged into the mouse events
*/
export function __doubleClick__(element: Element | Document, options: MouseEventInit): void {
fireEvent(element, 'mousedown', options);
Expand Down Expand Up @@ -64,7 +66,7 @@ export function __doubleClick__(element: Element | Document, options: MouseEvent
@public
@param {string|Element} target the element or selector to double-click on
@param {Object} options the options to be merged into the mouse events
@param {MouseEventInit} _options the options to be merged into the mouse events
@return {Promise<void>} resolves when settled
@example
Expand All @@ -81,9 +83,11 @@ export function __doubleClick__(element: Element | Document, options: MouseEvent
doubleClick('button', { shiftKey: true });
*/
export default function doubleClick(target: Target, options: MouseEventInit = {}): Promise<void> {
export default function doubleClick(target: Target, _options: MouseEventInit = {}): Promise<void> {
log('doubleClick', target);

let options = assign({}, DEFAULT_CLICK_OPTIONS, _options);

return nextTickPromise().then(() => {
if (!target) {
throw new Error('Must pass an element or selector to `doubleClick`.');
Expand Down
14 changes: 11 additions & 3 deletions tests/unit/dom/click-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,20 @@ module('DOM Helper: click', function (hooks) {
assert.verifySteps(['mousedown', 'mouseup', 'click']);
});

test('clicking passes options through to mouse events', async function (assert) {
element = buildInstrumentedElement('div', ['clientX', 'clientY', 'button']);
test('clicking passes default options through to mouse events', async function (assert) {
element = buildInstrumentedElement('div', ['buttons', 'button']);

await click(element);

assert.verifySteps(['mousedown 1 0', 'mouseup 1 0', 'click 1 0']);
});

test('clicking passes options through to mouse events and merges with default options', async function (assert) {
element = buildInstrumentedElement('div', ['clientX', 'clientY', 'button', 'buttons']);

await click(element, { clientX: 13, clientY: 17, button: 2 });

assert.verifySteps(['mousedown 13 17 2', 'mouseup 13 17 2', 'click 13 17 2']);
assert.verifySteps(['mousedown 13 17 2 1', 'mouseup 13 17 2 1', 'click 13 17 2 1']);
});

test('clicking accepts modifiers', async function (assert) {
Expand Down
34 changes: 25 additions & 9 deletions tests/unit/dom/double-click-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,19 +130,35 @@ module('DOM Helper: doubleClick', function (hooks) {
);
});

test('double-clicking passes options through to mouse events', async function (assert) {
element = buildInstrumentedElement('div', ['clientX', 'clientY', 'button']);
test('double-clicking passes default options through to mouse events', async function (assert) {
element = buildInstrumentedElement('div', ['button', 'buttons']);

await doubleClick(element);

assert.verifySteps([
'mousedown 0 1',
'mouseup 0 1',
'click 0 1',
'mousedown 0 1',
'mouseup 0 1',
'click 0 1',
'dblclick 0 1',
]);
});

test('double-clicking passes options through to mouse events and merges with default options', async function (assert) {
element = buildInstrumentedElement('div', ['clientX', 'clientY', 'button', 'buttons']);

await doubleClick(element, { clientX: 13, clientY: 17, button: 1 });

assert.verifySteps([
'mousedown 13 17 1',
'mouseup 13 17 1',
'click 13 17 1',
'mousedown 13 17 1',
'mouseup 13 17 1',
'click 13 17 1',
'dblclick 13 17 1',
'mousedown 13 17 1 1',
'mouseup 13 17 1 1',
'click 13 17 1 1',
'mousedown 13 17 1 1',
'mouseup 13 17 1 1',
'click 13 17 1 1',
'dblclick 13 17 1 1',
]);
});
});
Expand Down

0 comments on commit 00618f4

Please sign in to comment.