Skip to content

Commit 1a1bcbf

Browse files
committed
add a test + bump test coverage
1 parent 34a90c7 commit 1a1bcbf

File tree

2 files changed

+68
-46
lines changed

2 files changed

+68
-46
lines changed

jest.config.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ module.exports = {
66
coverageReporters: ['text', 'html'],
77
coverageThreshold: {
88
global: {
9-
branches: 77,
10-
functions: 89,
11-
lines: 92,
12-
statements: 91,
9+
branches: 78,
10+
functions: 92.5,
11+
lines: 94,
12+
statements: 93.5,
1313
},
1414
},
1515
moduleFileExtensions: ['js', 'json', 'jsx', 'ts', 'tsx', 'node'],

src/SmartTransactionsController.test.ts

Lines changed: 64 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,9 @@ const testHistory = [
258258
];
259259

260260
const ethereumChainIdDec = parseInt(CHAIN_IDS.ETHEREUM, 16);
261+
const goerliChainIdDec = parseInt(CHAIN_IDS.GOERLI, 16);
261262

262263
const trackMetaMetricsEventSpy = jest.fn();
263-
const getNetworkClientByIdSpy = jest.fn();
264-
265264
const defaultState = {
266265
smartTransactionsState: {
267266
smartTransactions: {
@@ -307,7 +306,24 @@ describe('SmartTransactionsController', () => {
307306
provider: jest.fn(),
308307
confirmExternalTransaction: confirmExternalMock,
309308
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+
}),
311327
});
312328
// eslint-disable-next-line jest/prefer-spy-on
313329
smartTransactionsController.subscribe = jest.fn();
@@ -402,6 +418,8 @@ describe('SmartTransactionsController', () => {
402418
});
403419

404420
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.
405423
it('calls fetchSmartTransactionsStatus if there are pending transactions', () => {
406424
const fetchSmartTransactionsStatusSpy = jest
407425
.spyOn(smartTransactionsController, 'fetchSmartTransactionsStatus')
@@ -490,25 +508,24 @@ describe('SmartTransactionsController', () => {
490508
});
491509

492510
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+
// });
512529

513530
const tradeTx = createUnsignedTransaction(goerliChainIdDec);
514531
const approvalTx = createUnsignedTransaction(goerliChainIdDec);
@@ -681,6 +698,30 @@ describe('SmartTransactionsController', () => {
681698
const liveness = await smartTransactionsController.fetchLiveness();
682699
expect(liveness).toBe(true);
683700
});
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+
});
684725
});
685726

686727
describe('updateSmartTransaction', () => {
@@ -867,7 +908,7 @@ describe('SmartTransactionsController', () => {
867908
});
868909

869910
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 () => {
871912
// mock this to a noop because it causes an extra fetch call to the API upon state changes
872913
jest
873914
.spyOn(smartTransactionsController, 'checkPoll')
@@ -898,25 +939,6 @@ describe('SmartTransactionsController', () => {
898939
},
899940
});
900941

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-
920942
jest.useFakeTimers();
921943
const handleFetchSpy = jest.spyOn(utils, 'handleFetch');
922944

0 commit comments

Comments
 (0)