-
Notifications
You must be signed in to change notification settings - Fork 153
/
index.js
134 lines (108 loc) · 3.53 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import {configure as configureDTL, queries} from '@testing-library/dom'
import {getFirstElement} from './utils'
function configure({fallbackRetryWithoutPreviousSubject, ...config}) {
return configureDTL(config)
}
const findRegex = /^find/
const queryNames = Object.keys(queries).filter(q => findRegex.test(q))
const commands = queryNames.map(queryName => {
return createQuery(queryName, queryName.replace(findRegex, 'get'))
})
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 = 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,
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 = 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
}
export {commands, configure}
/* eslint no-new-func:0, complexity:0 */
/* globals Cypress, cy */