|
| 1 | +import React from 'react'; |
| 2 | +import { fireEvent } from '@testing-library/react-native'; |
| 3 | +import { renderScreen } from '../../../util/test/renderWithProvider'; |
| 4 | +import { backgroundState } from '../../../util/test/initial-root-state'; |
| 5 | +import MultichainTransactionDetailsSheet from './MultichainTransactionDetailsSheet'; |
| 6 | +import type { MultichainTransactionDisplayData } from '../../hooks/useMultichainTransactionDisplay'; |
| 7 | +import type { Transaction } from '@metamask/keyring-api'; |
| 8 | + |
| 9 | +const mockNavigate = jest.fn(); |
| 10 | +const mockOnCloseBottomSheet = jest.fn(); |
| 11 | +const mockUseRoute = jest.fn(); |
| 12 | + |
| 13 | +jest.mock('@react-navigation/native', () => ({ |
| 14 | + ...jest.requireActual('@react-navigation/native'), |
| 15 | + useNavigation: () => ({ navigate: mockNavigate }), |
| 16 | + useRoute: () => mockUseRoute(), |
| 17 | +})); |
| 18 | + |
| 19 | +jest.mock( |
| 20 | + '../../../component-library/components/BottomSheets/BottomSheet', |
| 21 | + () => { |
| 22 | + const { forwardRef, useImperativeHandle } = jest.requireActual('react'); |
| 23 | + const { View } = jest.requireActual('react-native'); |
| 24 | + return { |
| 25 | + __esModule: true, |
| 26 | + default: forwardRef( |
| 27 | + ( |
| 28 | + { children }: { children: React.ReactNode }, |
| 29 | + ref: React.Ref<unknown>, |
| 30 | + ) => { |
| 31 | + useImperativeHandle(ref, () => ({ |
| 32 | + onCloseBottomSheet: mockOnCloseBottomSheet, |
| 33 | + })); |
| 34 | + return <View>{children}</View>; |
| 35 | + }, |
| 36 | + ), |
| 37 | + }; |
| 38 | + }, |
| 39 | +); |
| 40 | + |
| 41 | +jest.mock( |
| 42 | + '../../../component-library/components/BottomSheets/BottomSheetHeader', |
| 43 | + () => { |
| 44 | + const { View, TouchableOpacity, Text } = jest.requireActual('react-native'); |
| 45 | + return ({ |
| 46 | + children, |
| 47 | + onClose, |
| 48 | + }: { |
| 49 | + children: React.ReactNode; |
| 50 | + onClose: () => void; |
| 51 | + }) => ( |
| 52 | + <View> |
| 53 | + <TouchableOpacity testID="bottomsheet-close" onPress={onClose} /> |
| 54 | + {children} |
| 55 | + </View> |
| 56 | + ); |
| 57 | + }, |
| 58 | +); |
| 59 | + |
| 60 | +jest.mock('../../../core/Multichain/utils', () => ({ |
| 61 | + getTransactionUrl: jest.fn( |
| 62 | + (id: string, chain: string) => |
| 63 | + `https://explorer.example.com/tx/${id}?chain=${chain}`, |
| 64 | + ), |
| 65 | + getAddressUrl: jest.fn( |
| 66 | + (address: string, chain: string) => |
| 67 | + `https://explorer.example.com/address/${address}?chain=${chain}`, |
| 68 | + ), |
| 69 | +})); |
| 70 | + |
| 71 | +const mockDisplayData: MultichainTransactionDisplayData = { |
| 72 | + title: 'Send ETH', |
| 73 | + isRedeposit: false, |
| 74 | + from: { |
| 75 | + address: '0xabc123def456abc123def456abc123def456abc1', |
| 76 | + amount: '0.5', |
| 77 | + unit: 'ETH', |
| 78 | + }, |
| 79 | + to: { |
| 80 | + address: '0xdef456abc123def456abc123def456abc123def4', |
| 81 | + amount: '0.5', |
| 82 | + unit: 'ETH', |
| 83 | + }, |
| 84 | + baseFee: { amount: '0.001', unit: 'ETH' }, |
| 85 | + priorityFee: { amount: '0.0001', unit: 'ETH' }, |
| 86 | +}; |
| 87 | + |
| 88 | +const mockTransaction = { |
| 89 | + id: 'tx-hash-123', |
| 90 | + timestamp: 1700000000, |
| 91 | + chain: 'eip155:1', |
| 92 | + status: 'confirmed', |
| 93 | + account: '0xabc123def456abc123def456abc123def456abc1', |
| 94 | + type: 'send', |
| 95 | +} as unknown as Transaction; |
| 96 | + |
| 97 | +const renderSheet = () => |
| 98 | + renderScreen( |
| 99 | + MultichainTransactionDetailsSheet, |
| 100 | + { name: 'MultichainTransactionDetailsSheet' }, |
| 101 | + { state: { engine: { backgroundState } } }, |
| 102 | + ); |
| 103 | + |
| 104 | +const defaultRouteParams = { |
| 105 | + params: { displayData: mockDisplayData, transaction: mockTransaction }, |
| 106 | +}; |
| 107 | + |
| 108 | +describe('MultichainTransactionDetailsSheet', () => { |
| 109 | + beforeEach(() => { |
| 110 | + jest.clearAllMocks(); |
| 111 | + mockUseRoute.mockReturnValue(defaultRouteParams); |
| 112 | + }); |
| 113 | + |
| 114 | + it('renders the transaction title', () => { |
| 115 | + const { getByText } = renderSheet(); |
| 116 | + expect(getByText('Send ETH')).toBeTruthy(); |
| 117 | + }); |
| 118 | + |
| 119 | + it('renders the transaction status row', () => { |
| 120 | + const { getByTestId } = renderSheet(); |
| 121 | + expect( |
| 122 | + getByTestId(`transaction-status-${mockTransaction.id}`), |
| 123 | + ).toBeTruthy(); |
| 124 | + }); |
| 125 | + |
| 126 | + it('renders the from address row', () => { |
| 127 | + const { getByText } = renderSheet(); |
| 128 | + // formatAddress shortens it — just check the label exists |
| 129 | + expect(getByText('From')).toBeTruthy(); |
| 130 | + }); |
| 131 | + |
| 132 | + it('renders the to address row', () => { |
| 133 | + const { getByText } = renderSheet(); |
| 134 | + expect(getByText('To')).toBeTruthy(); |
| 135 | + }); |
| 136 | + |
| 137 | + it('renders the amount row', () => { |
| 138 | + const { getByText } = renderSheet(); |
| 139 | + expect(getByText('Amount')).toBeTruthy(); |
| 140 | + expect(getByText('0.5 ETH')).toBeTruthy(); |
| 141 | + }); |
| 142 | + |
| 143 | + it('renders the network fee row', () => { |
| 144 | + const { getByText } = renderSheet(); |
| 145 | + expect(getByText('Network fee')).toBeTruthy(); |
| 146 | + expect(getByText('0.001 ETH')).toBeTruthy(); |
| 147 | + }); |
| 148 | + |
| 149 | + it('renders the priority fee row', () => { |
| 150 | + const { getByText } = renderSheet(); |
| 151 | + expect(getByText('0.0001 ETH')).toBeTruthy(); |
| 152 | + }); |
| 153 | + |
| 154 | + it('closes the sheet when close button is pressed', () => { |
| 155 | + const { getByTestId } = renderSheet(); |
| 156 | + fireEvent.press(getByTestId('bottomsheet-close')); |
| 157 | + expect(mockOnCloseBottomSheet).toHaveBeenCalledTimes(1); |
| 158 | + }); |
| 159 | + |
| 160 | + it('renders without from/to/fees when they are absent', () => { |
| 161 | + mockUseRoute.mockReturnValue({ |
| 162 | + params: { |
| 163 | + displayData: { title: 'Receive' }, |
| 164 | + transaction: mockTransaction, |
| 165 | + }, |
| 166 | + }); |
| 167 | + const { getByText, queryByText } = renderSheet(); |
| 168 | + expect(getByText('Receive')).toBeTruthy(); |
| 169 | + expect(queryByText('From')).toBeNull(); |
| 170 | + expect(queryByText('To')).toBeNull(); |
| 171 | + expect(queryByText('Network fee')).toBeNull(); |
| 172 | + }); |
| 173 | +}); |
0 commit comments