From 119054b5963b0d2e064b13c5cc6fc9db32c8b7b5 Mon Sep 17 00:00:00 2001 From: BlueWinds Date: Thu, 27 Oct 2022 14:56:43 -0700 Subject: [PATCH] Add /dist temprorarily to get Cypress tests running while pointing to this branch --- .gitignore | 1 - dist/add-commands.js | 14 ++++++ dist/index.js | 109 +++++++++++++++++++++++++++++++++++++++++++ dist/utils.js | 14 ++++++ 4 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 dist/add-commands.js create mode 100644 dist/index.js create mode 100644 dist/utils.js diff --git a/.gitignore b/.gitignore index dd6ebe2..20f4331 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ node_modules coverage -dist .DS_Store # these cause more harm than good diff --git a/dist/add-commands.js b/dist/add-commands.js new file mode 100644 index 0000000..cf7698c --- /dev/null +++ b/dist/add-commands.js @@ -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 */ \ No newline at end of file diff --git a/dist/index.js b/dist/index.js new file mode 100644 index 0000000..c06f3fc --- /dev/null +++ b/dist/index.js @@ -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 */ \ No newline at end of file diff --git a/dist/utils.js b/dist/utils.js new file mode 100644 index 0000000..fca0d7f --- /dev/null +++ b/dist/utils.js @@ -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 */ \ No newline at end of file