Skip to content

Commit dfead6f

Browse files
authored
Merge pull request #946 from drewlee/master
Fixes maxlength conditional check for textarea & constrained input types
2 parents f966c34 + 3eb1065 commit dfead6f

File tree

3 files changed

+104
-2
lines changed

3 files changed

+104
-2
lines changed

addon-test-support/@ember/test-helpers/dom/-guard-for-maxlength.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ function isMaxLengthConstrained(
1313
): element is HTMLInputElement | HTMLTextAreaElement {
1414
return (
1515
!!Number(element.getAttribute('maxLength')) &&
16-
(element instanceof HTMLInputElement ||
17-
(element instanceof HTMLTextAreaElement && constrainedInputTypes.indexOf(element.type) > -1))
16+
(element instanceof HTMLTextAreaElement ||
17+
(element instanceof HTMLInputElement && constrainedInputTypes.indexOf(element.type) > -1))
1818
);
1919
}
2020

tests/unit/dom/fill-in-test.js

+49
Original file line numberDiff line numberDiff line change
@@ -254,4 +254,53 @@ module('DOM Helper: fillIn', function (hooks) {
254254
new Error("Can not `fillIn` with text: 'foo' that exceeds maxlength: '1'.")
255255
);
256256
});
257+
258+
test('filling in a non-constrained input type with maxlength', async function (assert) {
259+
element = buildInstrumentedElement('input');
260+
const maxLengthString = '1';
261+
const tooLongString = maxLengthString.concat('23');
262+
element.setAttribute('type', 'number');
263+
element.setAttribute('maxlength', maxLengthString.length);
264+
await setupContext(context);
265+
266+
await fillIn(element, tooLongString);
267+
268+
assert.verifySteps(clickSteps);
269+
assert.equal(
270+
element.value,
271+
tooLongString,
272+
'fillIn does not reject non-constrained input types'
273+
);
274+
});
275+
276+
test('filling a textarea with a maxlength with suitable value', async function (assert) {
277+
element = buildInstrumentedElement('textarea');
278+
const maxLengthString = 'f';
279+
element.setAttribute('maxlength', maxLengthString.length);
280+
281+
await setupContext(context);
282+
283+
await fillIn(element, maxLengthString);
284+
285+
assert.verifySteps(clickSteps);
286+
assert.equal(
287+
element.value,
288+
maxLengthString,
289+
`fillIn respects textarea attribute [maxlength=${maxLengthString.length}]`
290+
);
291+
});
292+
293+
test('filling a textarea with a maxlength with too long value', async function (assert) {
294+
element = buildInstrumentedElement('textarea');
295+
const maxLengthString = 'f';
296+
const tooLongString = maxLengthString.concat('oo');
297+
element.setAttribute('maxlength', maxLengthString.length);
298+
299+
await setupContext(context);
300+
301+
assert.rejects(
302+
fillIn(element, tooLongString),
303+
new Error("Can not `fillIn` with text: 'foo' that exceeds maxlength: '1'.")
304+
);
305+
});
257306
});

tests/unit/dom/type-in-test.js

+53
Original file line numberDiff line numberDiff line change
@@ -280,4 +280,57 @@ module('DOM Helper: typeIn', function (hooks) {
280280
new Error("Can not `typeIn` with text: 'fo' that exceeds maxlength: '1'.")
281281
);
282282
});
283+
284+
test('typing in a non-constrained input type with maxlength', async function (assert) {
285+
element = buildInstrumentedElement('input');
286+
const maxLengthString = '1';
287+
const tooLongString = maxLengthString.concat('23');
288+
element.setAttribute('type', 'number');
289+
element.setAttribute('maxlength', maxLengthString.length);
290+
await setupContext(context);
291+
292+
await typeIn(element, tooLongString);
293+
294+
assert.verifySteps(expectedEvents);
295+
assert.equal(
296+
element.value,
297+
tooLongString,
298+
'typeIn does not reject non-constrained input types'
299+
);
300+
});
301+
302+
test('typing in a textarea with a maxlength with suitable value', async function (assert) {
303+
element = buildInstrumentedElement('textarea');
304+
const maxLengthString = 'foo';
305+
element.setAttribute('maxlength', maxLengthString.length);
306+
307+
await setupContext(context);
308+
309+
await typeIn(element, maxLengthString);
310+
311+
assert.verifySteps(expectedEvents);
312+
assert.equal(
313+
element.value,
314+
maxLengthString,
315+
`typeIn respects textarea attribute [maxlength=${maxLengthString.length}]`
316+
);
317+
});
318+
319+
test('typing in a textarea with a maxlength with too long value', async function (assert) {
320+
element = buildInstrumentedElement('textarea');
321+
const maxLengthString = 'f';
322+
const tooLongString = maxLengthString.concat('o');
323+
element.setAttribute('maxlength', maxLengthString.length);
324+
325+
await setupContext(context);
326+
327+
await assert.rejects(
328+
typeIn(element, tooLongString).finally(() => {
329+
// should throw before the second input event (or second keyup for IE)
330+
const expectedNumberOfSteps = isIE11 ? 6 : 8;
331+
assert.verifySteps(expectedEvents.slice(0, expectedNumberOfSteps));
332+
}),
333+
new Error("Can not `typeIn` with text: 'fo' that exceeds maxlength: '1'.")
334+
);
335+
});
283336
});

0 commit comments

Comments
 (0)