@@ -258,10 +258,9 @@ const testHistory = [
258
258
] ;
259
259
260
260
const ethereumChainIdDec = parseInt ( CHAIN_IDS . ETHEREUM , 16 ) ;
261
+ const goerliChainIdDec = parseInt ( CHAIN_IDS . GOERLI , 16 ) ;
261
262
262
263
const trackMetaMetricsEventSpy = jest . fn ( ) ;
263
- const getNetworkClientByIdSpy = jest . fn ( ) ;
264
-
265
264
const defaultState = {
266
265
smartTransactionsState : {
267
266
smartTransactions : {
@@ -307,7 +306,24 @@ describe('SmartTransactionsController', () => {
307
306
provider : jest . fn ( ) ,
308
307
confirmExternalTransaction : confirmExternalMock ,
309
308
trackMetaMetricsEvent : trackMetaMetricsEventSpy ,
310
- getNetworkClientById : getNetworkClientByIdSpy ,
309
+ getNetworkClientById : jest . fn ( ) . mockImplementation ( ( networkClientId ) => {
310
+ switch ( networkClientId ) {
311
+ case 'mainnet' :
312
+ return {
313
+ configuration : {
314
+ chainId : CHAIN_IDS . ETHEREUM ,
315
+ } ,
316
+ } ;
317
+ case 'goerli' :
318
+ return {
319
+ configuration : {
320
+ chainId : CHAIN_IDS . GOERLI ,
321
+ } ,
322
+ } ;
323
+ default :
324
+ throw new Error ( 'Invalid network client id' ) ;
325
+ }
326
+ } ) ,
311
327
} ) ;
312
328
// eslint-disable-next-line jest/prefer-spy-on
313
329
smartTransactionsController . subscribe = jest . fn ( ) ;
@@ -402,6 +418,8 @@ describe('SmartTransactionsController', () => {
402
418
} ) ;
403
419
404
420
describe ( 'updateSmartTransactions' , ( ) => {
421
+ // TODO rewrite this test... updateSmartTransactions is getting called via the checkPoll method which is called whenever state is updated.
422
+ // this test should be more isolated to the updateSmartTransactions method.
405
423
it ( 'calls fetchSmartTransactionsStatus if there are pending transactions' , ( ) => {
406
424
const fetchSmartTransactionsStatusSpy = jest
407
425
. spyOn ( smartTransactionsController , 'fetchSmartTransactionsStatus' )
@@ -490,25 +508,24 @@ describe('SmartTransactionsController', () => {
490
508
} ) ;
491
509
492
510
it ( 'should add fee data to feesByChainId state using the networkClientId passed in to identify the appropriate chain' , async ( ) => {
493
- const goerliChainIdDec = parseInt ( CHAIN_IDS . GOERLI , 16 ) ;
494
- getNetworkClientByIdSpy . mockImplementation ( ( networkClientId ) => {
495
- switch ( networkClientId ) {
496
- case 'mainnet' :
497
- return {
498
- configuration : {
499
- chainId : CHAIN_IDS . ETHEREUM ,
500
- } ,
501
- } ;
502
- case 'goerli' :
503
- return {
504
- configuration : {
505
- chainId : CHAIN_IDS . GOERLI ,
506
- } ,
507
- } ;
508
- default :
509
- throw new Error ( 'Invalid network client id' ) ;
510
- }
511
- } ) ;
511
+ // getNetworkClientByIdSpy.mockImplementation((networkClientId) => {
512
+ // switch (networkClientId) {
513
+ // case 'mainnet':
514
+ // return {
515
+ // configuration: {
516
+ // chainId: CHAIN_IDS.ETHEREUM,
517
+ // },
518
+ // };
519
+ // case 'goerli':
520
+ // return {
521
+ // configuration: {
522
+ // chainId: CHAIN_IDS.GOERLI,
523
+ // },
524
+ // };
525
+ // default:
526
+ // throw new Error('Invalid network client id');
527
+ // }
528
+ // });
512
529
513
530
const tradeTx = createUnsignedTransaction ( goerliChainIdDec ) ;
514
531
const approvalTx = createUnsignedTransaction ( goerliChainIdDec ) ;
@@ -681,6 +698,30 @@ describe('SmartTransactionsController', () => {
681
698
const liveness = await smartTransactionsController . fetchLiveness ( ) ;
682
699
expect ( liveness ) . toBe ( true ) ;
683
700
} ) ;
701
+
702
+ it ( 'fetches liveness and sets in feesByChainId state for the Smart Transactions API for the chainId of the networkClientId passed in' , async ( ) => {
703
+ nock ( API_BASE_URL )
704
+ . get ( `/networks/${ goerliChainIdDec } /health` )
705
+ . replyWithError ( 'random error' ) ;
706
+
707
+ expect (
708
+ smartTransactionsController . state . smartTransactionsState
709
+ . livenessByChainId ,
710
+ ) . toStrictEqual ( {
711
+ [ CHAIN_IDS . ETHEREUM ] : true ,
712
+ [ CHAIN_IDS . GOERLI ] : true ,
713
+ } ) ;
714
+
715
+ await smartTransactionsController . fetchLiveness ( 'goerli' ) ;
716
+
717
+ expect (
718
+ smartTransactionsController . state . smartTransactionsState
719
+ . livenessByChainId ,
720
+ ) . toStrictEqual ( {
721
+ [ CHAIN_IDS . ETHEREUM ] : true ,
722
+ [ CHAIN_IDS . GOERLI ] : false ,
723
+ } ) ;
724
+ } ) ;
684
725
} ) ;
685
726
686
727
describe ( 'updateSmartTransaction' , ( ) => {
@@ -867,7 +908,7 @@ describe('SmartTransactionsController', () => {
867
908
} ) ;
868
909
869
910
describe ( 'startPollingByNetworkClientId' , ( ) => {
870
- it ( 'starts and stops calling smart transactions batch status api endpoint with the correct chainId at the interval passed via the constructor ' , async ( ) => {
911
+ it ( 'starts and stops calling smart transactions batch status api endpoint with the correct chainId at the polling interval ' , async ( ) => {
871
912
// mock this to a noop because it causes an extra fetch call to the API upon state changes
872
913
jest
873
914
. spyOn ( smartTransactionsController , 'checkPoll' )
@@ -898,25 +939,6 @@ describe('SmartTransactionsController', () => {
898
939
} ,
899
940
} ) ;
900
941
901
- getNetworkClientByIdSpy . mockImplementation ( ( networkClientId ) => {
902
- switch ( networkClientId ) {
903
- case 'mainnet' :
904
- return {
905
- configuration : {
906
- chainId : CHAIN_IDS . ETHEREUM ,
907
- } ,
908
- } ;
909
- case 'goerli' :
910
- return {
911
- configuration : {
912
- chainId : CHAIN_IDS . GOERLI ,
913
- } ,
914
- } ;
915
- default :
916
- throw new Error ( 'Invalid network client id' ) ;
917
- }
918
- } ) ;
919
-
920
942
jest . useFakeTimers ( ) ;
921
943
const handleFetchSpy = jest . spyOn ( utils , 'handleFetch' ) ;
922
944
0 commit comments