1- import { Environment } from '@imtbl/config' ;
21import { AxiosResponse } from 'axios' ;
32import { ChainId } from '../types' ;
43import { RemoteConfigFetcher } from './remoteConfigFetcher' ;
5- import {
6- CHECKOUT_CDN_BASE_URL ,
7- ENV_DEVELOPMENT ,
8- } from '../env' ;
94import { HttpClient } from '../api/http' ;
105
116jest . mock ( '../api/http' ) ;
@@ -22,31 +17,51 @@ describe('RemoteConfig', () => {
2217 mockedHttpClient = new HttpClient ( ) as jest . Mocked < HttpClient > ;
2318 } ) ;
2419
25- [ Environment . PRODUCTION , Environment . SANDBOX , ENV_DEVELOPMENT ] . forEach ( ( env ) => {
26- describe ( 'config' , ( ) => {
27- it ( `should fetch configs and cache them [${ env } ]` , async ( ) => {
28- const mockResponse = {
29- status : 200 ,
30- data : {
31- connect : {
32- walletConnect : false ,
33- } ,
34- dex : {
35- overrides : {
36- rpcURL : 'https://test.com' ,
37- } ,
20+ describe ( 'config' , ( ) => {
21+ it ( 'should fetch configs and cache them' , async ( ) => {
22+ const mockResponse = {
23+ status : 200 ,
24+ data : {
25+ connect : {
26+ walletConnect : false ,
27+ } ,
28+ dex : {
29+ overrides : {
30+ rpcURL : 'https://test.com' ,
3831 } ,
39- allowedNetworks : [ ChainId . SEPOLIA ] ,
4032 } ,
41- } as AxiosResponse ;
42- mockedHttpClient . get . mockResolvedValueOnce ( mockResponse ) ;
33+ allowedNetworks : [ ChainId . SEPOLIA ] ,
34+ } ,
35+ } as AxiosResponse ;
36+ mockedHttpClient . get . mockResolvedValueOnce ( mockResponse ) ;
37+
38+ const fetcher = new RemoteConfigFetcher ( mockedHttpClient , {
39+ remoteConfigEndpoint : 'configUrl' ,
40+ } ) ;
41+
42+ expect ( await fetcher . getConfig ( ) ) . toEqual ( {
43+ connect : {
44+ walletConnect : false ,
45+ } ,
46+ dex : {
47+ overrides : {
48+ rpcURL : 'https://test.com' ,
49+ } ,
50+ } ,
51+ allowedNetworks : [ ChainId . SEPOLIA ] ,
52+ } ) ;
4353
44- const fetcher = new RemoteConfigFetcher ( mockedHttpClient , {
45- isDevelopment : env === ENV_DEVELOPMENT ,
46- isProduction : env !== ENV_DEVELOPMENT && env === Environment . PRODUCTION ,
47- } ) ;
54+ expect ( mockedHttpClient . get ) . toHaveBeenCalledTimes ( 1 ) ;
55+ expect ( mockedHttpClient . get ) . toHaveBeenNthCalledWith (
56+ 1 ,
57+ `configUrl/${ version } /config` ,
58+ ) ;
59+ } ) ;
4860
49- expect ( await fetcher . getConfig ( ) ) . toEqual ( {
61+ it ( 'should fetch config for key' , async ( ) => {
62+ const mockResponse = {
63+ status : 200 ,
64+ data : {
5065 connect : {
5166 walletConnect : false ,
5267 } ,
@@ -56,70 +71,44 @@ describe('RemoteConfig', () => {
5671 } ,
5772 } ,
5873 allowedNetworks : [ ChainId . SEPOLIA ] ,
59- } ) ;
74+ } ,
75+ } as AxiosResponse ;
76+ mockedHttpClient . get . mockResolvedValueOnce ( mockResponse ) ;
6077
61- expect ( mockedHttpClient . get ) . toHaveBeenCalledTimes ( 1 ) ;
62- expect ( mockedHttpClient . get ) . toHaveBeenNthCalledWith (
63- 1 ,
64- `${ CHECKOUT_CDN_BASE_URL [ env ] } /${ version } /config` ,
65- ) ;
78+ const fetcher = new RemoteConfigFetcher ( mockedHttpClient , {
79+ remoteConfigEndpoint : 'abc' ,
6680 } ) ;
6781
68- it ( `should fetch config for key [${ env } ]` , async ( ) => {
69- const mockResponse = {
70- status : 200 ,
71- data : {
72- connect : {
73- walletConnect : false ,
74- } ,
75- dex : {
76- overrides : {
77- rpcURL : 'https://test.com' ,
78- } ,
79- } ,
80- allowedNetworks : [ ChainId . SEPOLIA ] ,
81- } ,
82- } as AxiosResponse ;
83- mockedHttpClient . get . mockResolvedValueOnce ( mockResponse ) ;
82+ expect ( await fetcher . getConfig ( 'allowedNetworks' ) ) . toEqual ( [ ChainId . SEPOLIA ] ) ;
83+ } ) ;
8484
85- const fetcher = new RemoteConfigFetcher ( mockedHttpClient , {
86- isDevelopment : env === ENV_DEVELOPMENT ,
87- isProduction : env !== ENV_DEVELOPMENT && env === Environment . PRODUCTION ,
88- } ) ;
85+ it ( 'should return undefined if missing config' , async ( ) => {
86+ const mockResponse = {
87+ status : 200 ,
88+ } as AxiosResponse ;
89+ mockedHttpClient . get . mockResolvedValueOnce ( mockResponse ) ;
8990
90- expect ( await fetcher . getConfig ( 'allowedNetworks' ) ) . toEqual ( [ ChainId . SEPOLIA ] ) ;
91+ const fetcher = new RemoteConfigFetcher ( mockedHttpClient , {
92+ remoteConfigEndpoint : 'abc' ,
9193 } ) ;
9294
93- it ( `should return undefined if missing config [${ env } ]` , async ( ) => {
94- const mockResponse = {
95- status : 200 ,
96- } as AxiosResponse ;
97- mockedHttpClient . get . mockResolvedValueOnce ( mockResponse ) ;
95+ expect ( await fetcher . getConfig ( ) ) . toBeUndefined ( ) ;
96+ } ) ;
9897
99- const fetcher = new RemoteConfigFetcher ( mockedHttpClient , {
100- isDevelopment : env === ENV_DEVELOPMENT ,
101- isProduction : env !== ENV_DEVELOPMENT && env === Environment . PRODUCTION ,
102- } ) ;
98+ it ( 'should throw error when configuration is invalid JSON' , async ( ) => {
99+ const mockInvalidJSONResponse = {
100+ status : 200 ,
101+ data : 'invalid json' ,
102+ } as AxiosResponse ;
103+ mockedHttpClient . get . mockResolvedValue ( mockInvalidJSONResponse ) ;
103104
104- expect ( await fetcher . getConfig ( ) ) . toBeUndefined ( ) ;
105+ const fetcher = new RemoteConfigFetcher ( mockedHttpClient , {
106+ remoteConfigEndpoint : 'abc' ,
105107 } ) ;
106108
107- it ( 'should throw error when configuration is invalid JSON' , async ( ) => {
108- const mockInvalidJSONResponse = {
109- status : 200 ,
110- data : 'invalid json' ,
111- } as AxiosResponse ;
112- mockedHttpClient . get . mockResolvedValue ( mockInvalidJSONResponse ) ;
113-
114- const fetcher = new RemoteConfigFetcher ( mockedHttpClient , {
115- isDevelopment : env === ENV_DEVELOPMENT ,
116- isProduction : env !== ENV_DEVELOPMENT && env === Environment . PRODUCTION ,
117- } ) ;
118-
119- await expect ( fetcher . getConfig ( ) ) . rejects . toThrowError (
120- new Error ( 'Invalid configuration' ) ,
121- ) ;
122- } ) ;
109+ await expect ( fetcher . getConfig ( ) ) . rejects . toThrowError (
110+ new Error ( 'Invalid configuration' ) ,
111+ ) ;
123112 } ) ;
124113 } ) ;
125114} ) ;
0 commit comments