@@ -7,86 +7,94 @@ const getDefaultCommandOptions = () => {
7
7
}
8
8
}
9
9
10
- const queryNames = Object . keys ( queries ) ;
10
+ const queryNames = Object . keys ( queries )
11
11
12
- const getRegex = / ^ g e t / ;
13
- const queryRegex = / ^ q u e r y / ;
14
- const findRegex = / ^ f i n d / ;
12
+ const getRegex = / ^ g e t /
13
+ const queryRegex = / ^ q u e r y /
14
+ const findRegex = / ^ f i n d /
15
15
16
- const getQueryNames = queryNames . filter ( q => getRegex . test ( q ) ) ;
17
- const queryQueryNames = queryNames . filter ( q => queryRegex . test ( q ) ) ;
18
- const findQueryNames = queryNames . filter ( q => findRegex . test ( q ) ) ;
16
+ const getQueryNames = queryNames . filter ( q => getRegex . test ( q ) )
17
+ const queryQueryNames = queryNames . filter ( q => queryRegex . test ( q ) )
18
+ const findQueryNames = queryNames . filter ( q => findRegex . test ( q ) )
19
19
20
20
const getCommands = getQueryNames . map ( queryName => {
21
21
return {
22
22
name : queryName ,
23
23
command : ( ) => {
24
24
Cypress . log ( {
25
- name : queryName
26
- } ) ;
27
-
28
- throw new Error ( `You used '${ queryName } ' which has been removed from Cypress Testing Library because it does not make sense in this context. Please use '${ queryName . replace ( getRegex , 'find' ) } ' instead.` )
29
- }
25
+ name : queryName ,
26
+ } )
27
+
28
+ throw new Error (
29
+ `You used '${ queryName } ' which has been removed from Cypress Testing Library because it does not make sense in this context. Please use '${ queryName . replace (
30
+ getRegex ,
31
+ 'find' ,
32
+ ) } ' instead.`,
33
+ )
34
+ } ,
30
35
}
31
36
} )
32
37
33
38
const queryCommands = queryQueryNames . map ( queryName => {
34
- return createCommand ( queryName , queryName ) ;
39
+ return createCommand ( queryName , queryName )
35
40
} )
36
41
37
42
const findCommands = findQueryNames . map ( queryName => {
38
43
// dom-testing-library find* queries use a promise to look for an element, but that doesn't work well with Cypress retryability
39
44
// Use the query* commands so that we can lean on Cypress to do the retry for us
40
45
// When it does return a null or empty array, Cypress will retry until the assertions are satisfied or the command times out
41
- return createCommand ( queryName , queryName . replace ( findRegex , 'query' ) ) ;
46
+ return createCommand ( queryName , queryName . replace ( findRegex , 'query' ) )
42
47
} )
43
48
44
49
function createCommand ( queryName , implementationName ) {
45
50
return {
46
51
name : queryName ,
47
- command : ( ...args ) => {
52
+ options : { prevSubject : [ 'optional' , 'document' , 'element' , 'window' ] } ,
53
+ command : ( prevSubject , ...args ) => {
48
54
const lastArg = args [ args . length - 1 ]
49
55
const defaults = getDefaultCommandOptions ( )
50
56
const waitOptions =
51
57
typeof lastArg === 'object' ? { ...defaults , ...lastArg } : defaults
52
58
53
59
const queryImpl = queries [ implementationName ]
54
60
const baseCommandImpl = doc => {
55
- const container = getContainer ( waitOptions . container || doc )
61
+ const container = getContainer (
62
+ waitOptions . container || prevSubject || doc ,
63
+ )
56
64
return queryImpl ( container , ...args )
57
65
}
58
66
const commandImpl = doc => baseCommandImpl ( doc )
59
67
60
- const inputArr = args . filter ( filterInputs ) ;
68
+ const inputArr = args . filter ( filterInputs )
61
69
62
70
const consoleProps = {
63
71
// TODO: Would be good to completely separate out the types of input into their own properties
64
- input : inputArr
72
+ input : inputArr ,
65
73
}
66
74
67
75
Cypress . log ( {
68
76
$el : inputArr ,
69
77
name : queryName ,
70
78
message : inputArr ,
71
- consoleProps : ( ) => consoleProps
72
- } ) ;
79
+ consoleProps : ( ) => consoleProps ,
80
+ } )
73
81
74
82
return cy
75
83
. window ( { log : false } )
76
- . then ( { timeout : waitOptions . timeout + 100 } , ( thenArgs ) => {
84
+ . then ( { timeout : waitOptions . timeout + 100 } , thenArgs => {
77
85
const getValue = ( ) => {
78
- const value = commandImpl ( thenArgs . document ) ;
79
- const result = Cypress . $ ( value ) ;
80
-
86
+ const value = commandImpl ( thenArgs . document )
87
+ const result = Cypress . $ ( value )
88
+
81
89
// Overriding the selector of the jquery object because it's displayed in the long message of .should('exist') failure message
82
90
// Hopefully it makes it clearer, because I find the normal response of "Expected to find element '', but never found it" confusing
83
- result . selector = `${ queryName } (${ queryArgument ( args ) } )` ;
91
+ result . selector = `${ queryName } (${ queryArgument ( args ) } )`
84
92
85
93
if ( result . length > 0 ) {
86
94
consoleProps . yielded = result . toArray ( )
87
95
}
88
96
89
- return result ;
97
+ return result
90
98
}
91
99
92
100
const resolveValue = ( ) => {
@@ -100,19 +108,17 @@ function createCommand(queryName, implementationName) {
100
108
101
109
if ( queryRegex . test ( queryName ) ) {
102
110
// For get* queries, do not retry
103
- return getValue ( ) ;
111
+ return getValue ( )
104
112
}
105
113
106
- return resolveValue ( )
107
- . then ( subject => {
108
-
109
- // Remove the error that occurred because it is irrelevant now
110
- if ( consoleProps . error ) {
111
- delete consoleProps . error ;
112
- }
113
-
114
- return subject ;
115
- } )
114
+ return resolveValue ( ) . then ( subject => {
115
+ // Remove the error that occurred because it is irrelevant now
116
+ if ( consoleProps . error ) {
117
+ delete consoleProps . error
118
+ }
119
+
120
+ return subject
121
+ } )
116
122
} )
117
123
} ,
118
124
}
@@ -125,33 +131,25 @@ function filterInputs(value) {
125
131
if ( value instanceof RegExp ) {
126
132
return value . toString ( )
127
133
}
128
- if (
129
- typeof value === 'object' &&
130
- Object . keys ( value ) . length === 0
131
- ) {
134
+ if ( typeof value === 'object' && Object . keys ( value ) . length === 0 ) {
132
135
return false
133
136
}
134
137
return Boolean ( value )
135
138
}
136
139
137
140
function queryArgument ( args ) {
138
- const input = args
139
- . find ( value => {
140
- return ( value instanceof RegExp ) || ( typeof value === 'string' )
141
- } ) ;
141
+ const input = args . find ( value => {
142
+ return value instanceof RegExp || typeof value === 'string'
143
+ } )
142
144
143
- if ( input && typeof input === 'string' ) {
144
- return `\`${ input } \`` ;
145
- }
145
+ if ( input && typeof input === 'string' ) {
146
+ return `\`${ input } \``
147
+ }
146
148
147
- return input ;
149
+ return input
148
150
}
149
151
150
- const commands = [
151
- ...getCommands ,
152
- ...findCommands ,
153
- ...queryCommands
154
- ] ;
152
+ const commands = [ ...getCommands , ...findCommands , ...queryCommands ]
155
153
156
154
export { commands , configure }
157
155
0 commit comments