detox-allure2-adapter
Version:
Detox adapter for jest-allure2-reporter
122 lines • 3.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatPredicate = void 0;
const utils_1 = require("../../utils");
function formatPredicate(predicate, prefix = '') {
const result = _formatPredicate(predicate, prefix, false);
result.message = (0, utils_1.truncate)(result.message);
return result;
}
exports.formatPredicate = formatPredicate;
function _formatPredicate(predicate, prefix, prependAND) {
if (!predicate) {
return (0, utils_1.msg)('?');
}
switch (predicate?.type) {
case 'and': {
return formatCompoundPredicate(predicate, prefix);
}
case 'descendant': {
return formatDescendantPredicate(predicate, prefix);
}
case 'ancestor': {
return formatAncestorPredicate(predicate, prefix);
}
default: {
const result = formatBasePredicate(predicate, prefix);
return prependAND ? (0, utils_1.concat)('&&', result) : result;
}
}
}
function formatCompoundPredicate(predicate, prefix = '') {
const { predicates = [] } = predicate;
if (!Array.isArray(predicates)) {
return (0, utils_1.msg)('?');
}
const result = predicates
.map((p, index) => _formatPredicate(p, prefix, index > 0))
.reduce((a, b) => (a ? (0, utils_1.concat)(a, b) : b), null) ??
(0, utils_1.msg)('?');
result.message = `(${result.message})`;
return result;
}
function formatDescendantPredicate(predicate, prefix = '') {
const { predicate: descendant } = predicate;
return (0, utils_1.concat)('containing', formatPredicate(descendant, join(prefix, 'descendant')));
}
function formatAncestorPredicate(predicate, prefix = '') {
const { predicate: ancestor } = predicate;
return (0, utils_1.concat)('inside', formatPredicate(ancestor, join(prefix, 'ancestor')));
}
function formatBasePredicate(predicate, prefix = '') {
const { atIndex, isRegex, type, value } = predicate;
const _ = (name) => join(prefix, name);
if (value == null || value === '') {
return (0, utils_1.msg)('(?)');
}
const index$ = atIndex == null ? '' : `[${atIndex}]`;
const args = { [_(type)]: value };
if (index$) {
args[_('index')] = atIndex;
}
const value$ = String(value);
if (isRegex) {
return formatRegexPredicate(type, value$, index$, args);
}
if (isTextPredicate(type)) {
return formatTextPredicate(value$, index$, args);
}
if (type === 'traits') {
return formatTraitsPredicate(value$, index$, args);
}
if (type === 'id') {
return formatIdPredicate(value$, index$, args);
}
if (type === 'type') {
return formatTypePredicate(value$, index$, args);
}
return formatDefaultPredicate(type, value$, index$, args);
}
function formatRegexPredicate(type, value, index, args) {
return {
message: `[${type}] ~ ${value}${index}`,
args,
};
}
function isTextPredicate(type) {
return type === 'label' || type === 'accessibilityLabel' || type === 'text';
}
function formatTextPredicate(value, index, args) {
return {
message: `"${value}"${index ? ' ' : ''}${index}`,
args,
};
}
function formatTraitsPredicate(value, index, args) {
return {
message: `[${value}]${index}`,
args,
};
}
function formatIdPredicate(value, index, args) {
return {
message: `#${value}${index}`,
args,
};
}
function formatTypePredicate(value, index, args) {
return {
message: `${value}${index}`,
args,
};
}
function formatDefaultPredicate(type, value, index, args) {
return {
message: `[${type}] = ${value}${index}`,
args,
};
}
function join(a, b) {
return a && b ? `${a}_${b}` : a || b || '';
}
//# sourceMappingURL=predicate-formatters.js.map