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

Error when fillIn/ typeIn attempt to enter a value longer than maxlength #747

Merged
merged 6 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
@private
@param {Element} element the element to check
@returns {boolean} `true` when the element should constrain input by the maxlength attribute, `false` otherwise
*/
export default function isMaxLengthConstrained(
element: Element
): element is HTMLInputElement | HTMLTextAreaElement {
// ref: https://html.spec.whatwg.org/multipage/input.html#concept-input-apply
const constrainedInputTypes = ['text', 'search', 'url', 'tel', 'email', 'password'];
jaydgruber marked this conversation as resolved.
Show resolved Hide resolved
return (
!!Number(element.getAttribute('maxLength')) &&
(element.tagName === 'TEXTAREA' ||
(element.tagName === 'INPUT' &&
constrainedInputTypes.indexOf((element as HTMLInputElement).type) > -1))
jaydgruber marked this conversation as resolved.
Show resolved Hide resolved
);
}
9 changes: 8 additions & 1 deletion addon-test-support/@ember/test-helpers/dom/fill-in.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import getElement from './-get-element';
import isFormControl from './-is-form-control';
import isMaxLengthConstrained from './-is-maxlength-constrained';
import { __focus__ } from './focus';
import settled from '../settled';
import fireEvent from './fire-event';
Expand Down Expand Up @@ -50,6 +51,13 @@ export default function fillIn(target: Target, text: string): Promise<void> {
throw new Error(`Can not \`fillIn\` readonly '${target}'.`);
}

const maxlength = element.getAttribute('maxlength');
if (isMaxLengthConstrained(element) && maxlength && text && text.length > Number(maxlength)) {
throw new Error(
`Can not \`fillIn\` with text: '${text}' that exceeds maxlength: '${maxlength}'.`
);
}

__focus__(element);

element.value = text;
Expand All @@ -60,7 +68,6 @@ export default function fillIn(target: Target, text: string): Promise<void> {
} else {
throw new Error('`fillIn` is only usable on form controls or contenteditable elements.');
}

fireEvent(element, 'input');
fireEvent(element, 'change');

Expand Down
16 changes: 15 additions & 1 deletion addon-test-support/@ember/test-helpers/dom/type-in.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import isFormControl, { FormControl } from './-is-form-control';
import { __focus__ } from './focus';
import { Promise } from 'rsvp';
import fireEvent from './fire-event';
import isMaxLengthConstrained from './-is-maxlength-constrained';
import Target from './-target';
import { __triggerKeyEvent__ } from './trigger-key-event';
import { log } from '@ember/test-helpers/dom/-logging';
Expand Down Expand Up @@ -94,7 +95,20 @@ function keyEntry(element: FormControl, character: string): () => void {
.then(() => __triggerKeyEvent__(element, 'keydown', characterKey, options))
.then(() => __triggerKeyEvent__(element, 'keypress', characterKey, options))
.then(() => {
element.value = element.value + character;
const newValue = element.value + character;
const maxlength = element.getAttribute('maxlength');
const shouldTruncate =
isMaxLengthConstrained(element) &&
maxlength &&
newValue &&
newValue.length > Number(maxlength);
if (shouldTruncate) {
jaydgruber marked this conversation as resolved.
Show resolved Hide resolved
throw new Error(
`Can not \`typeIn\` with text: '${newValue}' that exceeds maxlength: '${maxlength}'.`
);
}

element.value = newValue;
fireEvent(element, 'input');
})
.then(() => __triggerKeyEvent__(element, 'keyup', characterKey, options));
Expand Down
31 changes: 31 additions & 0 deletions tests/unit/dom/fill-in-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,35 @@ module('DOM Helper: fillIn', function (hooks) {
assert.strictEqual(document.activeElement, element, 'activeElement updated');
assert.equal(element.value, '');
});

test('filling an input with a maxlength with suitable value', async function (assert) {
element = buildInstrumentedElement('input');
const maxLengthString = 'f';
element.setAttribute('maxlength', maxLengthString.length);

await setupContext(context);

await fillIn(element, maxLengthString);

assert.verifySteps(clickSteps);
assert.equal(
element.value,
maxLengthString,
`fillIn respects input attribute [maxlength=${maxLengthString.length}]`
);
});

test('filling an input with a maxlength with too long value', async function (assert) {
element = buildInstrumentedElement('input');
const maxLengthString = 'f';
const tooLongString = maxLengthString.concat('oo');
element.setAttribute('maxlength', maxLengthString.length);

await setupContext(context);

assert.rejects(
fillIn(element, tooLongString),
new Error("Can not `fillIn` with text: 'foo' that exceeds maxlength: '1'.")
);
});
});
34 changes: 34 additions & 0 deletions tests/unit/dom/type-in-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,38 @@ module('DOM Helper: typeIn', function (hooks) {
assert.verifySteps(expectedEvents);
assert.equal(runcount, 1, 'debounced function only called once');
});

test('typing in an input with a maxlength with suitable value', async function (assert) {
element = buildInstrumentedElement('input');
const maxLengthString = 'foo';
element.setAttribute('maxlength', maxLengthString.length);

await setupContext(context);

await typeIn(element, maxLengthString);

assert.verifySteps(expectedEvents);
assert.equal(
element.value,
maxLengthString,
`typeIn respects input attribute [maxlength=${maxLengthString.length}]`
);
});

test('typing in an input with a maxlength with too long value', async function (assert) {
element = buildInstrumentedElement('input');
const maxLengthString = 'f';
const tooLongString = maxLengthString.concat('o');
element.setAttribute('maxlength', maxLengthString.length);

await setupContext(context);

await assert.rejects(
typeIn(element, tooLongString).finally(() => {
// should throw before the second input event
assert.verifySteps(expectedEvents.slice(0, 8));
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like for isIE11 it would expect to throw before the third keypress, that doesn't seem correct.

@rwjblue do we have IE tests broken?

Copy link
Member

Choose a reason for hiding this comment

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

Ya, it is possible, we don't have IE11 under CI test at the moment. We should look into hooking that up... 🤔

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 believe I corrected the assertion here, but I don't have an IE launcher for testem locally, so not sure the best way to confirm

}),
new Error("Can not `typeIn` with text: 'fo' that exceeds maxlength: '1'.")
);
});
});