@ember/test-helpers
Version:
Helpers for testing Ember.js applications
29 lines (25 loc) • 1.3 kB
JavaScript
// ref: https://html.spec.whatwg.org/multipage/input.html#concept-input-apply
const constrainedInputTypes = ['text', 'search', 'url', 'tel', 'email', 'password'];
/**
@private
@param {Element} element - the element to check
@returns {boolean} `true` when the element should constrain input by the maxlength attribute, `false` otherwise
*/
function isMaxLengthConstrained(element) {
return !!Number(element.getAttribute('maxlength')) && (element instanceof HTMLTextAreaElement || element instanceof HTMLInputElement && constrainedInputTypes.indexOf(element.type) > -1);
}
/**
* @private
* @param {Element} element - the element to check
* @param {string} text - the text being added to element
* @param {string} testHelper - the test helper context the guard is called from (for Error message)
* @throws if `element` has `maxlength` & `value` exceeds `maxlength`
*/
function guardForMaxlength(element, text, testHelper) {
const maxlength = element.getAttribute('maxlength');
if (isMaxLengthConstrained(element) && maxlength && text && text.length > Number(maxlength)) {
throw new Error(`Can not \`${testHelper}\` with text: '${text}' that exceeds maxlength: '${maxlength}'.`);
}
}
export { guardForMaxlength as default };
//# sourceMappingURL=-guard-for-maxlength.js.map