You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

64 lines
2.4 KiB

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.toBeDisabled = toBeDisabled;
exports.toBeEnabled = toBeEnabled;
var _jestMatcherUtils = require("jest-matcher-utils");
var _utils = require("./utils");
// form elements that support 'disabled'
const FORM_TAGS = ['fieldset', 'input', 'select', 'optgroup', 'option', 'button', 'textarea'];
/*
* According to specification:
* If <fieldset> is disabled, the form controls that are its descendants,
* except descendants of its first optional <legend> element, are disabled
*
* https://html.spec.whatwg.org/multipage/form-elements.html#concept-fieldset-disabled
*
* This method tests whether element is first legend child of fieldset parent
*/
function isFirstLegendChildOfFieldset(element, parent) {
return (0, _utils.getTag)(element) === 'legend' && (0, _utils.getTag)(parent) === 'fieldset' && element.isSameNode(Array.from(parent.children).find(child => (0, _utils.getTag)(child) === 'legend'));
}
function isElementDisabledByParent(element, parent) {
return isElementDisabled(parent) && !isFirstLegendChildOfFieldset(element, parent);
}
function isElementDisabled(element) {
return FORM_TAGS.includes((0, _utils.getTag)(element)) && element.hasAttribute('disabled');
}
function isAncestorDisabled(element) {
const parent = element.parentElement;
return Boolean(parent) && (isElementDisabledByParent(element, parent) || isAncestorDisabled(parent));
}
function toBeDisabled(element) {
(0, _utils.checkHtmlElement)(element, toBeDisabled, this);
const isDisabled = isElementDisabled(element) || isAncestorDisabled(element);
return {
pass: isDisabled,
message: () => {
const is = isDisabled ? 'is' : 'is not';
return [(0, _jestMatcherUtils.matcherHint)(`${this.isNot ? '.not' : ''}.toBeDisabled`, 'element', ''), '', `Received element ${is} disabled:`, ` ${(0, _jestMatcherUtils.printReceived)(element.cloneNode(false))}`].join('\n');
}
};
}
function toBeEnabled(element) {
(0, _utils.checkHtmlElement)(element, toBeEnabled, this);
const isEnabled = !(isElementDisabled(element) || isAncestorDisabled(element));
return {
pass: isEnabled,
message: () => {
const is = isEnabled ? 'is' : 'is not';
return [(0, _jestMatcherUtils.matcherHint)(`${this.isNot ? '.not' : ''}.toBeEnabled`, 'element', ''), '', `Received element ${is} enabled:`, ` ${(0, _jestMatcherUtils.printReceived)(element.cloneNode(false))}`].join('\n');
}
};
}