Skip to content

Commit

Permalink
Add /dist temprorarily to get Cypress tests running while pointing to…
Browse files Browse the repository at this point in the history
… this branch
  • Loading branch information
BlueWinds committed Oct 27, 2022
1 parent c78ccf9 commit 119054b
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 1 deletion.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
node_modules
coverage
dist
.DS_Store

# these cause more harm than good
Expand Down
14 changes: 14 additions & 0 deletions dist/add-commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use strict";

var _ = require("./");
_.commands.forEach(({
name,
command
}) => {
Cypress.Commands.addQuery(name, command);
});
Cypress.Commands.add('configureCypressTestingLibrary', config => {
(0, _.configure)(config);
});

/* global Cypress */
109 changes: 109 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
"use strict";

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 = queryNames.map(queryName => {
return createQuery(queryName, queryName.replace(findRegex, 'get'));
});
exports.commands = commands;
function createQuery(queryName, implementationName) {
return {
name: queryName,
command(...args) {
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('prev').get('chainerId') === this.get('chainerId') ? 'child' : 'parent',
message: inputArr,
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 */
14 changes: 14 additions & 0 deletions dist/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getFirstElement = getFirstElement;
function getFirstElement(jqueryOrElement) {
if (Cypress.dom.isJquery(jqueryOrElement)) {
return jqueryOrElement.get(0);
}
return jqueryOrElement;
}

/* globals Cypress, cy */

0 comments on commit 119054b

Please sign in to comment.