|
| 1 | +import React from 'react'; |
| 2 | +import { merge } from 'lodash'; |
| 3 | +import { Hex } from '@metamask/utils'; |
| 4 | + |
| 5 | +import renderWithProvider from '../../../../../../../util/test/renderWithProvider'; |
| 6 | +import { transferConfirmationState } from '../../../../../../../util/test/confirm-data-helpers'; |
| 7 | +import { MMM_ORIGIN } from '../../../../constants/confirmations'; |
| 8 | +import NetworkRow from './network-row'; |
| 9 | + |
| 10 | +jest.mock('../../../../hooks/metrics/useConfirmationMetricEvents'); |
| 11 | +jest.mock('../../../../../../../core/Engine', () => ({ |
| 12 | + context: { |
| 13 | + GasFeeController: { |
| 14 | + startPolling: jest.fn(), |
| 15 | + stopPollingByPollingToken: jest.fn(), |
| 16 | + }, |
| 17 | + NetworkController: { |
| 18 | + getNetworkConfigurationByNetworkClientId: jest.fn(), |
| 19 | + }, |
| 20 | + TokenListController: { |
| 21 | + fetchTokenList: jest.fn(), |
| 22 | + }, |
| 23 | + }, |
| 24 | +})); |
| 25 | + |
| 26 | +const MOCK_DAPP_ORIGIN = 'https://exampledapp.com'; |
| 27 | + |
| 28 | +const mockNetworkImage = { uri: 'https://mocknetwork.com/image.png' }; |
| 29 | +jest.mock('../../../../../../../util/networks', () => ({ |
| 30 | + getNetworkImageSource: jest.fn(() => mockNetworkImage), |
| 31 | +})); |
| 32 | + |
| 33 | +const networkConfigurationMock = { |
| 34 | + name: 'Test Network', |
| 35 | + chainId: '0x1' as Hex, |
| 36 | +}; |
| 37 | + |
| 38 | +// Mock state with transaction from MetaMask Mobile origin |
| 39 | +const internalTransactionState = merge({}, transferConfirmationState, { |
| 40 | + engine: { |
| 41 | + backgroundState: { |
| 42 | + TransactionController: { |
| 43 | + transactions: [ |
| 44 | + { |
| 45 | + txParams: { |
| 46 | + from: '0xdc47789de4ceff0e8fe9d15d728af7f17550c164', |
| 47 | + }, |
| 48 | + origin: MMM_ORIGIN, |
| 49 | + }, |
| 50 | + ], |
| 51 | + }, |
| 52 | + NetworkController: { |
| 53 | + networkConfigurationsByChainId: { |
| 54 | + '0x1': networkConfigurationMock, |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | +}); |
| 60 | + |
| 61 | +// Mock state with transaction from external dapp origin |
| 62 | +const dappOriginTransactionState = merge({}, transferConfirmationState, { |
| 63 | + engine: { |
| 64 | + backgroundState: { |
| 65 | + TransactionController: { |
| 66 | + transactions: [ |
| 67 | + { |
| 68 | + txParams: { |
| 69 | + from: '0xdc47789de4ceff0e8fe9d15d728af7f17550c164', |
| 70 | + }, |
| 71 | + origin: MOCK_DAPP_ORIGIN, |
| 72 | + }, |
| 73 | + ], |
| 74 | + }, |
| 75 | + NetworkController: { |
| 76 | + networkConfigurationsByChainId: { |
| 77 | + '0x1': networkConfigurationMock, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | +}); |
| 83 | + |
| 84 | +describe('NetworkRow', () => { |
| 85 | + it('displays the correct network name', async () => { |
| 86 | + const { getByText } = renderWithProvider(<NetworkRow />, { |
| 87 | + state: internalTransactionState, |
| 88 | + }); |
| 89 | + |
| 90 | + expect(getByText('Test Network')).toBeDefined(); |
| 91 | + expect(getByText('Network')).toBeDefined(); |
| 92 | + }); |
| 93 | + |
| 94 | + it('displays origin info for dapp transactions', async () => { |
| 95 | + const { getByText } = renderWithProvider(<NetworkRow />, { |
| 96 | + state: dappOriginTransactionState, |
| 97 | + }); |
| 98 | + |
| 99 | + expect(getByText('Test Network')).toBeDefined(); |
| 100 | + expect(getByText('Request from')).toBeDefined(); |
| 101 | + expect(getByText(MOCK_DAPP_ORIGIN)).toBeDefined(); |
| 102 | + }); |
| 103 | + |
| 104 | + it('does not display origin info for internal transactions', async () => { |
| 105 | + const { getByText, queryByText } = renderWithProvider(<NetworkRow />, { |
| 106 | + state: internalTransactionState, |
| 107 | + }); |
| 108 | + |
| 109 | + expect(getByText('Test Network')).toBeDefined(); |
| 110 | + expect(queryByText('Request from')).toBeNull(); |
| 111 | + expect(queryByText(MMM_ORIGIN)).toBeNull(); |
| 112 | + }); |
| 113 | +}); |
0 commit comments