11import type { BlockTracker } from '@metamask/network-controller' ;
22
3- import { ACCELERATED_COUNT_MAX , TransactionPoller } from './TransactionPoller' ;
43import { flushPromises } from '../../../../tests/helpers' ;
4+ import type { TransactionControllerMessenger } from '../TransactionController' ;
55import type { TransactionMeta } from '../types' ;
6+ import { TransactionPoller } from './TransactionPoller' ;
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+ // Mock feature flags
28+ jest . mock ( '../utils/feature-flags' , ( ) => ( {
29+ getAcceleratedPollingParams : jest . fn ( ) . mockReturnValue ( {
30+ countMax : DEFAULT_ACCELERATED_COUNT_MAX ,
31+ intervalMs : DEFAULT_ACCELERATED_POLLING_INTERVAL_MS ,
32+ } ) ,
33+ FEATURE_FLAG_TRANSACTIONS : 'confirmations_transactions' ,
34+ } ) ) ;
35+
1736/**
1837 * Creates a mock transaction metadata object.
1938 *
@@ -32,7 +51,11 @@ describe('TransactionPoller', () => {
3251
3352 describe ( 'Accelerated Polling' , ( ) => {
3453 it ( 'invokes listener after timeout' , async ( ) => {
35- const poller = new TransactionPoller ( BLOCK_TRACKER_MOCK ) ;
54+ const poller = new TransactionPoller (
55+ BLOCK_TRACKER_MOCK ,
56+ MESSENGER_MOCK ,
57+ CHAIN_ID_MOCK ,
58+ ) ;
3659
3760 const listener = jest . fn ( ) ;
3861 poller . start ( listener ) ;
@@ -46,21 +69,29 @@ describe('TransactionPoller', () => {
4669 } ) ;
4770
4871 it ( 'stops creating timeouts after max reached' , async ( ) => {
49- const poller = new TransactionPoller ( BLOCK_TRACKER_MOCK ) ;
72+ const poller = new TransactionPoller (
73+ BLOCK_TRACKER_MOCK ,
74+ MESSENGER_MOCK ,
75+ CHAIN_ID_MOCK ,
76+ ) ;
5077
5178 const listener = jest . fn ( ) ;
5279 poller . start ( listener ) ;
5380
54- for ( let i = 0 ; i < ACCELERATED_COUNT_MAX * 3 ; i ++ ) {
81+ for ( let i = 0 ; i < DEFAULT_ACCELERATED_COUNT_MAX * 3 ; i ++ ) {
5582 jest . runOnlyPendingTimers ( ) ;
5683 await flushPromises ( ) ;
5784 }
5885
59- expect ( listener ) . toHaveBeenCalledTimes ( ACCELERATED_COUNT_MAX ) ;
86+ expect ( listener ) . toHaveBeenCalledTimes ( DEFAULT_ACCELERATED_COUNT_MAX ) ;
6087 } ) ;
6188
6289 it ( 'invokes listener with latest block number from block tracker' , async ( ) => {
63- const poller = new TransactionPoller ( BLOCK_TRACKER_MOCK ) ;
90+ const poller = new TransactionPoller (
91+ BLOCK_TRACKER_MOCK ,
92+ MESSENGER_MOCK ,
93+ CHAIN_ID_MOCK ,
94+ ) ;
6495
6596 BLOCK_TRACKER_MOCK . getLatestBlock . mockResolvedValue ( BLOCK_NUMBER_MOCK ) ;
6697
@@ -74,7 +105,11 @@ describe('TransactionPoller', () => {
74105 } ) ;
75106
76107 it ( 'does not create timeout if stopped while listener being invoked' , async ( ) => {
77- const poller = new TransactionPoller ( BLOCK_TRACKER_MOCK ) ;
108+ const poller = new TransactionPoller (
109+ BLOCK_TRACKER_MOCK ,
110+ MESSENGER_MOCK ,
111+ CHAIN_ID_MOCK ,
112+ ) ;
78113
79114 const listener = jest . fn ( ) ;
80115 listener . mockImplementation ( ( ) => poller . stop ( ) ) ;
@@ -90,12 +125,16 @@ describe('TransactionPoller', () => {
90125
91126 describe ( 'Block Tracker Polling' , ( ) => {
92127 it ( 'invokes listener on block tracker update after accelerated limit reached' , async ( ) => {
93- const poller = new TransactionPoller ( BLOCK_TRACKER_MOCK ) ;
128+ const poller = new TransactionPoller (
129+ BLOCK_TRACKER_MOCK ,
130+ MESSENGER_MOCK ,
131+ CHAIN_ID_MOCK ,
132+ ) ;
94133
95134 const listener = jest . fn ( ) ;
96135 poller . start ( listener ) ;
97136
98- for ( let i = 0 ; i < ACCELERATED_COUNT_MAX ; i ++ ) {
137+ for ( let i = 0 ; i < DEFAULT_ACCELERATED_COUNT_MAX ; i ++ ) {
99138 jest . runOnlyPendingTimers ( ) ;
100139 await flushPromises ( ) ;
101140 }
@@ -106,16 +145,20 @@ describe('TransactionPoller', () => {
106145 BLOCK_TRACKER_MOCK . on . mock . calls [ 0 ] [ 1 ] ( ) ;
107146 await flushPromises ( ) ;
108147
109- expect ( listener ) . toHaveBeenCalledTimes ( ACCELERATED_COUNT_MAX + 2 ) ;
148+ expect ( listener ) . toHaveBeenCalledTimes ( DEFAULT_ACCELERATED_COUNT_MAX + 2 ) ;
110149 } ) ;
111150
112151 it ( 'invokes listener with latest block number from event' , async ( ) => {
113- const poller = new TransactionPoller ( BLOCK_TRACKER_MOCK ) ;
152+ const poller = new TransactionPoller (
153+ BLOCK_TRACKER_MOCK ,
154+ MESSENGER_MOCK ,
155+ CHAIN_ID_MOCK ,
156+ ) ;
114157
115158 const listener = jest . fn ( ) ;
116159 poller . start ( listener ) ;
117160
118- for ( let i = 0 ; i < ACCELERATED_COUNT_MAX ; i ++ ) {
161+ for ( let i = 0 ; i < DEFAULT_ACCELERATED_COUNT_MAX ; i ++ ) {
119162 jest . runOnlyPendingTimers ( ) ;
120163 await flushPromises ( ) ;
121164 }
@@ -129,7 +172,11 @@ describe('TransactionPoller', () => {
129172
130173 describe ( 'start' , ( ) => {
131174 it ( 'does nothing if already started' , ( ) => {
132- const poller = new TransactionPoller ( BLOCK_TRACKER_MOCK ) ;
175+ const poller = new TransactionPoller (
176+ BLOCK_TRACKER_MOCK ,
177+ MESSENGER_MOCK ,
178+ CHAIN_ID_MOCK ,
179+ ) ;
133180
134181 poller . start ( jest . fn ( ) ) ;
135182 poller . start ( jest . fn ( ) ) ;
@@ -140,7 +187,11 @@ describe('TransactionPoller', () => {
140187
141188 describe ( 'stop' , ( ) => {
142189 it ( 'removes timeout' , ( ) => {
143- const poller = new TransactionPoller ( BLOCK_TRACKER_MOCK ) ;
190+ const poller = new TransactionPoller (
191+ BLOCK_TRACKER_MOCK ,
192+ MESSENGER_MOCK ,
193+ CHAIN_ID_MOCK ,
194+ ) ;
144195
145196 const listener = jest . fn ( ) ;
146197 poller . start ( listener ) ;
@@ -151,24 +202,32 @@ describe('TransactionPoller', () => {
151202 } ) ;
152203
153204 it ( 'removes block tracker listener' , async ( ) => {
154- const poller = new TransactionPoller ( BLOCK_TRACKER_MOCK ) ;
205+ const poller = new TransactionPoller (
206+ BLOCK_TRACKER_MOCK ,
207+ MESSENGER_MOCK ,
208+ CHAIN_ID_MOCK ,
209+ ) ;
155210
156211 const listener = jest . fn ( ) ;
157212 poller . start ( listener ) ;
158213
159- for ( let i = 0 ; i < ACCELERATED_COUNT_MAX ; i ++ ) {
214+ for ( let i = 0 ; i < DEFAULT_ACCELERATED_COUNT_MAX ; i ++ ) {
160215 jest . runOnlyPendingTimers ( ) ;
161216 await flushPromises ( ) ;
162217 }
163218
164219 poller . stop ( ) ;
165220
166221 expect ( BLOCK_TRACKER_MOCK . removeListener ) . toHaveBeenCalledTimes ( 1 ) ;
167- expect ( listener ) . toHaveBeenCalledTimes ( ACCELERATED_COUNT_MAX ) ;
222+ expect ( listener ) . toHaveBeenCalledTimes ( DEFAULT_ACCELERATED_COUNT_MAX ) ;
168223 } ) ;
169224
170225 it ( 'does nothing if not started' , async ( ) => {
171- const poller = new TransactionPoller ( BLOCK_TRACKER_MOCK ) ;
226+ const poller = new TransactionPoller (
227+ BLOCK_TRACKER_MOCK ,
228+ MESSENGER_MOCK ,
229+ CHAIN_ID_MOCK ,
230+ ) ;
172231
173232 poller . stop ( ) ;
174233
@@ -191,7 +250,11 @@ describe('TransactionPoller', () => {
191250 ] ) (
192251 'resets accelerated count if transaction IDs %s' ,
193252 async ( _title , newPendingTransactions ) => {
194- const poller = new TransactionPoller ( BLOCK_TRACKER_MOCK ) ;
253+ const poller = new TransactionPoller (
254+ BLOCK_TRACKER_MOCK ,
255+ MESSENGER_MOCK ,
256+ CHAIN_ID_MOCK ,
257+ ) ;
195258
196259 poller . setPendingTransactions ( [
197260 createTransactionMetaMock ( '1' ) ,
@@ -208,12 +271,14 @@ describe('TransactionPoller', () => {
208271
209272 poller . setPendingTransactions ( newPendingTransactions ) ;
210273
211- for ( let i = 0 ; i < ACCELERATED_COUNT_MAX ; i ++ ) {
274+ for ( let i = 0 ; i < DEFAULT_ACCELERATED_COUNT_MAX ; i ++ ) {
212275 jest . runOnlyPendingTimers ( ) ;
213276 await flushPromises ( ) ;
214277 }
215278
216- expect ( listener ) . toHaveBeenCalledTimes ( ACCELERATED_COUNT_MAX + 3 ) ;
279+ expect ( listener ) . toHaveBeenCalledTimes (
280+ DEFAULT_ACCELERATED_COUNT_MAX + 3 ,
281+ ) ;
217282 } ,
218283 ) ;
219284
@@ -230,7 +295,11 @@ describe('TransactionPoller', () => {
230295 ] ) (
231296 'resets to accelerated polling if transaction IDs added' ,
232297 async ( _title , newPendingTransactions ) => {
233- const poller = new TransactionPoller ( BLOCK_TRACKER_MOCK ) ;
298+ const poller = new TransactionPoller (
299+ BLOCK_TRACKER_MOCK ,
300+ MESSENGER_MOCK ,
301+ CHAIN_ID_MOCK ,
302+ ) ;
234303
235304 poller . setPendingTransactions ( [
236305 createTransactionMetaMock ( '1' ) ,
@@ -240,7 +309,7 @@ describe('TransactionPoller', () => {
240309 const listener = jest . fn ( ) ;
241310 poller . start ( listener ) ;
242311
243- for ( let i = 0 ; i < ACCELERATED_COUNT_MAX ; i ++ ) {
312+ for ( let i = 0 ; i < DEFAULT_ACCELERATED_COUNT_MAX ; i ++ ) {
244313 jest . runOnlyPendingTimers ( ) ;
245314 await flushPromises ( ) ;
246315 }
@@ -253,12 +322,14 @@ describe('TransactionPoller', () => {
253322
254323 poller . setPendingTransactions ( newPendingTransactions ) ;
255324
256- for ( let i = 0 ; i < ACCELERATED_COUNT_MAX ; i ++ ) {
325+ for ( let i = 0 ; i < DEFAULT_ACCELERATED_COUNT_MAX ; i ++ ) {
257326 jest . runOnlyPendingTimers ( ) ;
258327 await flushPromises ( ) ;
259328 }
260329
261- expect ( listener ) . toHaveBeenCalledTimes ( ACCELERATED_COUNT_MAX * 2 + 2 ) ;
330+ expect ( listener ) . toHaveBeenCalledTimes (
331+ DEFAULT_ACCELERATED_COUNT_MAX * 2 + 2 ,
332+ ) ;
262333 } ,
263334 ) ;
264335 } ) ;
0 commit comments