forked from testing-library/cypress-testing-library
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add /dist temprorarily to get Cypress tests running while pointing to…
… this branch
- Loading branch information
Showing
4 changed files
with
137 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ |