Skip to content

Commit

Permalink
added maxlength logic to fillIn helper
Browse files Browse the repository at this point in the history
  • Loading branch information
jaydgruber committed Dec 5, 2019
1 parent faea7fa commit ae4aab2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
10 changes: 7 additions & 3 deletions addon-test-support/@ember/test-helpers/dom/fill-in.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,17 @@ export default function fillIn(target: Target, text: string): Promise<void> {
}

__focus__(element);

if (isControl) {
element.value = text;
// ref: https://html.spec.whatwg.org/multipage/input.html#concept-input-apply
const maxlengthApplicableTypes = ['text', 'search', 'url', 'tel', 'email', 'password'];
const { type, tagName } = element;
const maxlength = element.getAttribute('maxlength');
const shouldValidateMaxlength =
maxlength && (maxlengthApplicableTypes.includes(type) || tagName === 'TEXTAREA');
element.value = shouldValidateMaxlength ? text.slice(0, maxlength) : text;
} else {
element.innerHTML = text;
}

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

Expand Down
21 changes: 19 additions & 2 deletions tests/unit/dom/fill-in-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,32 @@ module('DOM Helper: fillIn', function(hooks) {
assert.equal(element.value, '');
});

test('filling an input with a maxlength', async function(assert) {
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);

await fillIn(`#${element.id}`, tooLongString);
await fillIn(element, tooLongString);

assert.verifySteps(clickSteps);
assert.equal(
Expand Down

0 comments on commit ae4aab2

Please sign in to comment.