Skip to content

Commit

Permalink
fix integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronfigueiredo committed Sep 6, 2024
1 parent 8e3b055 commit 098c276
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,27 +1,46 @@
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'),
submitRequestToBackground: jest.fn(),
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 = {
Expand Down Expand Up @@ -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(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 = {
Expand Down Expand Up @@ -112,7 +131,7 @@ const setupSubmitRequestToBackgroundMocks = (
};

describe('ERC721 Approve Confirmation', () => {
let useIsNFTMock;
let useIsNFTMock, useAssetDetailsMock, useApproveTokenSimulationMock;
beforeEach(() => {
jest.resetAllMocks();
setupSubmitRequestToBackgroundMocks();
Expand All @@ -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(() => {
Expand Down

0 comments on commit 098c276

Please sign in to comment.