Skip to content

Commit

Permalink
Merge pull request #1473 from emberjs/fix-test-failures
Browse files Browse the repository at this point in the history
Get tests passing w/ Chrome's new selectionchange event behavior
  • Loading branch information
NullVoxPopuli authored Aug 19, 2024
2 parents ff604ae + ce8cdcb commit 5aad468
Show file tree
Hide file tree
Showing 10 changed files with 505 additions and 221 deletions.
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) {
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',
];
}

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

0 comments on commit 5aad468

Please sign in to comment.