|
| 1 | +import { fireEvent } from '@testing-library/react-native'; |
| 2 | +import { |
| 3 | + renderScreen, |
| 4 | + DeepPartial, |
| 5 | +} from '../../../../../util/test/renderWithProvider'; |
| 6 | +import QuoteDetailsCard from './QuoteDetailsCard'; |
| 7 | +import { strings } from '../../../../../../locales/i18n'; |
| 8 | +import Routes from '../../../../../constants/navigation/Routes'; |
| 9 | +import { defaultBridgeControllerState } from '../../../../../core/Engine/controllers/bridge-controller/constants'; |
| 10 | +import mockQuotes from '../../_mocks_/mock-quotes-native-erc20.json'; |
| 11 | +import { QuoteResponse } from '@metamask/bridge-controller'; |
| 12 | +import { mockNetworkState } from '../../../../../util/test/network'; |
| 13 | +import { RpcEndpointType } from '@metamask/network-controller'; |
| 14 | +import { mockBridgeReducerState } from '../../_mocks_/bridgeReducerState'; |
| 15 | +import initialRootState from '../../../../../util/test/initial-root-state'; |
| 16 | +import { RootState } from '../../../../../reducers'; |
| 17 | +import { ChainId } from '@metamask/controller-utils'; |
| 18 | +import { createMockInternalAccount } from '../../../../../util/test/accountsControllerTestUtils'; |
| 19 | + |
| 20 | +const mockNavigate = jest.fn(); |
| 21 | +jest.mock('@react-navigation/native', () => ({ |
| 22 | + ...jest.requireActual('@react-navigation/native'), |
| 23 | + useNavigation: () => ({ |
| 24 | + navigate: mockNavigate, |
| 25 | + }), |
| 26 | +})); |
| 27 | + |
| 28 | +describe('QuoteDetailsCard', () => { |
| 29 | + const mockAccount = createMockInternalAccount( |
| 30 | + '0x1234567890123456789012345678901234567890', |
| 31 | + 'Test Account', |
| 32 | + ); |
| 33 | + |
| 34 | + const initialState: DeepPartial<RootState> = { |
| 35 | + engine: { |
| 36 | + backgroundState: { |
| 37 | + ...initialRootState, |
| 38 | + BridgeController: { |
| 39 | + ...defaultBridgeControllerState, |
| 40 | + quotes: mockQuotes as unknown as QuoteResponse[], |
| 41 | + }, |
| 42 | + NetworkController: { |
| 43 | + ...mockNetworkState( |
| 44 | + { |
| 45 | + chainId: ChainId.mainnet, |
| 46 | + id: 'mainnet', |
| 47 | + nickname: 'Ethereum', |
| 48 | + ticker: 'ETH', |
| 49 | + type: RpcEndpointType.Infura, |
| 50 | + rpcUrl: 'https://eth-mainnet.alchemyapi.io/v2/demo', |
| 51 | + }, |
| 52 | + { |
| 53 | + chainId: ChainId['linea-mainnet'], |
| 54 | + id: 'linea', |
| 55 | + nickname: 'Linea', |
| 56 | + ticker: 'LINEA', |
| 57 | + type: RpcEndpointType.Custom, |
| 58 | + rpcUrl: 'https://linea-rpc.com', |
| 59 | + }, |
| 60 | + ), |
| 61 | + }, |
| 62 | + MultichainNetworkController: { |
| 63 | + multichainNetworkConfigurationsByChainId: {}, |
| 64 | + }, |
| 65 | + AccountsController: { |
| 66 | + internalAccounts: { |
| 67 | + accounts: { |
| 68 | + [mockAccount.id]: mockAccount, |
| 69 | + }, |
| 70 | + selectedAccount: mockAccount.id, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + bridge: mockBridgeReducerState, |
| 76 | + }; |
| 77 | + |
| 78 | + beforeEach(() => { |
| 79 | + jest.clearAllMocks(); |
| 80 | + }); |
| 81 | + |
| 82 | + it('renders initial state', () => { |
| 83 | + const { toJSON } = renderScreen( |
| 84 | + QuoteDetailsCard, |
| 85 | + { |
| 86 | + name: Routes.BRIDGE.ROOT, |
| 87 | + }, |
| 88 | + { state: initialState }, |
| 89 | + ); |
| 90 | + expect(toJSON()).toMatchSnapshot(); |
| 91 | + }); |
| 92 | + |
| 93 | + it('renders expanded state', () => { |
| 94 | + const { getByLabelText, toJSON } = renderScreen( |
| 95 | + QuoteDetailsCard, |
| 96 | + { |
| 97 | + name: Routes.BRIDGE.ROOT, |
| 98 | + }, |
| 99 | + { state: initialState }, |
| 100 | + ); |
| 101 | + |
| 102 | + // Expand the accordion |
| 103 | + const expandButton = getByLabelText('Expand quote details'); |
| 104 | + fireEvent.press(expandButton); |
| 105 | + |
| 106 | + expect(toJSON()).toMatchSnapshot(); |
| 107 | + }); |
| 108 | + |
| 109 | + it('displays fee amount', () => { |
| 110 | + const { getByText } = renderScreen( |
| 111 | + QuoteDetailsCard, |
| 112 | + { |
| 113 | + name: Routes.BRIDGE.ROOT, |
| 114 | + }, |
| 115 | + { state: initialState }, |
| 116 | + ); |
| 117 | + |
| 118 | + expect(getByText('$0.01')).toBeDefined(); |
| 119 | + }); |
| 120 | + |
| 121 | + it('displays processing time', () => { |
| 122 | + const { getByText } = renderScreen( |
| 123 | + QuoteDetailsCard, |
| 124 | + { |
| 125 | + name: Routes.BRIDGE.ROOT, |
| 126 | + }, |
| 127 | + { state: initialState }, |
| 128 | + ); |
| 129 | + |
| 130 | + expect(getByText('1 min')).toBeDefined(); |
| 131 | + }); |
| 132 | + |
| 133 | + it('displays quote rate', () => { |
| 134 | + const { getByText } = renderScreen( |
| 135 | + QuoteDetailsCard, |
| 136 | + { |
| 137 | + name: Routes.BRIDGE.ROOT, |
| 138 | + }, |
| 139 | + { state: initialState }, |
| 140 | + ); |
| 141 | + |
| 142 | + expect(getByText('1 ETH = 0.0 USDC')).toBeDefined(); |
| 143 | + }); |
| 144 | + |
| 145 | + it('toggles content visibility on chevron press', () => { |
| 146 | + const { getByLabelText, queryByText } = renderScreen( |
| 147 | + QuoteDetailsCard, |
| 148 | + { |
| 149 | + name: Routes.BRIDGE.ROOT, |
| 150 | + }, |
| 151 | + { state: initialState }, |
| 152 | + ); |
| 153 | + |
| 154 | + // Initially price impact should not be visible |
| 155 | + expect(queryByText(strings('bridge.price_impact'))).toBeNull(); |
| 156 | + |
| 157 | + // Press chevron to expand |
| 158 | + const expandButton = getByLabelText('Expand quote details'); |
| 159 | + fireEvent.press(expandButton); |
| 160 | + |
| 161 | + // After expansion, price impact should be visible |
| 162 | + expect(queryByText(strings('bridge.price_impact'))).toBeDefined(); |
| 163 | + expect(queryByText('-0.06%')).toBeDefined(); |
| 164 | + |
| 165 | + // Press chevron again to collapse |
| 166 | + fireEvent.press(expandButton); |
| 167 | + |
| 168 | + // After collapse, price impact should not be visible |
| 169 | + expect(queryByText(strings('bridge.price_impact'))).toBeNull(); |
| 170 | + }); |
| 171 | + |
| 172 | + it('navigates to slippage modal on edit press', () => { |
| 173 | + const { getByLabelText, getByTestId } = renderScreen( |
| 174 | + QuoteDetailsCard, |
| 175 | + { |
| 176 | + name: Routes.BRIDGE.ROOT, |
| 177 | + }, |
| 178 | + { state: initialState }, |
| 179 | + ); |
| 180 | + |
| 181 | + // Expand the accordion first |
| 182 | + const expandButton = getByLabelText('Expand quote details'); |
| 183 | + fireEvent.press(expandButton); |
| 184 | + |
| 185 | + // Find and press the edit button |
| 186 | + const editButton = getByTestId('edit-slippage-button'); |
| 187 | + fireEvent.press(editButton); |
| 188 | + |
| 189 | + // Check if navigation was called with correct params |
| 190 | + expect(mockNavigate).toHaveBeenCalledWith(Routes.BRIDGE.MODALS.ROOT, { |
| 191 | + screen: Routes.BRIDGE.MODALS.SLIPPAGE_MODAL, |
| 192 | + }); |
| 193 | + }); |
| 194 | + |
| 195 | + it('displays network names', () => { |
| 196 | + const { getByText } = renderScreen( |
| 197 | + QuoteDetailsCard, |
| 198 | + { |
| 199 | + name: Routes.BRIDGE.ROOT, |
| 200 | + }, |
| 201 | + { state: initialState }, |
| 202 | + ); |
| 203 | + |
| 204 | + expect(getByText('Ethereum')).toBeDefined(); |
| 205 | + expect(getByText('Linea')).toBeDefined(); |
| 206 | + }); |
| 207 | + |
| 208 | + it('displays slippage value', () => { |
| 209 | + const { getByLabelText, getByText } = renderScreen( |
| 210 | + QuoteDetailsCard, |
| 211 | + { |
| 212 | + name: Routes.BRIDGE.ROOT, |
| 213 | + }, |
| 214 | + { state: initialState }, |
| 215 | + ); |
| 216 | + |
| 217 | + // Expand the accordion first |
| 218 | + const expandButton = getByLabelText('Expand quote details'); |
| 219 | + fireEvent.press(expandButton); |
| 220 | + |
| 221 | + // Verify slippage value |
| 222 | + expect(getByText('0.5%')).toBeDefined(); |
| 223 | + }); |
| 224 | +}); |
0 commit comments