cli-testing-library
Version:
Simple and complete CLI testing utilities that encourage good testing practices.
74 lines (73 loc) • 2.41 kB
JavaScript
import stripAnsiFn from "strip-ansi";
function assertNotNullOrUndefined(matcher) {
if (matcher === null || matcher === void 0) {
throw new Error(
`It looks like ${matcher} was passed instead of a matcher. Did you do something like getByText(${matcher})?`
);
}
}
function fuzzyMatches(textToMatch, node, matcher, normalizer) {
if (typeof textToMatch !== "string") {
return false;
}
assertNotNullOrUndefined(matcher);
const normalizedText = normalizer(textToMatch);
if (typeof matcher === "string" || typeof matcher === "number") {
return normalizedText.toLowerCase().includes(matcher.toString().toLowerCase());
} else if (typeof matcher === "function") {
return matcher(normalizedText, node);
} else {
return matcher.test(normalizedText);
}
}
function matches(textToMatch, node, matcher, normalizer) {
if (typeof textToMatch !== "string") {
return false;
}
assertNotNullOrUndefined(matcher);
const normalizedText = normalizer(textToMatch);
if (matcher instanceof Function) {
return matcher(normalizedText, node);
} else if (matcher instanceof RegExp) {
return matcher.test(normalizedText);
} else {
return normalizedText === String(matcher);
}
}
function getDefaultNormalizer({
trim = true,
collapseWhitespace = true,
stripAnsi = true
} = {}) {
return (text) => {
let normalizedText = text;
normalizedText = trim ? normalizedText.trim() : normalizedText;
normalizedText = collapseWhitespace ? normalizedText.replace(/\s+/g, " ") : normalizedText;
normalizedText = stripAnsi ? stripAnsiFn(normalizedText) : normalizedText;
return normalizedText;
};
}
function makeNormalizer({
trim,
stripAnsi,
collapseWhitespace,
normalizer
}) {
if (normalizer) {
if (typeof trim !== "undefined" || typeof collapseWhitespace !== "undefined" || typeof stripAnsi !== "undefined") {
throw new Error(
'trim and collapseWhitespace are not supported with a normalizer. If you want to use the default trim and collapseWhitespace logic in your normalizer, use "getDefaultNormalizer({trim, collapseWhitespace})" and compose that into your normalizer'
);
}
return normalizer;
} else {
return getDefaultNormalizer({ trim, collapseWhitespace, stripAnsi });
}
}
export {
fuzzyMatches,
getDefaultNormalizer,
makeNormalizer,
matches
};
//# sourceMappingURL=matches.js.map