@@ -12,133 +12,112 @@ const aliasDisplayName = (name) => {
12
12
return name . replace ( aliasDisplayRe , '' )
13
13
}
14
14
15
- const getXhrTypeByAlias = ( alias ) => {
16
- if ( requestXhrRe . test ( alias ) ) {
17
- return 'request'
18
- }
15
+ // eslint-disable-next-line @cypress/dev/arrow-body-multiline-braces
16
+ export const create = ( cy ) => ( {
17
+ addAlias ( ctx , aliasObj ) {
18
+ const { alias , subject } = aliasObj
19
19
20
- return 'response'
21
- }
22
-
23
- const validateAlias = ( alias : string ) => {
24
- if ( ! _ . isString ( alias ) ) {
25
- $errUtils . throwErrByPath ( 'as.invalid_type' )
26
- }
27
-
28
- if ( aliasDisplayRe . test ( alias ) ) {
29
- $errUtils . throwErrByPath ( 'as.invalid_first_token' , {
30
- args : {
31
- alias,
32
- suggestedName : alias . replace ( aliasDisplayRe , '' ) ,
33
- } ,
34
- } )
35
- }
36
-
37
- if ( _ . isEmpty ( alias ) ) {
38
- $errUtils . throwErrByPath ( 'as.empty_string' )
39
- }
40
-
41
- if ( reserved . includes ( alias ) ) {
42
- return $errUtils . throwErrByPath ( 'as.reserved_word' , { args : { alias } } )
43
- }
44
-
45
- return null
46
- }
20
+ const aliases = cy . state ( 'aliases' ) || { }
47
21
48
- export default {
49
- create : ( cy ) => {
50
- const addAlias = ( ctx , aliasObj ) => {
51
- const { alias, subject } = aliasObj
22
+ aliases [ alias ] = aliasObj
23
+ cy . state ( 'aliases' , aliases )
52
24
53
- const aliases = cy . state ( 'aliases' ) || { }
25
+ const remoteSubject = cy . getRemotejQueryInstance ( subject )
54
26
55
- aliases [ alias ] = aliasObj
56
- cy . state ( 'aliases' , aliases )
27
+ ctx [ alias ] = remoteSubject ?? subject
28
+ } ,
57
29
58
- const remoteSubject = cy . getRemotejQueryInstance ( subject )
30
+ getAlias ( name , cmd , log ) {
31
+ const aliases = cy . state ( 'aliases' ) || { }
59
32
60
- ctx [ alias ] = remoteSubject ?? subject
33
+ // bail if the name doesnt reference an alias
34
+ if ( ! aliasRe . test ( name ) ) {
35
+ return
61
36
}
62
37
63
- const getNextAlias = ( ) => {
64
- const next = cy . state ( 'current' ) . get ( 'next' )
38
+ const alias = aliases [ name . slice ( 1 ) ]
65
39
66
- if ( next && ( next . get ( 'name' ) === 'as' ) ) {
67
- return next . get ( 'args' ) [ 0 ]
68
- }
40
+ // slice off the '@'
41
+ if ( ! alias ) {
42
+ this . aliasNotFoundFor ( name , cmd , log )
69
43
}
70
44
71
- const getAlias = ( name , cmd , log ) => {
72
- const aliases = cy . state ( 'aliases' ) || { }
45
+ return alias
46
+ } ,
73
47
74
- // bail if the name doesnt reference an alias
75
- if ( ! aliasRe . test ( name ) ) {
76
- return
77
- }
48
+ // below are public because its expected other commands
49
+ // know about them and are expected to call them
78
50
79
- const alias = aliases [ name . slice ( 1 ) ]
51
+ getNextAlias ( ) {
52
+ const next = cy . state ( 'current' ) . get ( 'next' )
80
53
81
- // slice off the '@'
82
- if ( ! alias ) {
83
- aliasNotFoundFor ( name , cmd , log )
84
- }
54
+ if ( next && ( next . get ( 'name' ) === 'as' ) ) {
55
+ return next . get ( 'args' ) [ 0 ]
56
+ }
57
+ } ,
85
58
86
- return alias
59
+ validateAlias ( alias : string ) {
60
+ if ( ! _ . isString ( alias ) ) {
61
+ $errUtils . throwErrByPath ( 'as.invalid_type' )
87
62
}
88
63
89
- const getAvailableAliases = ( ) => {
90
- const aliases = cy . state ( 'aliases' )
64
+ if ( aliasDisplayRe . test ( alias ) ) {
65
+ $errUtils . throwErrByPath ( 'as.invalid_first_token' , {
66
+ args : {
67
+ alias,
68
+ suggestedName : alias . replace ( aliasDisplayRe , '' ) ,
69
+ } ,
70
+ } )
71
+ }
91
72
92
- if ( ! aliases ) {
93
- return [ ]
94
- }
73
+ if ( _ . isEmpty ( alias ) ) {
74
+ $errUtils . throwErrByPath ( 'as.empty_string' )
75
+ }
95
76
96
- return _ . keys ( aliases )
77
+ if ( reserved . includes ( alias ) ) {
78
+ $errUtils . throwErrByPath ( 'as.reserved_word' , { args : { alias } } )
97
79
}
98
80
99
- const aliasNotFoundFor = ( name , cmd , log ) => {
100
- let displayName
101
- const availableAliases = getAvailableAliases ( )
102
-
103
- // throw a very specific error if our alias isnt in the right
104
- // format, but its word is found in the availableAliases
105
- if ( ! aliasRe . test ( name ) && availableAliases . includes ( name ) ) {
106
- displayName = aliasDisplayName ( name )
107
- $errUtils . throwErrByPath ( 'alias.invalid' , {
108
- onFail : log ,
109
- args : { name, displayName } ,
110
- } )
111
- }
112
-
113
- cmd = cmd ?? ( ( log && log . get ( 'name' ) ) || cy . state ( 'current' ) . get ( 'name' ) )
114
- displayName = aliasDisplayName ( name )
81
+ return null
82
+ } ,
83
+
84
+ aliasNotFoundFor ( name , cmd , log ) {
85
+ const availableAliases = cy . state ( 'aliases' )
86
+ ? _ . keys ( cy . state ( 'aliases' ) )
87
+ : [ ]
115
88
116
- const errPath = availableAliases . length
117
- ? 'alias.not_registered_with_available'
118
- : 'alias.not_registered_without_available'
89
+ let displayName
119
90
120
- return $errUtils . throwErrByPath ( errPath , {
91
+ // throw a very specific error if our alias isnt in the right
92
+ // format, but its word is found in the availableAliases
93
+ if ( ! aliasRe . test ( name ) && availableAliases . includes ( name ) ) {
94
+ displayName = aliasDisplayName ( name )
95
+ $errUtils . throwErrByPath ( 'alias.invalid' , {
121
96
onFail : log ,
122
- args : { cmd , displayName, availableAliases : availableAliases . join ( ', ' ) } ,
97
+ args : { name , displayName } ,
123
98
} )
124
99
}
125
100
126
- return {
127
- getAlias,
128
-
129
- addAlias,
130
-
131
- // these are public because its expected other commands
132
- // know about them and are expected to call them
133
- getNextAlias,
101
+ cmd = cmd ?? ( ( log && log . get ( 'name' ) ) || cy . state ( 'current' ) . get ( 'name' ) )
102
+ displayName = aliasDisplayName ( name )
134
103
135
- validateAlias,
104
+ const errPath = availableAliases . length
105
+ ? 'alias.not_registered_with_available'
106
+ : 'alias.not_registered_without_available'
136
107
137
- aliasNotFoundFor,
138
-
139
- getXhrTypeByAlias,
108
+ $errUtils . throwErrByPath ( errPath , {
109
+ onFail : log ,
110
+ args : { cmd, displayName, availableAliases : availableAliases . join ( ', ' ) } ,
111
+ } )
112
+ } ,
140
113
141
- getAvailableAliases,
114
+ getXhrTypeByAlias ( alias ) {
115
+ if ( requestXhrRe . test ( alias ) ) {
116
+ return 'request'
142
117
}
118
+
119
+ return 'response'
143
120
} ,
144
- }
121
+ } )
122
+
123
+ export interface IAliases extends ReturnType < typeof create > { }
0 commit comments