UNPKG

cli-testing-library

Version:

Simple and complete CLI testing utilities that encourage good testing practices.

55 lines (54 loc) 1.67 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const matches = require("./matches.cjs"); const normalize = matches.getDefaultNormalizer(); function escapeRegExp(string) { return string.replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&"); } function getRegExpMatcher(string) { return new RegExp(escapeRegExp(string.toLowerCase()), "i"); } function makeSuggestion(queryName, _instance, content, { variant, name }) { const warning = ""; const queryOptions = {}; const queryArgs = [ [].includes(queryName) ? content : getRegExpMatcher(content) ]; if (name) { queryOptions.name = getRegExpMatcher(name); } if (Object.keys(queryOptions).length > 0) { queryArgs.push(queryOptions); } const queryMethod = `${variant}By${queryName}`; return { queryName, queryMethod, queryArgs, variant, warning, toString() { const [text, options] = queryArgs; const newText = typeof text === "string" ? `'${text}'` : text; const newOptions = options ? `, { ${Object.entries(options).map(([k, v]) => `${k}: ${v}`).join(", ")} }` : ""; return `${queryMethod}(${newText}${newOptions})`; } }; } function canSuggest(currentMethod, requestedMethod, data) { return data && true; } function getSuggestedQuery(instance, variant = "get", method) { const textContent = normalize( instance.stdoutArr.map((obj) => obj.contents).join("\n") ); if (canSuggest("Text", method, textContent)) { return makeSuggestion("Text", instance, textContent, { variant }); } return void 0; } exports.getSuggestedQuery = getSuggestedQuery; //# sourceMappingURL=suggestions.cjs.map