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

Get tests passing w/ Chrome's new selectionchange event behavior #1473

Merged
merged 11 commits into from
Aug 19, 2024
3 changes: 3 additions & 0 deletions .github/workflows/ci-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ jobs:
cache: pnpm
- name: Install Dependencies
run: pnpm install
- run: |
google-chrome --version

- name: Build addon
working-directory: addon
run: pnpm build
Expand Down
5 changes: 4 additions & 1 deletion test-app/tests/helpers/browser-detect.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export const isEdge = navigator.userAgent.indexOf('Edge') >= 0;

// Unlike Chrome, Firefox emits `selectionchange` events.
// Firefox emits `selectionchange` events.
export const isFirefox = navigator.userAgent.indexOf('Firefox') >= 0;

// Chrome emits `selectionchange` events.
export const isChrome = navigator.userAgent.indexOf('Chrome') >= 0;
12 changes: 10 additions & 2 deletions test-app/tests/unit/dom/blur-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
registerHook,
} from '@ember/test-helpers';
import { buildInstrumentedElement, insertElement } from '../../helpers/events';
import { isEdge } from '../../helpers/browser-detect';
import { isEdge, isChrome } from '../../helpers/browser-detect';
import hasEmberVersion from '@ember/test-helpers/has-ember-version';
import { createDescriptor } from 'dom-element-descriptors';

Expand All @@ -18,6 +18,10 @@ if (isEdge) {
blurSteps = ['focusout', 'blur'];
}

if (isChrome) {
Copy link
Member

Choose a reason for hiding this comment

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

Why did we not do selectionchange for Firefox since that supports it also?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The behavior is slightly different.

When I was working on this PR, I had 5 browsers open

  • FireFox
  • FireFox Developer Edition
  • Safari
  • Chrome
  • Chrome Canary

😅

focusSteps.push('selectionchange');
}

module('DOM Helper: blur', function (hooks) {
if (!hasEmberVersion(2, 4)) {
return;
Expand Down Expand Up @@ -53,7 +57,11 @@ module('DOM Helper: blur', function (hooks) {
});

test('it executes registered blur hooks', async function (assert) {
assert.expect(13);
if (isChrome) {
assert.expect(15);
} else {
assert.expect(13);
}

let element = document.createElement('input');
insertElement(element);
Expand Down
105 changes: 85 additions & 20 deletions test-app/tests/unit/dom/click-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
insertElement,
} from '../../helpers/events';
import hasEmberVersion from '@ember/test-helpers/has-ember-version';
import { isChrome } from '../../helpers/browser-detect';
import {
registerHooks,
unregisterHooks,
Expand Down Expand Up @@ -202,6 +203,17 @@ module('DOM Helper: click', function (hooks) {
module('focusable element types', function () {
let clickSteps = ['mousedown', 'focus', 'focusin', 'mouseup', 'click'];

if (isChrome) {
clickSteps = [
'mousedown',
'focus',
'focusin',
'mouseup',
'click',
'selectionchange',
Copy link
Member

Choose a reason for hiding this comment

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

Should we just push selectionchange to the existing list?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

then we'd need to add inverted conditions for safari and firefox

];
}

test('clicking a input via selector with context set', async function (assert) {
element = buildInstrumentedElement('input');

Expand Down Expand Up @@ -297,7 +309,17 @@ module('DOM Helper: click', function (hooks) {

await click(child);

assert.verifySteps(clickSteps);
if (isChrome) {
assert.verifySteps([
'mousedown',
'focus',
'focusin',
'mouseup',
'click',
]);
} else {
assert.verifySteps(clickSteps);
}
assert.strictEqual(
document.activeElement,
element,
Expand Down Expand Up @@ -334,15 +356,28 @@ module('DOM Helper: click', function (hooks) {
await click(focusableElement);
await click(element);

assert.verifySteps([
'mousedown',
'focus',
'focusin',
'mouseup',
'click',
'blur',
'focusout',
]);
if (isChrome) {
assert.verifySteps([
'mousedown',
'focus',
'focusin',
'mouseup',
'click',
'selectionchange',
'blur',
'focusout',
]);
} else {
assert.verifySteps([
'mousedown',
'focus',
'focusin',
'mouseup',
'click',
'blur',
'focusout',
]);
}
});

test('clicking on non-focusable element inside active element does not trigger blur on active element', async function (assert) {
Expand Down Expand Up @@ -377,15 +412,28 @@ module('DOM Helper: click', function (hooks) {
await click(focusableElement);
await click(element);

assert.verifySteps([
'mousedown',
'focus',
'focusin',
'mouseup',
'click',
'blur',
'focusout',
]);
if (isChrome) {
assert.verifySteps([
'mousedown',
'focus',
'focusin',
'mouseup',
'click',
'selectionchange',
'blur',
'focusout',
]);
} else {
assert.verifySteps([
'mousedown',
'focus',
'focusin',
'mouseup',
'click',
'blur',
'focusout',
]);
}
});

test('clicking on non-focusable element does not trigger blur on non-focusable active element', async function (assert) {
Expand Down Expand Up @@ -417,7 +465,24 @@ module('DOM Helper: click', function (hooks) {
await click(focusableElement);
await click(element);

assert.verifySteps(['mousedown', 'focus', 'focusin', 'mouseup', 'click']);
if (isChrome) {
assert.verifySteps([
'mousedown',
'focus',
'focusin',
'mouseup',
'click',
'selectionchange',
]);
} else {
assert.verifySteps([
'mousedown',
'focus',
'focusin',
'mouseup',
'click',
]);
}

element.removeEventListener('mousedown', preventDefault);
await click(element);
Expand Down
Loading