11import { Environment } from '@imtbl/config' ;
2+ import { AxiosResponse } from 'axios' ;
23import { CheckoutConfiguration } from '../config' ;
34import { RemoteConfigFetcher } from '../config/remoteConfigFetcher' ;
45import { FiatRampService , FiatRampWidgetParams } from './fiatRamp' ;
56import { ExchangeType , OnRampProvider } from '../types' ;
67import { HttpClient } from '../api/http' ;
78
89const defaultURL = 'https://global-stg.transak.com' ;
9- const defaultParams = {
10- apiKey : 'mock-api-key' ,
11- network : 'immutablezkevm' ,
12- defaultPaymentMethod : 'credit_debit_card' ,
13- disablePaymentMethods : '' ,
14- productsAvailed : 'buy' ,
15- exchangeScreenTitle : 'Buy' ,
16- themeColor : '0D0D0D' ,
17- } ;
18-
19- const defaultWidgetUrl = `${ defaultURL } ?${ new URLSearchParams (
20- defaultParams ,
21- ) . toString ( ) } `;
10+ const SANDBOX_WIDGET_URL = 'https://api.sandbox.immutable.com/checkout/v1/widget-url' ;
2211
2312jest . mock ( '../config/remoteConfigFetcher' ) ;
2413
@@ -29,6 +18,9 @@ describe('FiatRampService', () => {
2918
3019 beforeEach ( ( ) => {
3120 mockedHttpClient = new HttpClient ( ) as jest . Mocked < HttpClient > ;
21+ mockedHttpClient . post = jest . fn ( ) . mockResolvedValue ( {
22+ data : { url : defaultURL } ,
23+ } as AxiosResponse ) ;
3224 } ) ;
3325
3426 describe ( 'feeEstimate' , ( ) => {
@@ -111,21 +103,22 @@ describe('FiatRampService', () => {
111103 ) ;
112104 fiatRampService = new FiatRampService ( config ) ;
113105 } ) ;
106+
114107 it ( `should return widget url with non-configurable query params when onRampProvider is Transak' +
115108 'and default to IMX` , async ( ) => {
116109 const params : FiatRampWidgetParams = {
117110 exchangeType : ExchangeType . ONRAMP ,
118111 isPassport : false ,
119112 } ;
120- const result = await fiatRampService . createWidgetUrl ( params ) ;
121- expect ( result ) . toContain ( defaultWidgetUrl ) ;
122- expect ( result ) . toContain ( '&defaultCryptoCurrency=IMX' ) ;
123- expect ( result ) . not . toContain ( '&email=' ) ;
124- expect ( result ) . not . toContain (
125- '&isAutoFillUserData=true&disableWalletAddressForm=true' ,
113+ const result = await fiatRampService . createWidgetUrl ( params , mockedHttpClient ) ;
114+ expect ( result ) . toContain ( 'https://global-stg.transak.com' ) ;
115+ expect ( mockedHttpClient . post ) . toHaveBeenCalledWith (
116+ SANDBOX_WIDGET_URL ,
117+ expect . objectContaining ( {
118+ default_crypto_currency : 'IMX' ,
119+ } ) ,
120+ { method : 'POST' } ,
126121 ) ;
127- expect ( result ) . not . toContain ( '&defaultCryptoAmount=' ) ;
128- expect ( result ) . not . toContain ( '&walletAddress=' ) ;
129122 } ) ;
130123
131124 it ( `should return widget url with encoded email, isAutoFillUserData and disableWalletAddressForm query params
@@ -135,11 +128,17 @@ describe('FiatRampService', () => {
135128 isPassport : true ,
136129137130 } ;
138- const result = await fiatRampService . createWidgetUrl ( params ) ;
139- expect ( result ) . toContain ( defaultWidgetUrl ) ;
140- expect ( result ) . toContain ( '&email=passport.user%2540immutable.com' ) ;
141- expect ( result ) . toContain ( '&isAutoFillUserData=true' ) ;
142- expect ( result ) . toContain ( '&disableWalletAddressForm=true' ) ;
131+ const result = await fiatRampService . createWidgetUrl ( params , mockedHttpClient ) ;
132+ expect ( result ) . toContain ( 'https://global-stg.transak.com' ) ;
133+ expect ( mockedHttpClient . post ) . toHaveBeenCalledWith (
134+ SANDBOX_WIDGET_URL ,
135+ expect . objectContaining ( {
136+ 137+ is_auto_fill_user_data : true ,
138+ disable_wallet_address_form : true ,
139+ } ) ,
140+ { method : 'POST' } ,
141+ ) ;
143142 } ) ;
144143
145144 it ( `should return widget url with defaultFiatAmount and defaultCryptoCurrency query params when tokenAmount and
@@ -148,12 +147,16 @@ describe('FiatRampService', () => {
148147 exchangeType : ExchangeType . ONRAMP ,
149148 isPassport : false ,
150149 } ;
151- const result = await fiatRampService . createWidgetUrl ( params ) ;
152- expect ( result ) . toContain ( defaultWidgetUrl ) ;
153- expect ( result ) . toContain ( '&defaultFiatAmount=50' ) ;
154- expect ( result ) . toContain ( '&defaultCryptoCurrency=IMX' ) ;
155- expect ( result ) . not . toContain ( '&defaultCryptoAmount=100' ) ;
156- expect ( result ) . not . toContain ( '&cryptoCurrencyCode=ETH' ) ;
150+ const result = await fiatRampService . createWidgetUrl ( params , mockedHttpClient ) ;
151+ expect ( result ) . toContain ( 'https://global-stg.transak.com' ) ;
152+ expect ( mockedHttpClient . post ) . toHaveBeenCalledWith (
153+ SANDBOX_WIDGET_URL ,
154+ expect . objectContaining ( {
155+ default_fiat_amount : 50 ,
156+ default_crypto_currency : 'IMX' ,
157+ } ) ,
158+ { method : 'POST' } ,
159+ ) ;
157160 } ) ;
158161
159162 it ( `should return widget url with defaultCryptoAmount and cryptoCurrencyCode query params when tokenAmount and
@@ -164,11 +167,16 @@ describe('FiatRampService', () => {
164167 tokenAmount : '100' ,
165168 tokenSymbol : 'ETH' ,
166169 } ;
167- const result = await fiatRampService . createWidgetUrl ( params ) ;
168- expect ( result ) . toContain ( defaultWidgetUrl ) ;
169- expect ( result ) . toContain ( '&defaultCryptoAmount=100' ) ;
170- expect ( result ) . toContain ( '&cryptoCurrencyCode=ETH' ) ;
171- expect ( result ) . not . toContain ( '&defaultCryptoCurrency=IMX' ) ;
170+ const result = await fiatRampService . createWidgetUrl ( params , mockedHttpClient ) ;
171+ expect ( result ) . toContain ( 'https://global-stg.transak.com' ) ;
172+ expect ( mockedHttpClient . post ) . toHaveBeenCalledWith (
173+ SANDBOX_WIDGET_URL ,
174+ expect . objectContaining ( {
175+ default_crypto_amount : '100' ,
176+ crypto_currency_code : 'ETH' ,
177+ } ) ,
178+ { method : 'POST' } ,
179+ ) ;
172180 } ) ;
173181
174182 it ( 'should return widget url with walletAddress query params when walletAddress is present' , async ( ) => {
@@ -177,9 +185,15 @@ describe('FiatRampService', () => {
177185 isPassport : false ,
178186 walletAddress : '0x1234567890' ,
179187 } ;
180- const result = await fiatRampService . createWidgetUrl ( params ) ;
181- expect ( result ) . toContain ( defaultWidgetUrl ) ;
182- expect ( result ) . toContain ( '&walletAddress=0x1234567890' ) ;
188+ const result = await fiatRampService . createWidgetUrl ( params , mockedHttpClient ) ;
189+ expect ( result ) . toContain ( 'https://global-stg.transak.com' ) ;
190+ expect ( mockedHttpClient . post ) . toHaveBeenCalledWith (
191+ SANDBOX_WIDGET_URL ,
192+ expect . objectContaining ( {
193+ wallet_address : '0x1234567890' ,
194+ } ) ,
195+ { method : 'POST' } ,
196+ ) ;
183197 } ) ;
184198
185199 it ( 'should return widget url with allowed crypto tokens in query params when allowed list is present' , async ( ) => {
@@ -188,9 +202,15 @@ describe('FiatRampService', () => {
188202 isPassport : false ,
189203 allowedTokens : [ 'ETH' , 'IMX' ] ,
190204 } ;
191- const result = await fiatRampService . createWidgetUrl ( params ) ;
192- expect ( result ) . toContain ( defaultWidgetUrl ) ;
193- expect ( result ) . toContain ( '&cryptoCurrencyList=eth%2Cimx' ) ;
205+ const result = await fiatRampService . createWidgetUrl ( params , mockedHttpClient ) ;
206+ expect ( result ) . toContain ( 'https://global-stg.transak.com' ) ;
207+ expect ( mockedHttpClient . post ) . toHaveBeenCalledWith (
208+ SANDBOX_WIDGET_URL ,
209+ expect . objectContaining ( {
210+ crypto_currency_list : 'eth,imx' ,
211+ } ) ,
212+ { method : 'POST' } ,
213+ ) ;
194214 } ) ;
195215 } ) ;
196216} ) ;
0 commit comments