Skip to content

Commit 33cbb0d

Browse files
committed
refactor(5564): fix lint and unit/e2e tests
1 parent 020aa7e commit 33cbb0d

File tree

9 files changed

+44
-53
lines changed

9 files changed

+44
-53
lines changed

ui/pages/confirmations/components/confirm/smart-account-tab/smart-account-tab.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22
import { Hex } from '@metamask/utils';
33

44
import mockState from '../../../../../../test/data/mock-state.json';
5-
import { renderWithProvider } from '../../../../../../test/lib/render-helpers';
5+
import { renderWithProvider } from '../../../../../../test/lib/render-helpers-navigate';
66
import configureStore from '../../../../../store/store';
77
import { SmartAccountTab } from './smart-account-tab';
88

ui/pages/confirmations/confirm-transaction/confirm-transaction.component.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useCallback, useEffect, useState } from 'react';
2-
import PropTypes from 'prop-types';
2+
import PropTypes from 'prop-types';
33
import { useDispatch, useSelector } from 'react-redux';
44
import {
55
Routes,

ui/pages/confirmations/confirm-transaction/confirm-transaction.test.js

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import ReactRouterDOM from 'react-router-dom';
77
import * as ConfirmTransactionDucks from '../../../ducks/confirm-transaction/confirm-transaction.duck';
88
import * as Actions from '../../../store/actions';
99
import _mockState from '../../../../test/data/mock-state.json';
10-
import { renderWithProvider } from '../../../../test/lib/render-helpers';
10+
import { renderWithProvider } from '../../../../test/lib/render-helpers-navigate';
1111
import { setBackgroundConnection } from '../../../store/background-connection';
1212

1313
import {
@@ -53,22 +53,13 @@ jest.mock(
5353
}),
5454
);
5555

56-
jest.mock('react-router-dom', () => {
57-
const original = jest.requireActual('react-router-dom');
58-
return {
59-
...original,
60-
useHistory: () => ({
61-
replace: jest.fn(),
62-
}),
63-
};
64-
});
65-
56+
const mockUseNavigate = jest.fn();
57+
const mockUseSearchParams = jest.fn();
6658
jest.mock('react-router-dom-v5-compat', () => {
67-
const original = jest.requireActual('react-router-dom-v5-compat');
6859
return {
69-
...original,
70-
useNavigate: () => jest.fn(),
71-
useSearchParams: () => [new URLSearchParams(''), jest.fn()],
60+
...jest.requireActual('react-router-dom-v5-compat'),
61+
useNavigate: () => mockUseNavigate,
62+
useSearchParams: () => mockUseSearchParams(),
7263
};
7364
});
7465

@@ -109,10 +100,13 @@ jest.mock('../confirm-transaction-switch', () => {
109100

110101
describe('Confirmation Transaction Page', () => {
111102
beforeEach(() => {
103+
jest.clearAllMocks();
112104
jest
113105
.spyOn(Actions, 'gasFeeStartPollingByNetworkClientId')
114106
.mockResolvedValue(null);
107+
mockUseSearchParams.mockReturnValue([new URLSearchParams(''), jest.fn()]);
115108
});
109+
116110
it('should display the Loading component when the transaction is invalid', () => {
117111
const mockStore = configureMockStore(middleware)({
118112
...mockState,
@@ -190,7 +184,7 @@ describe('Confirmation Transaction Page', () => {
190184
...mockState,
191185
metamask: { ...mockState.metamask, transactions: [] },
192186
});
193-
jest.spyOn(ReactRouterDOM, 'useParams').mockImplementation(() => {
187+
mockUseSearchParams.mockImplementation(() => {
194188
return { id: null };
195189
});
196190
ConfirmTransactionDucks.setTransactionToConfirm.mockClear();
@@ -203,17 +197,10 @@ describe('Confirmation Transaction Page', () => {
203197
});
204198

205199
describe('when unapproved transactions exist or a sendTo recipient exists', () => {
206-
it('should not call history.replace(mostRecentOverviewPage)', () => {
200+
it('should not call navigate(mostRecentOverviewPage)', () => {
207201
const mockStore = configureMockStore(middleware)(mockState);
208-
const replaceSpy = jest.fn();
209-
jest.spyOn(ReactRouterDOM, 'useHistory').mockImplementation(() => {
210-
return {
211-
replace: replaceSpy,
212-
};
213-
});
214-
215202
renderWithProvider(<ConfirmTransaction />, mockStore);
216-
expect(replaceSpy).not.toHaveBeenCalled();
203+
expect(mockUseNavigate).not.toHaveBeenCalled();
217204
});
218205
});
219206
});

ui/pages/confirmations/confirmation/templates/create-named-snap-account.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { waitFor } from '@testing-library/react';
55

66
import { KeyringTypes } from '@metamask/keyring-controller';
77
import Confirmation from '../confirmation';
8-
import { renderWithProvider } from '../../../../../test/lib/render-helpers';
8+
import { renderWithProvider } from '../../../../../test/lib/render-helpers-navigate';
99
import { SNAP_MANAGE_ACCOUNTS_CONFIRMATION_TYPES } from '../../../../../shared/constants/app';
1010
import mockState from '../../../../../test/data/mock-state.json';
1111
import { ETH_EOA_METHODS } from '../../../../../shared/constants/eth-methods';

ui/pages/confirmations/confirmation/templates/error.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import thunk from 'redux-thunk';
44
import { waitFor } from '@testing-library/react';
55

66
import { ApprovalType } from '@metamask/controller-utils';
7-
import { renderWithProvider } from '../../../../../test/lib/render-helpers';
7+
import { renderWithProvider } from '../../../../../test/lib/render-helpers-navigate';
88
import Confirmation from '../confirmation';
99

1010
jest.mock('../../../../../shared/lib/fetch-with-cache');

ui/pages/confirmations/confirmation/templates/success.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import thunk from 'redux-thunk';
44
import { waitFor } from '@testing-library/react';
55

66
import { ApprovalType } from '@metamask/controller-utils';
7-
import { renderWithProvider } from '../../../../../test/lib/render-helpers';
7+
import { renderWithProvider } from '../../../../../test/lib/render-helpers-navigate';
88
import Confirmation from '../confirmation';
99

1010
jest.mock('../../../../../shared/lib/fetch-with-cache');

ui/pages/confirmations/hooks/syncConfirmPath.test.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import { unapprovedPersonalSignMsg } from '../../../../test/data/confirmations/p
44
import { renderHookWithConfirmContextProvider } from '../../../../test/lib/confirmations/render-helpers';
55
import syncConfirmPath from './syncConfirmPath';
66

7-
const mockHistoryReplace = jest.fn();
8-
9-
jest.mock('react-router-dom', () => ({
10-
...jest.requireActual('react-router-dom'),
11-
useHistory: () => ({ replace: mockHistoryReplace }),
12-
}));
7+
const mockUseNavigate = jest.fn();
8+
jest.mock('react-router-dom-v5-compat', () => {
9+
return {
10+
...jest.requireActual('react-router-dom-v5-compat'),
11+
useNavigate: () => mockUseNavigate,
12+
};
13+
});
1314

1415
const STATE_MOCK = {
1516
...mockState,
@@ -25,6 +26,10 @@ const STATE_MOCK = {
2526
};
2627

2728
describe('syncConfirmPath', () => {
29+
beforeEach(() => {
30+
jest.clearAllMocks();
31+
});
32+
2833
it('should execute correctly', () => {
2934
const result = renderHookWithConfirmContextProvider(
3035
() => syncConfirmPath(unapprovedPersonalSignMsg),
@@ -34,11 +39,13 @@ describe('syncConfirmPath', () => {
3439
});
3540

3641
it('should replace history route', () => {
37-
mockHistoryReplace.mockClear();
3842
renderHookWithConfirmContextProvider(
3943
() => syncConfirmPath(unapprovedPersonalSignMsg),
4044
STATE_MOCK,
4145
);
42-
expect(mockHistoryReplace).toHaveBeenCalled();
46+
expect(mockUseNavigate).toHaveBeenCalledWith(
47+
'/confirm-transaction/0050d5b0-c023-11ee-a0cb-3390a510a0ab/signature-request',
48+
{ replace: true },
49+
);
4350
});
4451
});

ui/pages/confirmations/hooks/useConfirmSendNavigation.test.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@ import { useConfirmSendNavigation } from './useConfirmSendNavigation';
66

77
const mockUseRedesignedSendFlow = jest.mocked(useRedesignedSendFlow);
88

9-
const mockHistory = {
10-
goBack: jest.fn(),
11-
push: jest.fn(),
12-
};
13-
14-
jest.mock('react-router-dom', () => ({
15-
...jest.requireActual('react-router-dom'),
16-
useHistory: () => mockHistory,
17-
}));
9+
const mockUseNavigate = jest.fn();
10+
jest.mock('react-router-dom-v5-compat', () => {
11+
return {
12+
...jest.requireActual('react-router-dom-v5-compat'),
13+
useNavigate: () => mockUseNavigate,
14+
};
15+
});
1816

1917
jest.mock('react-redux', () => ({
2018
...jest.requireActual('react-redux'),
@@ -60,7 +58,7 @@ describe('useConfirmSendNavigation', () => {
6058
const result = renderHook();
6159
result.navigateBackIfSend();
6260

63-
expect(mockHistory.goBack).not.toHaveBeenCalled();
61+
expect(mockUseNavigate).not.toHaveBeenCalled();
6462
});
6563

6664
it('navigates back when send redesign is enabled and confirmation is metamask simpleSend', () => {
@@ -72,7 +70,7 @@ describe('useConfirmSendNavigation', () => {
7270
const result = renderHook();
7371
result.navigateBackIfSend();
7472

75-
expect(mockHistory.goBack).toHaveBeenCalled();
73+
expect(mockUseNavigate).toHaveBeenCalledWith(-1);
7674
});
7775

7876
it('does not navigate back when send redesign is enabled but origin is not metamask', () => {
@@ -84,7 +82,7 @@ describe('useConfirmSendNavigation', () => {
8482
const result = renderHook();
8583
result.navigateBackIfSend();
8684

87-
expect(mockHistory.goBack).not.toHaveBeenCalled();
85+
expect(mockUseNavigate).not.toHaveBeenCalled();
8886
});
8987

9088
it('does not navigate back when send redesign is enabled but type is not simpleSend', () => {
@@ -96,7 +94,7 @@ describe('useConfirmSendNavigation', () => {
9694
const result = renderHook();
9795
result.navigateBackIfSend();
9896

99-
expect(mockHistory.goBack).not.toHaveBeenCalled();
97+
expect(mockUseNavigate).not.toHaveBeenCalled();
10098
});
10199

102100
it('does not navigate back when send redesign is enabled but both origin and type do not match', () => {
@@ -108,6 +106,6 @@ describe('useConfirmSendNavigation', () => {
108106
const result = renderHook();
109107
result.navigateBackIfSend();
110108

111-
expect(mockHistory.goBack).not.toHaveBeenCalled();
109+
expect(mockUseNavigate).not.toHaveBeenCalled();
112110
});
113111
});

ui/pages/confirmations/hooks/useConfirmationNavigation.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { useHistory, useLocation } from 'react-router-dom';
21
import { ApprovalType } from '@metamask/controller-utils';
32
import { Json } from '@metamask/utils';
43
import { ApprovalFlowState } from '@metamask/approval-controller';

0 commit comments

Comments
 (0)