@testing-library/cypress
Version:
Simple and complete custom Cypress commands and utilities that encourage good testing practices.
110 lines (107 loc) • 3.69 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.commands = void 0;
exports.configure = configure;
var _dom = require("@testing-library/dom");
var _utils = require("./utils");
function configure({
fallbackRetryWithoutPreviousSubject,
...config
}) {
return (0, _dom.configure)(config);
}
const findRegex = /^find/;
const queryNames = Object.keys(_dom.queries).filter(q => findRegex.test(q));
const commands = exports.commands = queryNames.map(queryName => {
return createQuery(queryName, queryName.replace(findRegex, 'get'));
});
function createQuery(queryName, implementationName) {
return {
name: queryName,
command(...args) {
var _this$get;
const lastArg = args[args.length - 1];
const options = typeof lastArg === 'object' ? {
...lastArg
} : {};
this.set('timeout', options.timeout);
const queryImpl = _dom.queries[implementationName];
const inputArr = args.filter(filterInputs);
const selector = `${queryName}(${queryArgument(args)})`;
const consoleProps = {
// TODO: Would be good to completely separate out the types of input into their own properties
input: inputArr,
Selector: selector
};
const log = options.log !== false && Cypress.log({
name: queryName,
type: ((_this$get = this.get('prev')) == null ? void 0 : _this$get.get('chainerId')) === this.get('chainerId') ? 'child' : 'parent',
message: inputArr,
timeout: options.timeout,
consoleProps: () => consoleProps
});
const withinSubject = cy.state('withinSubjectChain');
let error;
this.set('onFail', err => {
if (error) {
err.message = error.message;
}
});
return subject => {
const container = (0, _utils.getFirstElement)(options.container || subject || cy.getSubjectFromChain(withinSubject) || cy.state('window').document);
consoleProps['Applied To'] = container;
let value;
try {
value = queryImpl(container, ...args);
} catch (e) {
error = e;
value = Cypress.$();
value.selector = selector;
}
const result = Cypress.$(value);
if (value && log) {
log.set('$el', result);
}
// Overriding the selector of the jquery object because it's displayed in the long message of .should('exist') failure message
// Hopefully it makes it clearer, because I find the normal response of "Expected to find element '', but never found it" confusing
result.selector = selector;
consoleProps.elements = result.length;
if (result.length === 1) {
consoleProps.yielded = result.toArray()[0];
} else if (result.length > 0) {
consoleProps.yielded = result.toArray();
}
if (result.length > 1 && !/All/.test(queryName)) {
// Is this useful?
throw Error(`Found multiple elements with the text: ${queryArgument(args)}`);
}
return result;
};
}
};
}
function filterInputs(value) {
if (Array.isArray(value) && value.length === 0) {
return false;
}
if (value instanceof RegExp) {
return value.toString();
}
if (typeof value === 'object' && Object.keys(value).length === 0) {
return false;
}
return Boolean(value);
}
function queryArgument(args) {
const input = args.find(value => {
return value instanceof RegExp || typeof value === 'string';
});
if (input && typeof input === 'string') {
return `\`${input}\``;
}
return input;
}
/* eslint no-new-func:0, complexity:0 */
/* globals Cypress, cy */
;