From 098c276242bbd6134202a8a8200899e661254dd2 Mon Sep 17 00:00:00 2001 From: Pedro Figueiredo Date: Fri, 6 Sep 2024 16:23:46 +0100 Subject: [PATCH] fix integration test --- .../contract-interaction.test.tsx | 46 ++++++++++++++++--- .../transactions/erc20-approve.test.tsx | 35 +++++++++++++- 2 files changed, 74 insertions(+), 7 deletions(-) diff --git a/test/integration/confirmations/transactions/contract-interaction.test.tsx b/test/integration/confirmations/transactions/contract-interaction.test.tsx index 7d07dc31e7db..d949a7447639 100644 --- a/test/integration/confirmations/transactions/contract-interaction.test.tsx +++ b/test/integration/confirmations/transactions/contract-interaction.test.tsx @@ -1,20 +1,22 @@ -import { fireEvent, waitFor, within } from '@testing-library/react'; import { ApprovalType } from '@metamask/controller-utils'; -import nock from 'nock'; import { TransactionType } from '@metamask/transaction-controller'; -import mockMetaMaskState from '../../data/integration-init-state.json'; -import { integrationTestRender } from '../../../lib/render-helpers'; -import * as backgroundConnection from '../../../../ui/store/background-connection'; +import { fireEvent, waitFor, within } from '@testing-library/react'; +import nock from 'nock'; import { MetaMetricsEventCategory, - MetaMetricsEventName, MetaMetricsEventLocation, + MetaMetricsEventName, } from '../../../../shared/constants/metametrics'; +import * as backgroundConnection from '../../../../ui/store/background-connection'; +import { integrationTestRender } from '../../../lib/render-helpers'; +import mockMetaMaskState from '../../data/integration-init-state.json'; import { createMockImplementation, mock4byte } from '../../helpers'; import { getMaliciousUnapprovedTransaction, getUnapprovedTransaction, } from './transactionDataHelpers'; +import { useAssetDetails } from '../../../../ui/pages/confirmations/hooks/useAssetDetails'; +import { useApproveTokenSimulation } from '../../../../ui/pages/confirmations/components/confirm/info/approve/hooks/use-approve-token-simulation'; jest.mock('../../../../ui/store/background-connection', () => ({ ...jest.requireActual('../../../../ui/store/background-connection'), @@ -22,6 +24,23 @@ jest.mock('../../../../ui/store/background-connection', () => ({ callBackgroundMethod: jest.fn(), })); +jest.mock('../../../../ui/pages/confirmations/hooks/useAssetDetails', () => ({ + ...jest.requireActual( + '../../../../ui/pages/confirmations/hooks/useAssetDetails', + ), + useAssetDetails: jest.fn(), +})); + +jest.mock( + '../../../../ui/pages/confirmations/components/confirm/info/approve/hooks/use-approve-token-simulation', + () => ({ + ...jest.requireActual( + '../../../../ui/pages/confirmations/components/confirm/info/approve/hooks/use-approve-token-simulation', + ), + useApproveTokenSimulation: jest.fn(), + }), +); + const mockedBackgroundConnection = jest.mocked(backgroundConnection); const backgroundConnectionMocked = { @@ -142,11 +161,26 @@ const getMetaMaskStateWithMaliciousUnapprovedContractInteraction = ( }; describe('Contract Interaction Confirmation', () => { + let useAssetDetailsMock, useApproveTokenSimulationMock; beforeEach(() => { jest.resetAllMocks(); setupSubmitRequestToBackgroundMocks(); const MINT_NFT_HEX_SIG = '0x3b4b1381'; mock4byte(MINT_NFT_HEX_SIG); + useAssetDetailsMock = jest.fn().mockImplementation(() => ({ + decimals: 18, + userBalance: '1000000', + tokenSymbol: 'TST', + })); + (useAssetDetails as jest.Mock).mockImplementation(useAssetDetailsMock); + useApproveTokenSimulationMock = jest.fn().mockImplementation(() => ({ + spendingCap: '1000', + formattedSpendingCap: '1000', + value: '1000', + })); + (useApproveTokenSimulation as jest.Mock).mockImplementation( + useApproveTokenSimulationMock, + ); }); afterEach(() => { diff --git a/test/integration/confirmations/transactions/erc20-approve.test.tsx b/test/integration/confirmations/transactions/erc20-approve.test.tsx index 824ca80e0b84..64f601941daf 100644 --- a/test/integration/confirmations/transactions/erc20-approve.test.tsx +++ b/test/integration/confirmations/transactions/erc20-approve.test.tsx @@ -1,7 +1,9 @@ import { ApprovalType } from '@metamask/controller-utils'; import { waitFor } from '@testing-library/react'; import nock from 'nock'; +import { useApproveTokenSimulation } from '../../../../ui/pages/confirmations/components/confirm/info/approve/hooks/use-approve-token-simulation'; import { useIsNFT } from '../../../../ui/pages/confirmations/components/confirm/info/approve/hooks/use-is-nft'; +import { useAssetDetails } from '../../../../ui/pages/confirmations/hooks/useAssetDetails'; import * as backgroundConnection from '../../../../ui/store/background-connection'; import { integrationTestRender } from '../../../lib/render-helpers'; import mockMetaMaskState from '../../data/integration-init-state.json'; @@ -23,6 +25,23 @@ jest.mock( }), ); +jest.mock('../../../../ui/pages/confirmations/hooks/useAssetDetails', () => ({ + ...jest.requireActual( + '../../../../ui/pages/confirmations/hooks/useAssetDetails', + ), + useAssetDetails: jest.fn(), +})); + +jest.mock( + '../../../../ui/pages/confirmations/components/confirm/info/approve/hooks/use-approve-token-simulation', + () => ({ + ...jest.requireActual( + '../../../../ui/pages/confirmations/components/confirm/info/approve/hooks/use-approve-token-simulation', + ), + useApproveTokenSimulation: jest.fn(), + }), +); + const mockedBackgroundConnection = jest.mocked(backgroundConnection); const backgroundConnectionMocked = { @@ -112,7 +131,7 @@ const setupSubmitRequestToBackgroundMocks = ( }; describe('ERC721 Approve Confirmation', () => { - let useIsNFTMock; + let useIsNFTMock, useAssetDetailsMock, useApproveTokenSimulationMock; beforeEach(() => { jest.resetAllMocks(); setupSubmitRequestToBackgroundMocks(); @@ -122,6 +141,20 @@ describe('ERC721 Approve Confirmation', () => { .fn() .mockImplementation(() => ({ isNFT: false, decimals: '18' })); (useIsNFT as jest.Mock).mockImplementation(useIsNFTMock); + useAssetDetailsMock = jest.fn().mockImplementation(() => ({ + decimals: 18, + userBalance: '1000000', + tokenSymbol: 'TST', + })); + (useAssetDetails as jest.Mock).mockImplementation(useAssetDetailsMock); + useApproveTokenSimulationMock = jest.fn().mockImplementation(() => ({ + spendingCap: '1000', + formattedSpendingCap: '1000', + value: '1000', + })); + (useApproveTokenSimulation as jest.Mock).mockImplementation( + useApproveTokenSimulationMock, + ); }); afterEach(() => {