Skip to content

Commit b4222f6

Browse files
committed
working on test
1 parent d2070ab commit b4222f6

File tree

4 files changed

+215
-37
lines changed

4 files changed

+215
-37
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"@metamask/base-controller": "^3.2.1",
3232
"@metamask/controller-utils": "^5.0.0",
3333
"@metamask/network-controller": "^13.0.0",
34-
"@metamask/polling-controller": "^0.1.0",
34+
"@metamask/polling-controller": "^0.2.0",
3535
"bignumber.js": "^9.0.1",
3636
"fast-json-patch": "^3.1.0",
3737
"lodash": "^4.17.21"

src/SmartTransactionsController.test.ts

Lines changed: 76 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ import SmartTransactionsController, {
66
import { API_BASE_URL, CHAIN_IDS } from './constants';
77
import { SmartTransaction, SmartTransactionStatuses } from './types';
88

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+
918
const confirmExternalMock = jest.fn();
1019

1120
jest.mock('@ethersproject/bytes', () => ({
@@ -30,6 +39,8 @@ jest.mock('@ethersproject/providers', () => ({
3039

3140
const addressFrom = '0x268392a24B6b093127E8581eAfbD1DA228bAdAe3';
3241

42+
const pollingInterval = 1000;
43+
3344
const createUnsignedTransaction = () => {
3445
return {
3546
from: addressFrom,
@@ -258,21 +269,24 @@ describe('SmartTransactionsController', () => {
258269
let networkListener: (networkState: NetworkState) => void;
259270

260271
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(),
264287
},
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+
);
276290
// eslint-disable-next-line jest/prefer-spy-on
277291
smartTransactionsController.subscribe = jest.fn();
278292
});
@@ -508,6 +522,54 @@ describe('SmartTransactionsController', () => {
508522
});
509523
});
510524

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+
511573
describe('fetchSmartTransactionsStatus', () => {
512574
beforeEach(() => {
513575
// eslint-disable-next-line jest/prefer-spy-on

src/SmartTransactionsController.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export default class SmartTransactionsController extends PollingControllerV1<
148148
},
149149
},
150150
};
151-
151+
this.setIntervalLength(this.config.interval || DEFAULT_INTERVAL);
152152
this.getNonceLock = getNonceLock;
153153
this.ethersProvider = new Web3Provider(provider);
154154
this.confirmExternalTransaction = confirmExternalTransaction;
@@ -170,6 +170,7 @@ export default class SmartTransactionsController extends PollingControllerV1<
170170
}
171171

172172
executePoll(networkClientId: string): Promise<void> {
173+
console.log('executePoll', networkClientId);
173174
// if this is going to be truly UI driven polling we shouldn't really reach here
174175
// with a networkClientId that is not supported, but for now I'll add a check in case
175176
// wondering if we should add some kind of predicate to the polling controller to check whether
@@ -465,7 +466,7 @@ export default class SmartTransactionsController extends PollingControllerV1<
465466
const params = new URLSearchParams({
466467
uuids: uuids.join(','),
467468
});
468-
469+
console.log('chainId', chainId);
469470
const url = `${getAPIRequestURL(
470471
APIType.BATCH_STATUS,
471472
chainId,

0 commit comments

Comments
 (0)