Skip to content

Commit 54c7ee9

Browse files
authored
fix: MUSD-151: Refactored useNavbar to allow render overrides (MetaMask#24171)
<!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until the template has been completely filled out, and PR status checks have passed at least once. --> ## **Description** ### Bug This PR fixes an app crash caused by the mUSD conversion flow.. When navigating away from the conversion screen, we didn't reject the transaction (`onReject`) which caused subsequent redirects to the redesigned confirmations to render the conversion screen and crash due to missing route params. ### Fix The mUSD conversion flow now extends `useNavbar` to ensure transactions are rejected when navigating away (e.g. via back button). ### Changes - Add render overrides options to `useNavbar` to allow experiences to customize their confirmations navbars while still inheriting behaviour (e.g. `onReject` called when clicking back button) <!-- Write a short description of the changes included in this pull request, also include relevant motivation and context. Have in mind the following questions: 1. What is the reason for the change? 2. What is the improvement/solution? --> ## **Changelog** <!-- If this PR is not End-User-Facing and should not show up in the CHANGELOG, you can choose to either: 1. Write `CHANGELOG entry: null` 2. Label with `no-changelog` If this PR is End-User-Facing, please write a short User-Facing description in the past tense like: `CHANGELOG entry: Added a new tab for users to see their NFTs` `CHANGELOG entry: Fixed a bug that was causing some NFTs to flicker` (This helps the Release Engineer do their job more quickly and accurately) --> CHANGELOG entry: fixed mUSD conversion flow not rejecting transactions when navigating away ## **Related issues** Fixes: [MUSD-151: Stale mUSD conversion transaction not cleared causing app crash](https://consensyssoftware.atlassian.net/browse/MUSD-151) ## **Manual testing steps** ```gherkin Feature: mUSD Conversion Navbar Scenario: user navigates back from mUSD conversion confirmation Given user is on the mUSD conversion confirmation screen When user taps the back button Then the pending mUSD conversion transaction is rejected And user returns to the previous screen ``` ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** <!-- [screenshots/recordings] --> https://github.com/user-attachments/assets/d178ecdb-e703-44eb-8402-bdbc1daa6be6 ### **After** <!-- [screenshots/recordings] --> https://github.com/user-attachments/assets/a481fc63-50e5-4f38-a103-7086f3df742f ## **Pre-merge author checklist** - [x] I’ve followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile Coding Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [x] I've completed the PR template to the best of my ability - [x] I’ve included tests if applicable - [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [x] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots. <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Introduces navbar render overrides and migrates mUSD conversion to a new hook using them, removes old navbar options, updates routes, and expands tests. > > - **Confirmations UI**: > - **Navbar overrides**: Extend `getNavbar` to accept `overrides` (`headerTitle`, `headerLeft(onBackPress)`, `headerTitleAlign`, `headerStyle` merged) with default center alignment. > - **Hook update**: `useNavbar(title, addBackButton, overrides)` now forwards overrides; `getEmptyNavHeader`/`useEmptyNavHeaderForConfirmations` aligned; tests added for overrides and defaults. > - **mUSD Conversion**: > - **New hook**: Add `useMusdConversionNavbar(chainId)` rendering `MUSD` icon with network `Badge` and a back `ButtonIcon`; passes `chainId` to `getNetworkImageSource`. > - **Integration**: `MusdConversionInfo` calls `useMusdConversionNavbar(outputChainId)` and retains `useAddToken`; tests added. > - **Cleanup**: Remove `Navbars/musdNavbarOptions.tsx` and its tests. > - **Navigation**: > - Use `useEmptyNavHeaderForConfirmations` for `Routes.FULL_SCREEN_CONFIRMATIONS.REDESIGNED_CONFIRMATIONS`. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 9eca8af. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent bf6cec9 commit 54c7ee9

12 files changed

Lines changed: 490 additions & 295 deletions

File tree

app/components/UI/Earn/Navbars/musdNavbarOptions.test.tsx

Lines changed: 0 additions & 108 deletions
This file was deleted.

app/components/UI/Earn/Navbars/musdNavbarOptions.tsx

Lines changed: 0 additions & 104 deletions
This file was deleted.
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import React from 'react';
2+
import { render, fireEvent } from '@testing-library/react-native';
3+
import { renderHook } from '@testing-library/react-hooks';
4+
import { CHAIN_IDS } from '@metamask/transaction-controller';
5+
import { useMusdConversionNavbar } from './useMusdConversionNavbar';
6+
import useNavbar from '../../../Views/confirmations/hooks/ui/useNavbar';
7+
import { strings } from '../../../../../locales/i18n';
8+
import { NavbarOverrides } from '../../../Views/confirmations/components/UI/navbar/navbar';
9+
import { getNetworkImageSource } from '../../../../util/networks';
10+
11+
jest.mock('../../../../../locales/i18n', () => ({
12+
strings: jest.fn((key: string) => key),
13+
}));
14+
15+
jest.mock('../../../Views/confirmations/hooks/ui/useNavbar');
16+
17+
jest.mock('../../../../util/networks', () => ({
18+
getNetworkImageSource: jest.fn(),
19+
}));
20+
21+
const mockUseNavbar = useNavbar as jest.MockedFunction<typeof useNavbar>;
22+
const mockStrings = strings as jest.MockedFunction<typeof strings>;
23+
const mockGetNetworkImageSource = getNetworkImageSource as jest.MockedFunction<
24+
typeof getNetworkImageSource
25+
>;
26+
27+
describe('useMusdConversionNavbar', () => {
28+
beforeEach(() => {
29+
jest.clearAllMocks();
30+
});
31+
32+
it('calls useNavbar with correct title and addBackButton parameters', () => {
33+
renderHook(() => useMusdConversionNavbar(CHAIN_IDS.MAINNET));
34+
35+
expect(mockUseNavbar).toHaveBeenCalledTimes(1);
36+
expect(mockStrings).toHaveBeenCalledWith(
37+
'earn.musd_conversion.convert_to_musd',
38+
);
39+
expect(mockUseNavbar).toHaveBeenCalledWith(
40+
'earn.musd_conversion.convert_to_musd',
41+
true,
42+
expect.objectContaining({
43+
headerTitle: expect.any(Function),
44+
headerLeft: expect.any(Function),
45+
}),
46+
);
47+
});
48+
49+
it('provides headerTitle override that renders mUSD icon with network badge', () => {
50+
let capturedOverrides: NavbarOverrides | undefined;
51+
mockUseNavbar.mockImplementation((_title, _addBackButton, overrides) => {
52+
capturedOverrides = overrides;
53+
});
54+
55+
renderHook(() => useMusdConversionNavbar(CHAIN_IDS.MAINNET));
56+
57+
expect(capturedOverrides?.headerTitle).toBeDefined();
58+
59+
const HeaderTitle = capturedOverrides?.headerTitle as React.FC;
60+
const { getByTestId, getByText } = render(<HeaderTitle />);
61+
62+
expect(getByTestId('musd-token-icon')).toBeOnTheScreen();
63+
expect(getByTestId('badge-wrapper-badge')).toBeOnTheScreen();
64+
expect(getByTestId('badgenetwork')).toBeOnTheScreen();
65+
expect(getByText('earn.musd_conversion.convert_to_musd')).toBeOnTheScreen();
66+
});
67+
68+
it('provides headerLeft override that renders back button', () => {
69+
let capturedOverrides: NavbarOverrides | undefined;
70+
mockUseNavbar.mockImplementation((_title, _addBackButton, overrides) => {
71+
capturedOverrides = overrides;
72+
});
73+
74+
renderHook(() => useMusdConversionNavbar(CHAIN_IDS.MAINNET));
75+
76+
expect(capturedOverrides?.headerLeft).toBeDefined();
77+
78+
const mockOnBackPress = jest.fn();
79+
const headerLeftFn = capturedOverrides?.headerLeft as (
80+
onBackPress: () => void,
81+
) => React.ReactNode;
82+
const HeaderLeft = () => <>{headerLeftFn(mockOnBackPress)}</>;
83+
84+
const { getByTestId } = render(<HeaderLeft />);
85+
86+
const backButton = getByTestId('button-icon');
87+
expect(backButton).toBeOnTheScreen();
88+
});
89+
90+
it('calls provided onBackPress when back button is pressed', () => {
91+
let capturedOverrides: NavbarOverrides | undefined;
92+
mockUseNavbar.mockImplementation((_title, _addBackButton, overrides) => {
93+
capturedOverrides = overrides;
94+
});
95+
96+
renderHook(() => useMusdConversionNavbar(CHAIN_IDS.MAINNET));
97+
98+
const mockOnBackPress = jest.fn();
99+
const headerLeftFn = capturedOverrides?.headerLeft as (
100+
onBackPress: () => void,
101+
) => React.ReactNode;
102+
const HeaderLeft = () => <>{headerLeftFn(mockOnBackPress)}</>;
103+
104+
const { getByTestId } = render(<HeaderLeft />);
105+
106+
const backButton = getByTestId('button-icon');
107+
fireEvent.press(backButton);
108+
109+
expect(mockOnBackPress).toHaveBeenCalledTimes(1);
110+
});
111+
112+
it('passes Linea chainId to getNetworkImageSource', () => {
113+
renderHook(() => useMusdConversionNavbar(CHAIN_IDS.LINEA_MAINNET));
114+
115+
expect(mockGetNetworkImageSource).toHaveBeenCalledWith({
116+
chainId: CHAIN_IDS.LINEA_MAINNET,
117+
});
118+
});
119+
120+
it('passes Mainnet chainId to getNetworkImageSource', () => {
121+
renderHook(() => useMusdConversionNavbar(CHAIN_IDS.MAINNET));
122+
123+
expect(mockGetNetworkImageSource).toHaveBeenCalledWith({
124+
chainId: CHAIN_IDS.MAINNET,
125+
});
126+
});
127+
});

0 commit comments

Comments
 (0)