Skip to content

Commit 8a8b836

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

File tree

2 files changed

+65
-45
lines changed

2 files changed

+65
-45
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: 61 additions & 41 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();
@@ -490,25 +506,24 @@ describe('SmartTransactionsController', () => {
490506
});
491507

492508
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-
});
509+
// getNetworkClientByIdSpy.mockImplementation((networkClientId) => {
510+
// switch (networkClientId) {
511+
// case 'mainnet':
512+
// return {
513+
// configuration: {
514+
// chainId: CHAIN_IDS.ETHEREUM,
515+
// },
516+
// };
517+
// case 'goerli':
518+
// return {
519+
// configuration: {
520+
// chainId: CHAIN_IDS.GOERLI,
521+
// },
522+
// };
523+
// default:
524+
// throw new Error('Invalid network client id');
525+
// }
526+
// });
512527

513528
const tradeTx = createUnsignedTransaction(goerliChainIdDec);
514529
const approvalTx = createUnsignedTransaction(goerliChainIdDec);
@@ -681,6 +696,30 @@ describe('SmartTransactionsController', () => {
681696
const liveness = await smartTransactionsController.fetchLiveness();
682697
expect(liveness).toBe(true);
683698
});
699+
700+
it('fetches liveness and sets in feesByChainId state for the Smart Transactions API for the chainId of the networkClientId passed in', async () => {
701+
nock(API_BASE_URL)
702+
.get(`/networks/${goerliChainIdDec}/health`)
703+
.replyWithError('random error');
704+
705+
expect(
706+
smartTransactionsController.state.smartTransactionsState
707+
.livenessByChainId,
708+
).toStrictEqual({
709+
[CHAIN_IDS.ETHEREUM]: true,
710+
[CHAIN_IDS.GOERLI]: true,
711+
});
712+
713+
await smartTransactionsController.fetchLiveness('goerli');
714+
715+
expect(
716+
smartTransactionsController.state.smartTransactionsState
717+
.livenessByChainId,
718+
).toStrictEqual({
719+
[CHAIN_IDS.ETHEREUM]: true,
720+
[CHAIN_IDS.GOERLI]: false,
721+
});
722+
});
684723
});
685724

686725
describe('updateSmartTransaction', () => {
@@ -898,25 +937,6 @@ describe('SmartTransactionsController', () => {
898937
},
899938
});
900939

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-
920940
jest.useFakeTimers();
921941
const handleFetchSpy = jest.spyOn(utils, 'handleFetch');
922942

0 commit comments

Comments
 (0)