11import  type  {  BlockTracker  }  from  '@metamask/network-controller' ; 
22
3- import  {  ACCELERATED_COUNT_MAX ,   TransactionPoller  }  from  './TransactionPoller' ; 
3+ import  {  TransactionPoller  }  from  './TransactionPoller' ; 
44import  {  flushPromises  }  from  '../../../../tests/helpers' ; 
5+ import  type  {  TransactionControllerMessenger  }  from  '../TransactionController' ; 
56import  type  {  TransactionMeta  }  from  '../types' ; 
67
78jest . useFakeTimers ( ) ; 
89
910const  BLOCK_NUMBER_MOCK  =  '0x123' ; 
11+ const  CHAIN_ID_MOCK  =  '0x1' ; 
12+ const  DEFAULT_ACCELERATED_COUNT_MAX  =  10 ; 
13+ const  DEFAULT_ACCELERATED_POLLING_INTERVAL_MS  =  3000 ; 
1014
1115const  BLOCK_TRACKER_MOCK  =  { 
1216  getLatestBlock : jest . fn ( ) , 
1317  on : jest . fn ( ) , 
1418  removeListener : jest . fn ( ) , 
1519}  as  unknown  as  jest . Mocked < BlockTracker > ; 
1620
21+ const  MESSENGER_MOCK  =  { 
22+   call : jest . fn ( ) . mockReturnValue ( { 
23+     remoteFeatureFlags : { } , 
24+   } ) , 
25+ }  as  unknown  as  jest . Mocked < TransactionControllerMessenger > ; 
26+ 
27+ jest . mock ( '../utils/feature-flags' ,  ( )  =>  ( { 
28+   getAcceleratedPollingParams : ( )  =>  ( { 
29+     countMax : DEFAULT_ACCELERATED_COUNT_MAX , 
30+     intervalMs : DEFAULT_ACCELERATED_POLLING_INTERVAL_MS , 
31+   } ) , 
32+   FEATURE_FLAG_TRANSACTIONS : 'confirmations_transactions' , 
33+ } ) ) ; 
34+ 
1735/** 
1836 * Creates a mock transaction metadata object. 
1937 * 
@@ -32,7 +50,11 @@ describe('TransactionPoller', () => {
3250
3351  describe ( 'Accelerated Polling' ,  ( )  =>  { 
3452    it ( 'invokes listener after timeout' ,  async  ( )  =>  { 
35-       const  poller  =  new  TransactionPoller ( BLOCK_TRACKER_MOCK ) ; 
53+       const  poller  =  new  TransactionPoller ( 
54+         BLOCK_TRACKER_MOCK , 
55+         MESSENGER_MOCK , 
56+         CHAIN_ID_MOCK , 
57+       ) ; 
3658
3759      const  listener  =  jest . fn ( ) ; 
3860      poller . start ( listener ) ; 
@@ -46,21 +68,29 @@ describe('TransactionPoller', () => {
4668    } ) ; 
4769
4870    it ( 'stops creating timeouts after max reached' ,  async  ( )  =>  { 
49-       const  poller  =  new  TransactionPoller ( BLOCK_TRACKER_MOCK ) ; 
71+       const  poller  =  new  TransactionPoller ( 
72+         BLOCK_TRACKER_MOCK , 
73+         MESSENGER_MOCK , 
74+         CHAIN_ID_MOCK , 
75+       ) ; 
5076
5177      const  listener  =  jest . fn ( ) ; 
5278      poller . start ( listener ) ; 
5379
54-       for  ( let  i  =  0 ;  i  <  ACCELERATED_COUNT_MAX  *  3 ;  i ++ )  { 
80+       for  ( let  i  =  0 ;  i  <  DEFAULT_ACCELERATED_COUNT_MAX  *  3 ;  i ++ )  { 
5581        jest . runOnlyPendingTimers ( ) ; 
5682        await  flushPromises ( ) ; 
5783      } 
5884
59-       expect ( listener ) . toHaveBeenCalledTimes ( ACCELERATED_COUNT_MAX ) ; 
85+       expect ( listener ) . toHaveBeenCalledTimes ( DEFAULT_ACCELERATED_COUNT_MAX ) ; 
6086    } ) ; 
6187
6288    it ( 'invokes listener with latest block number from block tracker' ,  async  ( )  =>  { 
63-       const  poller  =  new  TransactionPoller ( BLOCK_TRACKER_MOCK ) ; 
89+       const  poller  =  new  TransactionPoller ( 
90+         BLOCK_TRACKER_MOCK , 
91+         MESSENGER_MOCK , 
92+         CHAIN_ID_MOCK , 
93+       ) ; 
6494
6595      BLOCK_TRACKER_MOCK . getLatestBlock . mockResolvedValue ( BLOCK_NUMBER_MOCK ) ; 
6696
@@ -74,7 +104,11 @@ describe('TransactionPoller', () => {
74104    } ) ; 
75105
76106    it ( 'does not create timeout if stopped while listener being invoked' ,  async  ( )  =>  { 
77-       const  poller  =  new  TransactionPoller ( BLOCK_TRACKER_MOCK ) ; 
107+       const  poller  =  new  TransactionPoller ( 
108+         BLOCK_TRACKER_MOCK , 
109+         MESSENGER_MOCK , 
110+         CHAIN_ID_MOCK , 
111+       ) ; 
78112
79113      const  listener  =  jest . fn ( ) ; 
80114      listener . mockImplementation ( ( )  =>  poller . stop ( ) ) ; 
@@ -90,12 +124,16 @@ describe('TransactionPoller', () => {
90124
91125  describe ( 'Block Tracker Polling' ,  ( )  =>  { 
92126    it ( 'invokes listener on block tracker update after accelerated limit reached' ,  async  ( )  =>  { 
93-       const  poller  =  new  TransactionPoller ( BLOCK_TRACKER_MOCK ) ; 
127+       const  poller  =  new  TransactionPoller ( 
128+         BLOCK_TRACKER_MOCK , 
129+         MESSENGER_MOCK , 
130+         CHAIN_ID_MOCK , 
131+       ) ; 
94132
95133      const  listener  =  jest . fn ( ) ; 
96134      poller . start ( listener ) ; 
97135
98-       for  ( let  i  =  0 ;  i  <  ACCELERATED_COUNT_MAX ;  i ++ )  { 
136+       for  ( let  i  =  0 ;  i  <  DEFAULT_ACCELERATED_COUNT_MAX ;  i ++ )  { 
99137        jest . runOnlyPendingTimers ( ) ; 
100138        await  flushPromises ( ) ; 
101139      } 
@@ -106,16 +144,20 @@ describe('TransactionPoller', () => {
106144      BLOCK_TRACKER_MOCK . on . mock . calls [ 0 ] [ 1 ] ( ) ; 
107145      await  flushPromises ( ) ; 
108146
109-       expect ( listener ) . toHaveBeenCalledTimes ( ACCELERATED_COUNT_MAX  +  2 ) ; 
147+       expect ( listener ) . toHaveBeenCalledTimes ( DEFAULT_ACCELERATED_COUNT_MAX  +  2 ) ; 
110148    } ) ; 
111149
112150    it ( 'invokes listener with latest block number from event' ,  async  ( )  =>  { 
113-       const  poller  =  new  TransactionPoller ( BLOCK_TRACKER_MOCK ) ; 
151+       const  poller  =  new  TransactionPoller ( 
152+         BLOCK_TRACKER_MOCK , 
153+         MESSENGER_MOCK , 
154+         CHAIN_ID_MOCK , 
155+       ) ; 
114156
115157      const  listener  =  jest . fn ( ) ; 
116158      poller . start ( listener ) ; 
117159
118-       for  ( let  i  =  0 ;  i  <  ACCELERATED_COUNT_MAX ;  i ++ )  { 
160+       for  ( let  i  =  0 ;  i  <  DEFAULT_ACCELERATED_COUNT_MAX ;  i ++ )  { 
119161        jest . runOnlyPendingTimers ( ) ; 
120162        await  flushPromises ( ) ; 
121163      } 
@@ -129,7 +171,11 @@ describe('TransactionPoller', () => {
129171
130172  describe ( 'start' ,  ( )  =>  { 
131173    it ( 'does nothing if already started' ,  ( )  =>  { 
132-       const  poller  =  new  TransactionPoller ( BLOCK_TRACKER_MOCK ) ; 
174+       const  poller  =  new  TransactionPoller ( 
175+         BLOCK_TRACKER_MOCK , 
176+         MESSENGER_MOCK , 
177+         CHAIN_ID_MOCK , 
178+       ) ; 
133179
134180      poller . start ( jest . fn ( ) ) ; 
135181      poller . start ( jest . fn ( ) ) ; 
@@ -140,7 +186,11 @@ describe('TransactionPoller', () => {
140186
141187  describe ( 'stop' ,  ( )  =>  { 
142188    it ( 'removes timeout' ,  ( )  =>  { 
143-       const  poller  =  new  TransactionPoller ( BLOCK_TRACKER_MOCK ) ; 
189+       const  poller  =  new  TransactionPoller ( 
190+         BLOCK_TRACKER_MOCK , 
191+         MESSENGER_MOCK , 
192+         CHAIN_ID_MOCK , 
193+       ) ; 
144194
145195      const  listener  =  jest . fn ( ) ; 
146196      poller . start ( listener ) ; 
@@ -151,24 +201,32 @@ describe('TransactionPoller', () => {
151201    } ) ; 
152202
153203    it ( 'removes block tracker listener' ,  async  ( )  =>  { 
154-       const  poller  =  new  TransactionPoller ( BLOCK_TRACKER_MOCK ) ; 
204+       const  poller  =  new  TransactionPoller ( 
205+         BLOCK_TRACKER_MOCK , 
206+         MESSENGER_MOCK , 
207+         CHAIN_ID_MOCK , 
208+       ) ; 
155209
156210      const  listener  =  jest . fn ( ) ; 
157211      poller . start ( listener ) ; 
158212
159-       for  ( let  i  =  0 ;  i  <  ACCELERATED_COUNT_MAX ;  i ++ )  { 
213+       for  ( let  i  =  0 ;  i  <  DEFAULT_ACCELERATED_COUNT_MAX ;  i ++ )  { 
160214        jest . runOnlyPendingTimers ( ) ; 
161215        await  flushPromises ( ) ; 
162216      } 
163217
164218      poller . stop ( ) ; 
165219
166220      expect ( BLOCK_TRACKER_MOCK . removeListener ) . toHaveBeenCalledTimes ( 1 ) ; 
167-       expect ( listener ) . toHaveBeenCalledTimes ( ACCELERATED_COUNT_MAX ) ; 
221+       expect ( listener ) . toHaveBeenCalledTimes ( DEFAULT_ACCELERATED_COUNT_MAX ) ; 
168222    } ) ; 
169223
170224    it ( 'does nothing if not started' ,  async  ( )  =>  { 
171-       const  poller  =  new  TransactionPoller ( BLOCK_TRACKER_MOCK ) ; 
225+       const  poller  =  new  TransactionPoller ( 
226+         BLOCK_TRACKER_MOCK , 
227+         MESSENGER_MOCK , 
228+         CHAIN_ID_MOCK , 
229+       ) ; 
172230
173231      poller . stop ( ) ; 
174232
@@ -191,7 +249,11 @@ describe('TransactionPoller', () => {
191249    ] ) ( 
192250      'resets accelerated count if transaction IDs %s' , 
193251      async  ( _title ,  newPendingTransactions )  =>  { 
194-         const  poller  =  new  TransactionPoller ( BLOCK_TRACKER_MOCK ) ; 
252+         const  poller  =  new  TransactionPoller ( 
253+           BLOCK_TRACKER_MOCK , 
254+           MESSENGER_MOCK , 
255+           CHAIN_ID_MOCK , 
256+         ) ; 
195257
196258        poller . setPendingTransactions ( [ 
197259          createTransactionMetaMock ( '1' ) , 
@@ -208,12 +270,14 @@ describe('TransactionPoller', () => {
208270
209271        poller . setPendingTransactions ( newPendingTransactions ) ; 
210272
211-         for  ( let  i  =  0 ;  i  <  ACCELERATED_COUNT_MAX ;  i ++ )  { 
273+         for  ( let  i  =  0 ;  i  <  DEFAULT_ACCELERATED_COUNT_MAX ;  i ++ )  { 
212274          jest . runOnlyPendingTimers ( ) ; 
213275          await  flushPromises ( ) ; 
214276        } 
215277
216-         expect ( listener ) . toHaveBeenCalledTimes ( ACCELERATED_COUNT_MAX  +  3 ) ; 
278+         expect ( listener ) . toHaveBeenCalledTimes ( 
279+           DEFAULT_ACCELERATED_COUNT_MAX  +  3 , 
280+         ) ; 
217281      } , 
218282    ) ; 
219283
@@ -230,7 +294,11 @@ describe('TransactionPoller', () => {
230294    ] ) ( 
231295      'resets to accelerated polling if transaction IDs added' , 
232296      async  ( _title ,  newPendingTransactions )  =>  { 
233-         const  poller  =  new  TransactionPoller ( BLOCK_TRACKER_MOCK ) ; 
297+         const  poller  =  new  TransactionPoller ( 
298+           BLOCK_TRACKER_MOCK , 
299+           MESSENGER_MOCK , 
300+           CHAIN_ID_MOCK , 
301+         ) ; 
234302
235303        poller . setPendingTransactions ( [ 
236304          createTransactionMetaMock ( '1' ) , 
@@ -240,7 +308,7 @@ describe('TransactionPoller', () => {
240308        const  listener  =  jest . fn ( ) ; 
241309        poller . start ( listener ) ; 
242310
243-         for  ( let  i  =  0 ;  i  <  ACCELERATED_COUNT_MAX ;  i ++ )  { 
311+         for  ( let  i  =  0 ;  i  <  DEFAULT_ACCELERATED_COUNT_MAX ;  i ++ )  { 
244312          jest . runOnlyPendingTimers ( ) ; 
245313          await  flushPromises ( ) ; 
246314        } 
@@ -253,12 +321,14 @@ describe('TransactionPoller', () => {
253321
254322        poller . setPendingTransactions ( newPendingTransactions ) ; 
255323
256-         for  ( let  i  =  0 ;  i  <  ACCELERATED_COUNT_MAX ;  i ++ )  { 
324+         for  ( let  i  =  0 ;  i  <  DEFAULT_ACCELERATED_COUNT_MAX ;  i ++ )  { 
257325          jest . runOnlyPendingTimers ( ) ; 
258326          await  flushPromises ( ) ; 
259327        } 
260328
261-         expect ( listener ) . toHaveBeenCalledTimes ( ACCELERATED_COUNT_MAX  *  2  +  2 ) ; 
329+         expect ( listener ) . toHaveBeenCalledTimes ( 
330+           DEFAULT_ACCELERATED_COUNT_MAX  *  2  +  2 , 
331+         ) ; 
262332      } , 
263333    ) ; 
264334  } ) ; 
0 commit comments