|
| 1 | +import { render } from '@testing-library/react'; |
| 2 | +import userEvent from '@testing-library/user-event'; |
| 3 | + |
| 4 | +import React from 'react'; |
| 5 | + |
| 6 | +import { prettyTx } from '../../utils'; |
| 7 | +import { TableBase } from './TableBase'; |
| 8 | +import { Align } from './TableBase.types'; |
| 9 | + |
| 10 | +const testColumns = [ |
| 11 | + { |
| 12 | + id: 'index', |
| 13 | + title: 'Index', |
| 14 | + align: Align.center, |
| 15 | + cellRenderer: row => `${row.index}.`, |
| 16 | + }, |
| 17 | + { |
| 18 | + id: 'address', |
| 19 | + title: 'Address', |
| 20 | + align: Align.center, |
| 21 | + }, |
| 22 | + { |
| 23 | + id: 'balance', |
| 24 | + title: 'Balance', |
| 25 | + align: Align.center, |
| 26 | + cellRenderer: row => `${row.balance} RBTC`, |
| 27 | + }, |
| 28 | +]; |
| 29 | + |
| 30 | +const testRows = [ |
| 31 | + { |
| 32 | + index: 1, |
| 33 | + address: prettyTx('0xbcb5a190ACCbc80F4F2c130b5876521E4D5A2C0a', 6, 4), |
| 34 | + balance: 0.2, |
| 35 | + }, |
| 36 | + { |
| 37 | + index: 2, |
| 38 | + address: prettyTx('0xop42490ACCbc50F4F9c130b5876521I1q7b3C0p', 6, 4), |
| 39 | + balance: 2, |
| 40 | + }, |
| 41 | +]; |
| 42 | + |
| 43 | +describe('TableBase', () => { |
| 44 | + test('should render a table with 2 items', () => { |
| 45 | + const { getAllByTestId } = render( |
| 46 | + <TableBase columns={testColumns} rows={testRows} dataAttribute="table" />, |
| 47 | + ); |
| 48 | + |
| 49 | + const rows = getAllByTestId('table-row', { exact: false }); |
| 50 | + expect(rows.length).toBe(2); |
| 51 | + }); |
| 52 | + |
| 53 | + test('should click on an item if onRowClick is provided', () => { |
| 54 | + const handleClick = jest.fn(); |
| 55 | + |
| 56 | + const { getByTestId } = render( |
| 57 | + <TableBase |
| 58 | + columns={testColumns} |
| 59 | + rows={testRows} |
| 60 | + dataAttribute="table" |
| 61 | + onRowClick={handleClick} |
| 62 | + />, |
| 63 | + ); |
| 64 | + |
| 65 | + const row = getByTestId('table-row-1'); |
| 66 | + userEvent.click(row); |
| 67 | + |
| 68 | + expect(handleClick).toBeCalledTimes(1); |
| 69 | + }); |
| 70 | + |
| 71 | + test('should contain an empty message if there are no rows', () => { |
| 72 | + const noDataText = 'You should see this as there are no rows'; |
| 73 | + const { getByText } = render( |
| 74 | + <TableBase columns={testColumns} noData={noDataText} />, |
| 75 | + ); |
| 76 | + |
| 77 | + const emptyText = getByText(noDataText); |
| 78 | + expect(emptyText).toBeInTheDocument(); |
| 79 | + }); |
| 80 | +}); |
0 commit comments