Skip to content

Commit

Permalink
add a test + bump test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
adonesky1 committed Oct 16, 2023
1 parent 34a90c7 commit 8a8b836
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 45 deletions.
8 changes: 4 additions & 4 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ module.exports = {
coverageReporters: ['text', 'html'],
coverageThreshold: {
global: {
branches: 77,
functions: 89,
lines: 92,
statements: 91,
branches: 78,
functions: 92.5,
lines: 94,
statements: 93.5,
},
},
moduleFileExtensions: ['js', 'json', 'jsx', 'ts', 'tsx', 'node'],
Expand Down
102 changes: 61 additions & 41 deletions src/SmartTransactionsController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,9 @@ const testHistory = [
];

const ethereumChainIdDec = parseInt(CHAIN_IDS.ETHEREUM, 16);
const goerliChainIdDec = parseInt(CHAIN_IDS.GOERLI, 16);

const trackMetaMetricsEventSpy = jest.fn();
const getNetworkClientByIdSpy = jest.fn();

const defaultState = {
smartTransactionsState: {
smartTransactions: {
Expand Down Expand Up @@ -307,7 +306,24 @@ describe('SmartTransactionsController', () => {
provider: jest.fn(),
confirmExternalTransaction: confirmExternalMock,
trackMetaMetricsEvent: trackMetaMetricsEventSpy,
getNetworkClientById: getNetworkClientByIdSpy,
getNetworkClientById: jest.fn().mockImplementation((networkClientId) => {
switch (networkClientId) {
case 'mainnet':
return {
configuration: {
chainId: CHAIN_IDS.ETHEREUM,
},
};
case 'goerli':
return {
configuration: {
chainId: CHAIN_IDS.GOERLI,
},
};
default:
throw new Error('Invalid network client id');
}
}),
});
// eslint-disable-next-line jest/prefer-spy-on
smartTransactionsController.subscribe = jest.fn();
Expand Down Expand Up @@ -490,25 +506,24 @@ describe('SmartTransactionsController', () => {
});

it('should add fee data to feesByChainId state using the networkClientId passed in to identify the appropriate chain', async () => {
const goerliChainIdDec = parseInt(CHAIN_IDS.GOERLI, 16);
getNetworkClientByIdSpy.mockImplementation((networkClientId) => {
switch (networkClientId) {
case 'mainnet':
return {
configuration: {
chainId: CHAIN_IDS.ETHEREUM,
},
};
case 'goerli':
return {
configuration: {
chainId: CHAIN_IDS.GOERLI,
},
};
default:
throw new Error('Invalid network client id');
}
});
// getNetworkClientByIdSpy.mockImplementation((networkClientId) => {
// switch (networkClientId) {
// case 'mainnet':
// return {
// configuration: {
// chainId: CHAIN_IDS.ETHEREUM,
// },
// };
// case 'goerli':
// return {
// configuration: {
// chainId: CHAIN_IDS.GOERLI,
// },
// };
// default:
// throw new Error('Invalid network client id');
// }
// });

const tradeTx = createUnsignedTransaction(goerliChainIdDec);
const approvalTx = createUnsignedTransaction(goerliChainIdDec);
Expand Down Expand Up @@ -681,6 +696,30 @@ describe('SmartTransactionsController', () => {
const liveness = await smartTransactionsController.fetchLiveness();
expect(liveness).toBe(true);
});

it('fetches liveness and sets in feesByChainId state for the Smart Transactions API for the chainId of the networkClientId passed in', async () => {
nock(API_BASE_URL)
.get(`/networks/${goerliChainIdDec}/health`)
.replyWithError('random error');

expect(
smartTransactionsController.state.smartTransactionsState
.livenessByChainId,
).toStrictEqual({
[CHAIN_IDS.ETHEREUM]: true,
[CHAIN_IDS.GOERLI]: true,
});

await smartTransactionsController.fetchLiveness('goerli');

expect(
smartTransactionsController.state.smartTransactionsState
.livenessByChainId,
).toStrictEqual({
[CHAIN_IDS.ETHEREUM]: true,
[CHAIN_IDS.GOERLI]: false,
});
});
});

describe('updateSmartTransaction', () => {
Expand Down Expand Up @@ -898,25 +937,6 @@ describe('SmartTransactionsController', () => {
},
});

getNetworkClientByIdSpy.mockImplementation((networkClientId) => {
switch (networkClientId) {
case 'mainnet':
return {
configuration: {
chainId: CHAIN_IDS.ETHEREUM,
},
};
case 'goerli':
return {
configuration: {
chainId: CHAIN_IDS.GOERLI,
},
};
default:
throw new Error('Invalid network client id');
}
});

jest.useFakeTimers();
const handleFetchSpy = jest.spyOn(utils, 'handleFetch');

Expand Down

0 comments on commit 8a8b836

Please sign in to comment.