From 8ca0d24f8251ecc6ab915ebaae6929b060d07b5f Mon Sep 17 00:00:00 2001
From: Daniel <80175477+dan437@users.noreply.github.com>
Date: Fri, 2 Jul 2021 15:48:34 +0200
Subject: [PATCH] Increase Jest unit test coverage for the Swaps feature to
~60% (#11343)
---
jest.config.js | 8 +-
test/jest/mock-store.js | 132 ++++++++++++++-
ui/__mocks__/react-router-dom.js | 4 +-
.../ui/button-group/button-group.component.js | 1 +
.../page-container-header.component.js | 1 +
.../awaiting-signatures.test.js.snap | 26 ---
.../awaiting-signatures.test.js | 2 +-
.../__snapshots__/awaiting-swap.test.js.snap | 26 ---
ui/pages/swaps/awaiting-swap/awaiting-swap.js | 5 +-
.../swaps/awaiting-swap/awaiting-swap.test.js | 2 +-
.../__snapshots__/build-quote.test.js.snap | 34 +---
.../swaps/build-quote/build-quote.test.js | 1 -
.../__snapshots__/fee-card.test.js.snap | 1 +
ui/pages/swaps/fee-card/fee-card.js | 10 +-
.../loading-swaps-quotes.test.js | 41 +++++
.../main-quote-summary.test.js.snap | 2 +
.../main-quote-summary/main-quote-summary.js | 10 +-
.../slippage-buttons.test.js.snap | 3 +
...customization-modal.component.test.js.snap | 136 ++++++++++++++++
...customization-modal.container.test.js.snap | 98 ++++++++++++
...swaps-gas-customization-modal.component.js | 15 +-
...-gas-customization-modal.component.test.js | 81 ++++++++++
...-gas-customization-modal.container.test.js | 34 ++++
.../__snapshots__/view-quote.test.js.snap | 150 ++++++++++++++++++
ui/pages/swaps/view-quote/view-quote.test.js | 52 ++++++
25 files changed, 772 insertions(+), 103 deletions(-)
delete mode 100644 ui/pages/swaps/awaiting-signatures/__snapshots__/awaiting-signatures.test.js.snap
create mode 100644 ui/pages/swaps/loading-swaps-quotes/loading-swaps-quotes.test.js
create mode 100644 ui/pages/swaps/swaps-gas-customization-modal/__snapshots__/swaps-gas-customization-modal.component.test.js.snap
create mode 100644 ui/pages/swaps/swaps-gas-customization-modal/__snapshots__/swaps-gas-customization-modal.container.test.js.snap
create mode 100644 ui/pages/swaps/swaps-gas-customization-modal/swaps-gas-customization-modal.component.test.js
create mode 100644 ui/pages/swaps/swaps-gas-customization-modal/swaps-gas-customization-modal.container.test.js
create mode 100644 ui/pages/swaps/view-quote/__snapshots__/view-quote.test.js.snap
create mode 100644 ui/pages/swaps/view-quote/view-quote.test.js
diff --git a/jest.config.js b/jest.config.js
index 8e215b994..3219d5eba 100644
--- a/jest.config.js
+++ b/jest.config.js
@@ -5,10 +5,10 @@ module.exports = {
coveragePathIgnorePatterns: ['.stories.js', '.snap'],
coverageThreshold: {
global: {
- branches: 32.75,
- functions: 42.9,
- lines: 43.12,
- statements: 43.67,
+ branches: 45.45,
+ functions: 55.29,
+ lines: 60.22,
+ statements: 60.43,
},
},
setupFiles: ['./test/setup.js', './test/env.js'],
diff --git a/test/jest/mock-store.js b/test/jest/mock-store.js
index f09999f10..35e52682f 100644
--- a/test/jest/mock-store.js
+++ b/test/jest/mock-store.js
@@ -4,7 +4,19 @@ export const createSwapsMockStore = () => {
return {
swaps: {
customGas: {
+ limit: '0x0',
fallBackPrice: 5,
+ priceEstimates: {
+ blockTime: 14.1,
+ safeLow: 2.5,
+ safeLowWait: 6.6,
+ average: 4,
+ avgWait: 5.3,
+ fast: 5,
+ fastWait: 3.3,
+ fastest: 10,
+ fastestWait: 0.5,
+ },
},
fromToken: 'ETH',
},
@@ -84,7 +96,108 @@ export const createSwapsMockStore = () => {
},
],
swapsState: {
- quotes: {},
+ quotes: {
+ TEST_AGG_1: {
+ trade: {
+ from: '0xe18035bf8712672935fdb4e5e431b1a0183d2dfc',
+ value: '0x0',
+ gas: '0x61a80', // 4e5
+ to: '0x881D40237659C251811CEC9c364ef91dC08D300C',
+ },
+ sourceAmount: '10000000000000000000', // 10e18
+ destinationAmount: '20000000000000000000', // 20e18
+ error: null,
+ sourceToken: '0x6b175474e89094c44da98b954eedeac495271d0f',
+ destinationToken: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
+ approvalNeeded: null,
+ maxGas: 600000,
+ averageGas: 120000,
+ estimatedRefund: 80000,
+ fetchTime: 607,
+ aggregator: 'TEST_AGG_1',
+ aggType: 'AGG',
+ slippage: 2,
+ sourceTokenInfo: {
+ address: '0x6b175474e89094c44da98b954eedeac495271d0f',
+ symbol: 'DAI',
+ decimals: 18,
+ iconUrl: 'https://foo.bar/logo.png',
+ },
+ destinationTokenInfo: {
+ address: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
+ symbol: 'USDC',
+ decimals: 18,
+ },
+ fee: 1,
+ },
+
+ TEST_AGG_BEST: {
+ trade: {
+ from: '0xe18035bf8712672935fdb4e5e431b1a0183d2dfc',
+ value: '0x0',
+ gas: '0x61a80',
+ to: '0x881D40237659C251811CEC9c364ef91dC08D300C',
+ },
+ sourceAmount: '10000000000000000000',
+ destinationAmount: '25000000000000000000', // 25e18
+ error: null,
+ sourceToken: '0x6b175474e89094c44da98b954eedeac495271d0f',
+ destinationToken: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
+ approvalNeeded: null,
+ maxGas: 1100000,
+ averageGas: 411000,
+ estimatedRefund: 343090,
+ fetchTime: 1003,
+ aggregator: 'TEST_AGG_BEST',
+ aggType: 'AGG',
+ slippage: 2,
+ sourceTokenInfo: {
+ address: '0x6b175474e89094c44da98b954eedeac495271d0f',
+ symbol: 'DAI',
+ decimals: 18,
+ iconUrl: 'https://foo.bar/logo.png',
+ },
+ destinationTokenInfo: {
+ address: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
+ symbol: 'USDC',
+ decimals: 18,
+ },
+ fee: 1,
+ },
+ TEST_AGG_2: {
+ trade: {
+ from: '0xe18035bf8712672935fdb4e5e431b1a0183d2dfc',
+ value: '0x0',
+ gas: '0x61a80',
+ to: '0x881D40237659C251811CEC9c364ef91dC08D300C',
+ },
+ sourceAmount: '10000000000000000000',
+ destinationAmount: '22000000000000000000', // 22e18
+ error: null,
+ sourceToken: '0x6b175474e89094c44da98b954eedeac495271d0f',
+ destinationToken: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
+ approvalNeeded: null,
+ maxGas: 368000,
+ averageGas: 197000,
+ estimatedRefund: 18205,
+ fetchTime: 1354,
+ aggregator: 'TEST_AGG_2',
+ aggType: 'AGG',
+ slippage: 2,
+ sourceTokenInfo: {
+ address: '0x6b175474e89094c44da98b954eedeac495271d0f',
+ symbol: 'DAI',
+ decimals: 18,
+ iconUrl: 'https://foo.bar/logo.png',
+ },
+ destinationTokenInfo: {
+ address: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
+ symbol: 'USDC',
+ decimals: 18,
+ },
+ fee: 1,
+ },
+ },
fetchParams: {
metaData: {
sourceTokenInfo: {
@@ -97,10 +210,11 @@ export const createSwapsMockStore = () => {
},
tradeTxId: null,
approveTxId: null,
- quotesLastFetched: null,
+ quotesLastFetched: 1519211809934,
+ swapsQuoteRefreshTime: 60000,
customMaxGas: '',
customGasPrice: null,
- selectedAggId: null,
+ selectedAggId: 'TEST_AGG_2',
customApproveTxData: '',
errorKey: '',
topAggId: null,
@@ -108,5 +222,17 @@ export const createSwapsMockStore = () => {
swapsFeatureIsLive: false,
},
},
+ appState: {
+ modal: {
+ open: true,
+ modalState: {
+ name: 'test',
+ props: {
+ initialGasLimit: 100,
+ minimumGasLimit: 5,
+ },
+ },
+ },
+ },
};
};
diff --git a/ui/__mocks__/react-router-dom.js b/ui/__mocks__/react-router-dom.js
index dd15434a1..1f65e3395 100644
--- a/ui/__mocks__/react-router-dom.js
+++ b/ui/__mocks__/react-router-dom.js
@@ -2,7 +2,9 @@ const originalModule = jest.requireActual('react-router-dom');
module.exports = {
...originalModule,
- useHistory: jest.fn(),
+ useHistory: jest.fn(() => {
+ return [];
+ }),
useLocation: jest.fn(() => {
return {
pathname: '/swaps/build-quote',
diff --git a/ui/components/ui/button-group/button-group.component.js b/ui/components/ui/button-group/button-group.component.js
index d1f20e961..9cd5d9896 100644
--- a/ui/components/ui/button-group/button-group.component.js
+++ b/ui/components/ui/button-group/button-group.component.js
@@ -61,6 +61,7 @@ export default class ButtonGroup extends PureComponent {
index === this.state.activeButtonIndex,
},
)}
+ data-testid={`button-group__button${index}`}
onClick={() => {
this.handleButtonClick(index);
child.props.onClick?.();
diff --git a/ui/components/ui/page-container/page-container-header/page-container-header.component.js b/ui/components/ui/page-container/page-container-header/page-container-header.component.js
index 3717b7450..0c3995c10 100644
--- a/ui/components/ui/page-container/page-container-header/page-container-header.component.js
+++ b/ui/components/ui/page-container/page-container-header/page-container-header.component.js
@@ -61,6 +61,7 @@ export default class PageContainerHeader extends Component {
className={classnames('page-container__header', className, {
'page-container__header--no-padding-bottom': Boolean(tabs),
})}
+ data-testid="page-container__header"
>
{this.renderHeaderRow()}
diff --git a/ui/pages/swaps/awaiting-signatures/__snapshots__/awaiting-signatures.test.js.snap b/ui/pages/swaps/awaiting-signatures/__snapshots__/awaiting-signatures.test.js.snap
deleted file mode 100644
index a7b8b3546..000000000
--- a/ui/pages/swaps/awaiting-signatures/__snapshots__/awaiting-signatures.test.js.snap
+++ /dev/null
@@ -1,26 +0,0 @@
-// Jest Snapshot v1, https://goo.gl/fbAQLP
-
-exports[`AwaitingSignatures renders the component with initial props for 1 confirmation 1`] = `
-
-`;
diff --git a/ui/pages/swaps/awaiting-signatures/awaiting-signatures.test.js b/ui/pages/swaps/awaiting-signatures/awaiting-signatures.test.js
index c8e466ec2..bcf1d37ad 100644
--- a/ui/pages/swaps/awaiting-signatures/awaiting-signatures.test.js
+++ b/ui/pages/swaps/awaiting-signatures/awaiting-signatures.test.js
@@ -12,6 +12,6 @@ describe('AwaitingSignatures', () => {
const store = configureMockStore()(createSwapsMockStore());
const { getByText } = renderWithProvider(, store);
expect(getByText('Confirm with your hardware wallet')).toBeInTheDocument();
- expect(document.querySelector('.swaps-footer')).toMatchSnapshot();
+ expect(getByText('Cancel')).toBeInTheDocument();
});
});
diff --git a/ui/pages/swaps/awaiting-swap/__snapshots__/awaiting-swap.test.js.snap b/ui/pages/swaps/awaiting-swap/__snapshots__/awaiting-swap.test.js.snap
index 588a27dd9..c0c43246c 100644
--- a/ui/pages/swaps/awaiting-swap/__snapshots__/awaiting-swap.test.js.snap
+++ b/ui/pages/swaps/awaiting-swap/__snapshots__/awaiting-swap.test.js.snap
@@ -17,29 +17,3 @@ exports[`AwaitingSwap renders the component with initial props 1`] = `
`;
-
-exports[`AwaitingSwap renders the component with initial props 2`] = `
-
-`;
diff --git a/ui/pages/swaps/awaiting-swap/awaiting-swap.js b/ui/pages/swaps/awaiting-swap/awaiting-swap.js
index 75f7a07b5..e1a9e934d 100644
--- a/ui/pages/swaps/awaiting-swap/awaiting-swap.js
+++ b/ui/pages/swaps/awaiting-swap/awaiting-swap.js
@@ -130,9 +130,10 @@ export default function AwaitingSwap({
{ blockExplorerUrl: baseNetworkUrl },
);
- const isCustomBlockExplorerUrl =
+ const isCustomBlockExplorerUrl = Boolean(
SWAPS_CHAINID_DEFAULT_BLOCK_EXPLORER_URL_MAP[chainId] ||
- rpcPrefs.blockExplorerUrl;
+ rpcPrefs.blockExplorerUrl,
+ );
let headerText;
let statusImage;
diff --git a/ui/pages/swaps/awaiting-swap/awaiting-swap.test.js b/ui/pages/swaps/awaiting-swap/awaiting-swap.test.js
index bdd91c871..4f4286995 100644
--- a/ui/pages/swaps/awaiting-swap/awaiting-swap.test.js
+++ b/ui/pages/swaps/awaiting-swap/awaiting-swap.test.js
@@ -32,6 +32,6 @@ describe('AwaitingSwap', () => {
expect(
document.querySelector('.awaiting-swap__main-descrption'),
).toMatchSnapshot();
- expect(document.querySelector('.swaps-footer')).toMatchSnapshot();
+ expect(getByText('View in activity')).toBeInTheDocument();
});
});
diff --git a/ui/pages/swaps/build-quote/__snapshots__/build-quote.test.js.snap b/ui/pages/swaps/build-quote/__snapshots__/build-quote.test.js.snap
index c6f8bda5e..844710b12 100644
--- a/ui/pages/swaps/build-quote/__snapshots__/build-quote.test.js.snap
+++ b/ui/pages/swaps/build-quote/__snapshots__/build-quote.test.js.snap
@@ -8,6 +8,7 @@ exports[`BuildQuote renders the component with initial props 1`] = `