@@ -107,6 +107,87 @@ describe('api', () => {
107107 expectUrlToMatchWithPKCE ( url , { baseUrl : MY_ACCOUNT_DOMAINS . production , path : '/oauth/authorize' , query } )
108108 } ) ;
109109
110+ test ( 'includes affiliate_code when provided' , ( ) => {
111+ mockedStorage . set . mockClear ( ) ;
112+ open . mockClear ( ) ;
113+
114+ const state = 'state' ;
115+ const scopes = 'points_read' ;
116+ const samlSubjectId = 'mySubject' ;
117+ const affiliateCode = 'mtb_hoge' ;
118+
119+ const mtLinkSdk = new MtLinkSdk ( ) ;
120+ mtLinkSdk . init ( clientId , { samlSubjectId } ) ;
121+
122+ authorize ( mtLinkSdk . storedOptions , {
123+ state,
124+ redirectUri,
125+ scopes,
126+ affiliateCode
127+ } ) ;
128+
129+ expect ( open ) . toBeCalledTimes ( 1 ) ;
130+ expect ( open ) . toBeCalledWith ( expect . any ( String ) , '_self' , 'noreferrer' ) ;
131+ const url = open . mock . calls [ 0 ] [ 0 ]
132+
133+ const parsed = new URL ( url ) ;
134+ expect ( parsed . searchParams . has ( 'affiliate_code' ) ) . toBe ( true ) ;
135+ } ) ;
136+
137+ test ( 'does not include affiliate_code when affiliateCode is undefined' , ( ) => {
138+ mockedStorage . set . mockClear ( ) ;
139+ open . mockClear ( ) ;
140+
141+ const state = 'state' ;
142+ const scopes = 'points_read' ;
143+ const samlSubjectId = 'mySubject' ;
144+ const affiliateCode = undefined ;
145+
146+ const mtLinkSdk = new MtLinkSdk ( ) ;
147+ mtLinkSdk . init ( clientId , { samlSubjectId } ) ;
148+
149+ authorize ( mtLinkSdk . storedOptions , {
150+ state,
151+ redirectUri,
152+ scopes,
153+ affiliateCode
154+ } ) ;
155+
156+ expect ( open ) . toBeCalledTimes ( 1 ) ;
157+ expect ( open ) . toBeCalledWith ( expect . any ( String ) , '_self' , 'noreferrer' ) ;
158+ const url = open . mock . calls [ 0 ] [ 0 ]
159+
160+ const parsed = new URL ( url ) ;
161+ expect ( parsed . searchParams . has ( 'affiliate_code' ) ) . toBe ( false ) ;
162+ } ) ;
163+
164+ test ( 'does not include affiliate_code when affiliateCode is null' , ( ) => {
165+ mockedStorage . set . mockClear ( ) ;
166+ open . mockClear ( ) ;
167+
168+ const state = 'state' ;
169+ const scopes = 'points_read' ;
170+ const samlSubjectId = 'mySubject' ;
171+ const affiliateCode = null as any ;
172+
173+ const mtLinkSdk = new MtLinkSdk ( ) ;
174+ mtLinkSdk . init ( clientId , { samlSubjectId } ) ;
175+
176+ authorize ( mtLinkSdk . storedOptions , {
177+ state,
178+ redirectUri,
179+ scopes,
180+ affiliateCode
181+ } ) ;
182+
183+ expect ( open ) . toBeCalledTimes ( 1 ) ;
184+ expect ( open ) . toBeCalledWith ( expect . any ( String ) , '_self' , 'noreferrer' ) ;
185+ const url = open . mock . calls [ 0 ] [ 0 ]
186+
187+ const parsed = new URL ( url ) ;
188+ expect ( parsed . searchParams . has ( 'affiliate_code' ) ) . toBe ( false ) ;
189+ } ) ;
190+
110191 test ( 'without window' , ( ) => {
111192 const windowSpy = jest . spyOn ( global , 'window' , 'get' ) ;
112193 // @ts -ignore: mocking window object to undefined
0 commit comments