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.

33 lines
1.3 KiB

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.toHaveTextContent = void 0;
const jest_matcher_utils_1 = require("jest-matcher-utils");
const utils_1 = require("./utils");
function collectNormalizedText(element) {
const childrenText = collectChildrenText(element).join('');
return (0, utils_1.normalize)(childrenText);
}
function collectChildrenText(element) {
if (typeof element === 'string')
return [element];
if (!element?.children)
return [];
const result = [];
element.children.forEach((child) => {
result.push(...collectChildrenText(child));
});
return result;
}
function toHaveTextContent(element, checkWith) {
(0, utils_1.checkReactElement)(element, toHaveTextContent, this);
const textContent = collectNormalizedText(element);
return {
pass: (0, utils_1.matches)(textContent, checkWith),
message: () => {
const to = this.isNot ? 'not to' : 'to';
return (0, utils_1.getMessage)((0, jest_matcher_utils_1.matcherHint)(`${this.isNot ? '.not' : ''}.toHaveTextContent`, 'element', ''), `Expected element ${to} have text content`, checkWith, 'Received', textContent);
},
};
}
exports.toHaveTextContent = toHaveTextContent;