|
| 1 | +import React from 'react' |
| 2 | +import { render, screen, within } from '@testing-library/react' |
| 3 | +import { createMemoryRouter, RouterProvider } from 'react-router-dom' |
| 4 | + |
| 5 | +import AddressList from './AddressList' |
| 6 | +import { MintlayerContext, BitcoinContext, SettingsContext } from '@Contexts' |
| 7 | + |
| 8 | +const renderWithProviders = ({ |
| 9 | + coinType = 'Mintlayer', |
| 10 | + search = '', |
| 11 | + networkType = 'testnet', |
| 12 | + mintlayer = {}, |
| 13 | + bitcoin = {}, |
| 14 | +} = {}) => { |
| 15 | + const { fetchingBalances = false, addressData = [] } = mintlayer |
| 16 | + |
| 17 | + const { formatedAddresses = [] } = bitcoin |
| 18 | + |
| 19 | + const routes = [ |
| 20 | + { |
| 21 | + path: '/addresses/:coinType', |
| 22 | + element: ( |
| 23 | + <SettingsContext.Provider value={{ networkType }}> |
| 24 | + <MintlayerContext.Provider value={{ fetchingBalances, addressData }}> |
| 25 | + <BitcoinContext.Provider value={{ formatedAddresses }}> |
| 26 | + <AddressList search={search} /> |
| 27 | + </BitcoinContext.Provider> |
| 28 | + </MintlayerContext.Provider> |
| 29 | + </SettingsContext.Provider> |
| 30 | + ), |
| 31 | + }, |
| 32 | + ] |
| 33 | + |
| 34 | + const router = createMemoryRouter(routes, { |
| 35 | + initialEntries: [`/addresses/${coinType}`], |
| 36 | + future: { v7_relativeSplatPath: true }, |
| 37 | + }) |
| 38 | + |
| 39 | + return render(<RouterProvider router={router} />) |
| 40 | +} |
| 41 | + |
| 42 | +describe('AddressList', () => { |
| 43 | + test('renders and sorts Mintlayer addresses by used, available, locked, tokens', () => { |
| 44 | + const mlAddresses = [ |
| 45 | + { |
| 46 | + id: 'tmt1qAAA', |
| 47 | + coin_balance: { decimal: 0.5 }, |
| 48 | + locked_coin_balance: { decimal: 0.1 }, |
| 49 | + tokens: [{ token_id: 'token-1', amount: { decimal: 1 } }], |
| 50 | + transaction_history: [], // used = false |
| 51 | + }, |
| 52 | + { |
| 53 | + id: 'tmt1qBBB', |
| 54 | + coin_balance: { decimal: 1.2 }, |
| 55 | + locked_coin_balance: { decimal: 0 }, |
| 56 | + tokens: [], |
| 57 | + transaction_history: [1], // used = true |
| 58 | + }, |
| 59 | + { |
| 60 | + id: 'tmt1qCCC', |
| 61 | + coin_balance: { decimal: 1.2 }, |
| 62 | + locked_coin_balance: { decimal: 0.5 }, |
| 63 | + tokens: [ |
| 64 | + { token_id: 'token-2', amount: { decimal: 5 } }, |
| 65 | + { token_id: 'token-3', amount: { decimal: 10 } }, |
| 66 | + ], |
| 67 | + transaction_history: [], // used = false |
| 68 | + }, |
| 69 | + ] |
| 70 | + |
| 71 | + renderWithProviders({ |
| 72 | + coinType: 'Mintlayer', |
| 73 | + search: '', |
| 74 | + mintlayer: { fetchingBalances: false, addressData: mlAddresses }, |
| 75 | + }) |
| 76 | + |
| 77 | + // Expect order: used first (BBB), then by available (CCC), then (AAA) |
| 78 | + const row0 = screen.getByTestId('address-row-0') |
| 79 | + const row1 = screen.getByTestId('address-row-1') |
| 80 | + const row2 = screen.getByTestId('address-row-2') |
| 81 | + |
| 82 | + expect(within(row0).getByTitle('tmt1qBBB')).toBeInTheDocument() |
| 83 | + expect(within(row1).getByTitle('tmt1qCCC')).toBeInTheDocument() |
| 84 | + expect(within(row2).getByTitle('tmt1qAAA')).toBeInTheDocument() |
| 85 | + }) |
| 86 | + |
| 87 | + test('filters Mintlayer addresses by token id substring', () => { |
| 88 | + const mlAddresses = [ |
| 89 | + { |
| 90 | + id: 'tmt1qADDR1', |
| 91 | + coin_balance: { decimal: 0.1 }, |
| 92 | + locked_coin_balance: { decimal: 0 }, |
| 93 | + tokens: [ |
| 94 | + { token_id: 'ABC123', amount: { decimal: 1 } }, |
| 95 | + { token_id: 'XYZ999', amount: { decimal: 2 } }, |
| 96 | + ], |
| 97 | + transaction_history: [], |
| 98 | + }, |
| 99 | + { |
| 100 | + id: 'tmt1qADDR2', |
| 101 | + coin_balance: { decimal: 0.2 }, |
| 102 | + locked_coin_balance: { decimal: 0 }, |
| 103 | + tokens: [{ token_id: 'LMN000', amount: { decimal: 3 } }], |
| 104 | + transaction_history: [], |
| 105 | + }, |
| 106 | + ] |
| 107 | + |
| 108 | + renderWithProviders({ |
| 109 | + coinType: 'Mintlayer', |
| 110 | + search: 'XYZ9', |
| 111 | + mintlayer: { fetchingBalances: false, addressData: mlAddresses }, |
| 112 | + }) |
| 113 | + |
| 114 | + // Only the first address has a token id containing 'XYZ9' |
| 115 | + // rows include header row; we check specific rendered rows by data-testid |
| 116 | + const addressRow0 = screen.getByTestId('address-row-0') |
| 117 | + expect(within(addressRow0).getByTitle('tmt1qADDR1')).toBeInTheDocument() |
| 118 | + expect(screen.queryByTestId('address-row-1')).not.toBeInTheDocument() |
| 119 | + }) |
| 120 | + |
| 121 | + test('renders and sorts Bitcoin addresses', () => { |
| 122 | + const btcAddresses = [ |
| 123 | + { |
| 124 | + id: 'tb1qAAA', |
| 125 | + coin_balance: { available: 0.1, locked: 0 }, |
| 126 | + tokens: [], |
| 127 | + used: false, |
| 128 | + }, |
| 129 | + { |
| 130 | + id: 'tb1qBBB', |
| 131 | + coin_balance: { available: 1.5, locked: 0 }, |
| 132 | + tokens: [], |
| 133 | + used: true, |
| 134 | + }, |
| 135 | + { |
| 136 | + id: 'tb1qCCC', |
| 137 | + coin_balance: { available: 0.9, locked: 0.2 }, |
| 138 | + tokens: [{ token_id: 'btc-token', amount: { decimal: 1 } }], |
| 139 | + used: false, |
| 140 | + }, |
| 141 | + ] |
| 142 | + |
| 143 | + renderWithProviders({ |
| 144 | + coinType: 'Bitcoin', |
| 145 | + bitcoin: { formatedAddresses: btcAddresses }, |
| 146 | + }) |
| 147 | + |
| 148 | + // Expect order: used first (BBB), then highest available (CCC), then (AAA) |
| 149 | + const row0 = screen.getByTestId('address-row-0') |
| 150 | + const row1 = screen.getByTestId('address-row-1') |
| 151 | + const row2 = screen.getByTestId('address-row-2') |
| 152 | + |
| 153 | + expect(within(row0).getByTitle('tb1qBBB')).toBeInTheDocument() |
| 154 | + expect(within(row1).getByTitle('tb1qCCC')).toBeInTheDocument() |
| 155 | + expect(within(row2).getByTitle('tb1qAAA')).toBeInTheDocument() |
| 156 | + }) |
| 157 | + |
| 158 | + test('shows empty state when no addresses (Mintlayer)', () => { |
| 159 | + renderWithProviders({ |
| 160 | + coinType: 'Mintlayer', |
| 161 | + mintlayer: { fetchingBalances: false, addressData: [] }, |
| 162 | + }) |
| 163 | + |
| 164 | + expect(screen.getByText('No addresses found')).toBeInTheDocument() |
| 165 | + }) |
| 166 | + |
| 167 | + test('shows loading indicator when fetching balances', () => { |
| 168 | + renderWithProviders({ |
| 169 | + coinType: 'Mintlayer', |
| 170 | + mintlayer: { fetchingBalances: true, addressData: [] }, |
| 171 | + }) |
| 172 | + |
| 173 | + expect(screen.getByTestId('loading')).toBeInTheDocument() |
| 174 | + expect(screen.queryByTestId('address-table')).not.toBeInTheDocument() |
| 175 | + }) |
| 176 | +}) |
0 commit comments