@@ -12,133 +12,112 @@ const aliasDisplayName = (name) => {
1212 return name . replace ( aliasDisplayRe , '' )
1313}
1414
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
1919
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' ) || { }
4721
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 )
5224
53- const aliases = cy . state ( 'aliases' ) || { }
25+ const remoteSubject = cy . getRemotejQueryInstance ( subject )
5426
55- aliases [ alias ] = aliasObj
56- cy . state ( 'aliases' , aliases )
27+ ctx [ alias ] = remoteSubject ?? subject
28+ } ,
5729
58- const remoteSubject = cy . getRemotejQueryInstance ( subject )
30+ getAlias ( name , cmd , log ) {
31+ const aliases = cy . state ( 'aliases' ) || { }
5932
60- ctx [ alias ] = remoteSubject ?? subject
33+ // bail if the name doesnt reference an alias
34+ if ( ! aliasRe . test ( name ) ) {
35+ return
6136 }
6237
63- const getNextAlias = ( ) => {
64- const next = cy . state ( 'current' ) . get ( 'next' )
38+ const alias = aliases [ name . slice ( 1 ) ]
6539
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 )
6943 }
7044
71- const getAlias = ( name , cmd , log ) => {
72- const aliases = cy . state ( 'aliases' ) || { }
45+ return alias
46+ } ,
7347
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
7850
79- const alias = aliases [ name . slice ( 1 ) ]
51+ getNextAlias ( ) {
52+ const next = cy . state ( 'current' ) . get ( 'next' )
8053
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+ } ,
8558
86- return alias
59+ validateAlias ( alias : string ) {
60+ if ( ! _ . isString ( alias ) ) {
61+ $errUtils . throwErrByPath ( 'as.invalid_type' )
8762 }
8863
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+ }
9172
92- if ( ! aliases ) {
93- return [ ]
94- }
73+ if ( _ . isEmpty ( alias ) ) {
74+ $errUtils . throwErrByPath ( 'as.empty_string' )
75+ }
9576
96- return _ . keys ( aliases )
77+ if ( reserved . includes ( alias ) ) {
78+ $errUtils . throwErrByPath ( 'as.reserved_word' , { args : { alias } } )
9779 }
9880
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+ : [ ]
11588
116- const errPath = availableAliases . length
117- ? 'alias.not_registered_with_available'
118- : 'alias.not_registered_without_available'
89+ let displayName
11990
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' , {
12196 onFail : log ,
122- args : { cmd , displayName, availableAliases : availableAliases . join ( ', ' ) } ,
97+ args : { name , displayName } ,
12398 } )
12499 }
125100
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 )
134103
135- validateAlias,
104+ const errPath = availableAliases . length
105+ ? 'alias.not_registered_with_available'
106+ : 'alias.not_registered_without_available'
136107
137- aliasNotFoundFor,
138-
139- getXhrTypeByAlias,
108+ $errUtils . throwErrByPath ( errPath , {
109+ onFail : log ,
110+ args : { cmd, displayName, availableAliases : availableAliases . join ( ', ' ) } ,
111+ } )
112+ } ,
140113
141- getAvailableAliases,
114+ getXhrTypeByAlias ( alias ) {
115+ if ( requestXhrRe . test ( alias ) ) {
116+ return 'request'
142117 }
118+
119+ return 'response'
143120 } ,
144- }
121+ } )
122+
123+ export interface IAliases extends ReturnType < typeof create > { }
0 commit comments