1
+ "use strict" ;
2
+
3
+ Object . defineProperty ( exports , "__esModule" , {
4
+ value : true
5
+ } ) ;
6
+ exports . commands = void 0 ;
7
+ exports . configure = configure ;
8
+ var _dom = require ( "@testing-library/dom" ) ;
9
+ var _utils = require ( "./utils" ) ;
10
+ function configure ( {
11
+ fallbackRetryWithoutPreviousSubject,
12
+ ...config
13
+ } ) {
14
+ return ( 0 , _dom . configure ) ( config ) ;
15
+ }
16
+ const findRegex = / ^ f i n d / ;
17
+ const queryNames = Object . keys ( _dom . queries ) . filter ( q => findRegex . test ( q ) ) ;
18
+ const commands = queryNames . map ( queryName => {
19
+ return createQuery ( queryName , queryName . replace ( findRegex , 'get' ) ) ;
20
+ } ) ;
21
+ exports . commands = commands ;
22
+ function createQuery ( queryName , implementationName ) {
23
+ return {
24
+ name : queryName ,
25
+ command ( ...args ) {
26
+ const lastArg = args [ args . length - 1 ] ;
27
+ const options = typeof lastArg === 'object' ? {
28
+ ...lastArg
29
+ } : { } ;
30
+ this . set ( 'timeout' , options . timeout ) ;
31
+ const queryImpl = _dom . queries [ implementationName ] ;
32
+ const inputArr = args . filter ( filterInputs ) ;
33
+ const selector = `${ queryName } (${ queryArgument ( args ) } )` ;
34
+ const consoleProps = {
35
+ // TODO: Would be good to completely separate out the types of input into their own properties
36
+ input : inputArr ,
37
+ Selector : selector
38
+ } ;
39
+ const log = options . log !== false && Cypress . log ( {
40
+ name : queryName ,
41
+ type : this . get ( 'prev' ) . get ( 'chainerId' ) === this . get ( 'chainerId' ) ? 'child' : 'parent' ,
42
+ message : inputArr ,
43
+ consoleProps : ( ) => consoleProps
44
+ } ) ;
45
+ const withinSubject = cy . state ( 'withinSubjectChain' ) ;
46
+ let error ;
47
+ this . set ( 'onFail' , err => {
48
+ if ( error ) {
49
+ err . message = error . message ;
50
+ }
51
+ } ) ;
52
+ return subject => {
53
+ const container = ( 0 , _utils . getFirstElement ) ( options . container || subject || cy . getSubjectFromChain ( withinSubject ) || cy . state ( 'window' ) . document ) ;
54
+ consoleProps [ 'Applied To' ] = container ;
55
+ let value ;
56
+ try {
57
+ value = queryImpl ( container , ...args ) ;
58
+ } catch ( e ) {
59
+ error = e ;
60
+ value = Cypress . $ ( ) ;
61
+ value . selector = selector ;
62
+ }
63
+ const result = Cypress . $ ( value ) ;
64
+ if ( value && log ) {
65
+ log . set ( '$el' , result ) ;
66
+ }
67
+
68
+ // Overriding the selector of the jquery object because it's displayed in the long message of .should('exist') failure message
69
+ // Hopefully it makes it clearer, because I find the normal response of "Expected to find element '', but never found it" confusing
70
+ result . selector = selector ;
71
+ consoleProps . elements = result . length ;
72
+ if ( result . length === 1 ) {
73
+ consoleProps . yielded = result . toArray ( ) [ 0 ] ;
74
+ } else if ( result . length > 0 ) {
75
+ consoleProps . yielded = result . toArray ( ) ;
76
+ }
77
+ if ( result . length > 1 && ! / A l l / . test ( queryName ) ) {
78
+ // Is this useful?
79
+ throw Error ( `Found multiple elements with the text: ${ queryArgument ( args ) } ` ) ;
80
+ }
81
+ return result ;
82
+ } ;
83
+ }
84
+ } ;
85
+ }
86
+ function filterInputs ( value ) {
87
+ if ( Array . isArray ( value ) && value . length === 0 ) {
88
+ return false ;
89
+ }
90
+ if ( value instanceof RegExp ) {
91
+ return value . toString ( ) ;
92
+ }
93
+ if ( typeof value === 'object' && Object . keys ( value ) . length === 0 ) {
94
+ return false ;
95
+ }
96
+ return Boolean ( value ) ;
97
+ }
98
+ function queryArgument ( args ) {
99
+ const input = args . find ( value => {
100
+ return value instanceof RegExp || typeof value === 'string' ;
101
+ } ) ;
102
+ if ( input && typeof input === 'string' ) {
103
+ return `\`${ input } \`` ;
104
+ }
105
+ return input ;
106
+ }
107
+
108
+ /* eslint no-new-func:0, complexity:0 */
109
+ /* globals Cypress, cy */
0 commit comments