|
| 1 | +import React from 'react'; |
| 2 | +import { fireEvent, render, screen } from '@testing-library/react'; |
| 3 | +import { Table } from '..'; |
| 4 | + |
| 5 | +describe('Table component', () => { |
| 6 | + |
| 7 | + type TRowData= { |
| 8 | + id: string; |
| 9 | + col1: string; |
| 10 | + col2: string; |
| 11 | + } |
| 12 | + const columns = [ |
| 13 | + { id: '1', header: 'Header 1' }, |
| 14 | + { id: '2', header: 'Header 2' }, |
| 15 | + ]; |
| 16 | + |
| 17 | + const data = [ |
| 18 | + { id: '1', col1: 'Data 1', col2: 'Data A' }, |
| 19 | + { id: '2', col1: 'Data 2', col2: 'Data B' }, |
| 20 | + ]; |
| 21 | + |
| 22 | + const emptyDataMessage = 'No data available'; |
| 23 | + const isFetching = false; |
| 24 | + const loadMoreFunction = jest.fn(); |
| 25 | + const rowRender = (rowData: TRowData) => ( |
| 26 | + <div> |
| 27 | + <span>{rowData.col1}</span> |
| 28 | + <span>{rowData.col2}</span> |
| 29 | + </div> |
| 30 | + ); |
| 31 | + |
| 32 | + it('renders table with data and headers', () => { |
| 33 | + render( |
| 34 | + <Table |
| 35 | + columns={columns} |
| 36 | + data={data} |
| 37 | + emptyDataMessage={emptyDataMessage} |
| 38 | + isFetching={isFetching} |
| 39 | + loadMoreFunction={loadMoreFunction} |
| 40 | + rowRender={rowRender} |
| 41 | + /> |
| 42 | + ); |
| 43 | + |
| 44 | + expect(screen.getByText('Data 1')).toBeInTheDocument(); |
| 45 | + expect(screen.getByText('Data A')).toBeInTheDocument(); |
| 46 | + expect(screen.getByText('Data 2')).toBeInTheDocument(); |
| 47 | + expect(screen.getByText('Data B')).toBeInTheDocument(); |
| 48 | + }); |
| 49 | + |
| 50 | + it('renders empty data message when data is empty', () => { |
| 51 | + render( |
| 52 | + <Table |
| 53 | + columns={columns} |
| 54 | + data={[]} |
| 55 | + emptyDataMessage={emptyDataMessage} |
| 56 | + isFetching={isFetching} |
| 57 | + loadMoreFunction={loadMoreFunction} |
| 58 | + rowRender={rowRender} |
| 59 | + /> |
| 60 | + ); |
| 61 | + |
| 62 | + // Check if empty data message is rendered |
| 63 | + expect(screen.getByText('No data available')).toBeInTheDocument(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('calls loadMoreFunction when scrolling to bottom', () => { |
| 67 | + const {container} = render( |
| 68 | + <Table |
| 69 | + columns={columns} |
| 70 | + data={data} |
| 71 | + emptyDataMessage={emptyDataMessage} |
| 72 | + isFetching={isFetching} |
| 73 | + loadMoreFunction={loadMoreFunction} |
| 74 | + rowRender={rowRender} |
| 75 | + /> |
| 76 | + ); |
| 77 | + |
| 78 | + // Simulate scrolling to bottom |
| 79 | + const tableContent = container.getElementsByClassName('deriv-table__content')[0]; |
| 80 | + tableContent.scrollTop = 500; // Adjust scroll position |
| 81 | + fireEvent.scroll(tableContent); |
| 82 | + |
| 83 | + // Check if loadMoreFunction is called |
| 84 | + expect(loadMoreFunction).toHaveBeenCalledTimes(4); |
| 85 | + }); |
| 86 | + |
| 87 | + |
| 88 | +}); |
0 commit comments