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

test(fillIn,typeIn): account for selectionchange in Firefox #1195

Merged
merged 3 commits into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions tests/helpers/browser-detect.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ export const isIE11 = !window.ActiveXObject && 'ActiveXObject' in window;
export const isIE = 'ActiveXObject' in window;

export const isEdge = navigator.userAgent.indexOf('Edge') >= 0;

// Unlike Chrome, Firefox emits `selectionchange` events.
export const isFirefox = navigator.userAgent.indexOf('Firefox') >= 0;
Comment on lines +9 to +10
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wanted to do a proper feature detection instead of resorting to user agent parsing, but unfortunately that would have required async code, which is not possible here.

Copy link
Member

Choose a reason for hiding this comment

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

We could do it async, and have tests/test-helper.js call the function before calling start() to kick off the tests...

10 changes: 7 additions & 3 deletions tests/unit/dom/fill-in-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import {
_registerHook,
} from '@ember/test-helpers';
import { buildInstrumentedElement, insertElement } from '../../helpers/events';
import { isIE11 } from '../../helpers/browser-detect';
import { isIE11, isFirefox } from '../../helpers/browser-detect';
import hasEmberVersion from '@ember/test-helpers/has-ember-version';

let clickSteps = ['focus', 'focusin', 'input', 'change'];

if (isIE11) {
clickSteps = ['focusin', 'input', 'change', 'focus'];
} else if (isFirefox) {
clickSteps.push('selectionchange');
}

module('DOM Helper: fillIn', function (hooks) {
Expand Down Expand Up @@ -226,7 +228,8 @@ module('DOM Helper: fillIn', function (hooks) {
await setupContext(context);
await fillIn(element, 'foo');

assert.verifySteps(clickSteps);
// For this specific case, Firefox does not emit `selectionchange`.
Copy link
Member

Choose a reason for hiding this comment

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

Wat, why!?

assert.verifySteps(clickSteps.filter((s) => s !== 'selectionchange'));
assert.strictEqual(
document.activeElement,
element,
Expand Down Expand Up @@ -255,7 +258,8 @@ module('DOM Helper: fillIn', function (hooks) {
await setupContext(context);
await fillIn(`#${element.id}`, '');

assert.verifySteps(clickSteps);
// For this specific case, Firefox does not emit `selectionchange`.
assert.verifySteps(clickSteps.filter((s) => s !== 'selectionchange'));
assert.strictEqual(
document.activeElement,
element,
Expand Down
31 changes: 27 additions & 4 deletions tests/unit/dom/type-in-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
_registerHook,
} from '@ember/test-helpers';
import { buildInstrumentedElement, insertElement } from '../../helpers/events';
import { isIE11 } from '../../helpers/browser-detect';
import { isIE11, isFirefox } from '../../helpers/browser-detect';
import { debounce } from '@ember/runloop';
import { Promise } from 'rsvp';
import hasEmberVersion from '@ember/test-helpers/has-ember-version';
Expand Down Expand Up @@ -49,6 +49,27 @@ if (isIE11) {
'change',
'focus',
];
} else if (isFirefox) {
expectedEvents = [
'focus',
'focusin',
'keydown',
'keypress',
'input',
'keyup',
'selectionchange',
'keydown',
'keypress',
'input',
'keyup',
'selectionchange',
'keydown',
'keypress',
'input',
'keyup',
'change',
'selectionchange',
];
}

module('DOM Helper: typeIn', function (hooks) {
Expand Down Expand Up @@ -164,7 +185,9 @@ module('DOM Helper: typeIn', function (hooks) {
element.setAttribute('contenteditable', 'true');
await typeIn(element, 'foo');

assert.verifySteps(expectedEvents);
// For this specific case, Firefox does not emit `selectionchange`.
assert.verifySteps(expectedEvents.filter((s) => s !== 'selectionchange'));

assert.strictEqual(
document.activeElement,
element,
Expand Down Expand Up @@ -302,7 +325,7 @@ module('DOM Helper: typeIn', function (hooks) {
await assert.rejects(
typeIn(element, tooLongString).finally(() => {
// should throw before the second input event (or second keyup for IE)
const expectedNumberOfSteps = isIE11 ? 6 : 8;
const expectedNumberOfSteps = isIE11 ? 6 : isFirefox ? 9 : 8;
assert.verifySteps(expectedEvents.slice(0, expectedNumberOfSteps));
}),
new Error("Can not `typeIn` with text: 'fo' that exceeds maxlength: '1'.")
Expand Down Expand Up @@ -355,7 +378,7 @@ module('DOM Helper: typeIn', function (hooks) {
await assert.rejects(
typeIn(element, tooLongString).finally(() => {
// should throw before the second input event (or second keyup for IE)
const expectedNumberOfSteps = isIE11 ? 6 : 8;
const expectedNumberOfSteps = isIE11 ? 6 : isFirefox ? 9 : 8;
assert.verifySteps(expectedEvents.slice(0, expectedNumberOfSteps));
}),
new Error("Can not `typeIn` with text: 'fo' that exceeds maxlength: '1'.")
Expand Down