@@ -6,6 +6,15 @@ import SmartTransactionsController, {
6
6
import { API_BASE_URL , CHAIN_IDS } from './constants' ;
7
7
import { SmartTransaction , SmartTransactionStatuses } from './types' ;
8
8
9
+ /**
10
+ * Resolve all pending promises.
11
+ * This method is used for async tests that use fake timers.
12
+ * See https://stackoverflow.com/a/58716087 and https://jestjs.io/docs/timer-mocks.
13
+ */
14
+ function flushPromises ( ) : Promise < unknown > {
15
+ return new Promise ( jest . requireActual ( 'timers' ) . setImmediate ) ;
16
+ }
17
+
9
18
const confirmExternalMock = jest . fn ( ) ;
10
19
11
20
jest . mock ( '@ethersproject/bytes' , ( ) => ( {
@@ -30,6 +39,8 @@ jest.mock('@ethersproject/providers', () => ({
30
39
31
40
const addressFrom = '0x268392a24B6b093127E8581eAfbD1DA228bAdAe3' ;
32
41
42
+ const pollingInterval = 1000 ;
43
+
33
44
const createUnsignedTransaction = ( ) => {
34
45
return {
35
46
from : addressFrom ,
@@ -258,21 +269,24 @@ describe('SmartTransactionsController', () => {
258
269
let networkListener : ( networkState : NetworkState ) => void ;
259
270
260
271
beforeEach ( ( ) => {
261
- smartTransactionsController = new SmartTransactionsController ( {
262
- onNetworkStateChange : ( listener ) => {
263
- networkListener = listener ;
272
+ smartTransactionsController = new SmartTransactionsController (
273
+ {
274
+ onNetworkStateChange : ( listener ) => {
275
+ networkListener = listener ;
276
+ } ,
277
+ getNonceLock : jest . fn ( ( ) => {
278
+ return {
279
+ nextNonce : 'nextNonce' ,
280
+ releaseLock : jest . fn ( ) ,
281
+ } ;
282
+ } ) ,
283
+ provider : jest . fn ( ) ,
284
+ confirmExternalTransaction : confirmExternalMock ,
285
+ trackMetaMetricsEvent : trackMetaMetricsEventSpy ,
286
+ getNetworkClientById : jest . fn ( ) ,
264
287
} ,
265
- getNonceLock : jest . fn ( ( ) => {
266
- return {
267
- nextNonce : 'nextNonce' ,
268
- releaseLock : jest . fn ( ) ,
269
- } ;
270
- } ) ,
271
- provider : jest . fn ( ) ,
272
- confirmExternalTransaction : confirmExternalMock ,
273
- trackMetaMetricsEvent : trackMetaMetricsEventSpy ,
274
- getNetworkClientById : jest . fn ( ) ,
275
- } ) ;
288
+ { interval : pollingInterval } ,
289
+ ) ;
276
290
// eslint-disable-next-line jest/prefer-spy-on
277
291
smartTransactionsController . subscribe = jest . fn ( ) ;
278
292
} ) ;
@@ -508,6 +522,54 @@ describe('SmartTransactionsController', () => {
508
522
} ) ;
509
523
} ) ;
510
524
525
+ describe . only ( 'startPollingByNetworkClientId' , ( ) => {
526
+ it ( 'calls smart transactions batch status api endpoint with the correct chainId at the interval passed via the constructor' , async ( ) => {
527
+ jest . useFakeTimers ( ) ;
528
+ const pendingBatchStatusApiResponse =
529
+ createPendingBatchStatusApiResponse ( ) ;
530
+ nock ( API_BASE_URL )
531
+ . get ( `/networks/${ ethereumChainIdDec } /batchStatus?uuids=uuid1` )
532
+ . reply ( 200 , pendingBatchStatusApiResponse ) ;
533
+
534
+ smartTransactionsController . startPollingByNetworkClientId ( 'mainnet' ) ;
535
+
536
+ await Promise . all ( [
537
+ jest . advanceTimersByTime ( pollingInterval ) ,
538
+ flushPromises ( ) ,
539
+ ] ) ;
540
+
541
+ const pendingState = createStateAfterPending ( ) [ 0 ] ;
542
+ const pendingTransaction = { ...pendingState , history : [ pendingState ] } ;
543
+ expect ( smartTransactionsController . state ) . toStrictEqual ( {
544
+ smartTransactionsState : {
545
+ smartTransactions : {
546
+ [ CHAIN_IDS . ETHEREUM ] : [ pendingTransaction ] ,
547
+ } ,
548
+ userOptIn : undefined ,
549
+ fees : {
550
+ approvalTxFees : undefined ,
551
+ tradeTxFees : undefined ,
552
+ } ,
553
+ feesByChainId : {
554
+ [ CHAIN_IDS . ETHEREUM ] : {
555
+ approvalTxFees : undefined ,
556
+ tradeTxFees : undefined ,
557
+ } ,
558
+ [ CHAIN_IDS . GOERLI ] : {
559
+ approvalTxFees : undefined ,
560
+ tradeTxFees : undefined ,
561
+ } ,
562
+ } ,
563
+ liveness : true ,
564
+ livenessByChainId : {
565
+ [ CHAIN_IDS . ETHEREUM ] : true ,
566
+ [ CHAIN_IDS . GOERLI ] : true ,
567
+ } ,
568
+ } ,
569
+ } ) ;
570
+ } ) ;
571
+ } ) ;
572
+
511
573
describe ( 'fetchSmartTransactionsStatus' , ( ) => {
512
574
beforeEach ( ( ) => {
513
575
// eslint-disable-next-line jest/prefer-spy-on
0 commit comments