|
| 1 | +import React from 'react'; |
| 2 | +import { render, fireEvent } from '@testing-library/react'; |
| 3 | +import Table from '../index'; |
| 4 | + |
| 5 | +const columns = [ |
| 6 | + { title: 'Name', dataIndex: 'name', key: 'name' }, |
| 7 | + { title: 'Age', dataIndex: 'age', key: 'age', sorter: (a: any, b: any) => a.age - b.age }, |
| 8 | +]; |
| 9 | + |
| 10 | +const dataSource = [ |
| 11 | + { key: '1', name: 'Alice', age: 30 }, |
| 12 | + { key: '2', name: 'Bob', age: 25 }, |
| 13 | + { key: '3', name: 'Charlie', age: 35 }, |
| 14 | +]; |
| 15 | + |
| 16 | +describe('<Table />', () => { |
| 17 | + it('should match the snapshot', () => { |
| 18 | + const { asFragment } = render(<Table columns={columns} dataSource={dataSource} />); |
| 19 | + expect(asFragment()).toMatchSnapshot(); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should render correctly', () => { |
| 23 | + const { container } = render(<Table columns={columns} dataSource={dataSource} />); |
| 24 | + expect(container.firstChild).toHaveClass('ty-table'); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should render data rows', () => { |
| 28 | + const { getByText } = render(<Table columns={columns} dataSource={dataSource} />); |
| 29 | + expect(getByText('Alice')).toBeInTheDocument(); |
| 30 | + expect(getByText('Bob')).toBeInTheDocument(); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should render column headers', () => { |
| 34 | + const { getByText } = render(<Table columns={columns} dataSource={dataSource} />); |
| 35 | + expect(getByText('Name')).toBeInTheDocument(); |
| 36 | + expect(getByText('Age')).toBeInTheDocument(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should render bordered', () => { |
| 40 | + const { container } = render(<Table columns={columns} dataSource={dataSource} bordered />); |
| 41 | + expect(container.firstChild).toHaveClass('ty-table_bordered'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should show empty state', () => { |
| 45 | + const { getByText } = render(<Table columns={columns} dataSource={[]} />); |
| 46 | + expect(getByText('No Data')).toBeInTheDocument(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should sort on header click', () => { |
| 50 | + const { container, getAllByRole } = render( |
| 51 | + <Table columns={columns} dataSource={dataSource} pagination={false} /> |
| 52 | + ); |
| 53 | + const ageHeader = container.querySelector('.ty-table__cell_sortable'); |
| 54 | + fireEvent.click(ageHeader!); |
| 55 | + const rows = container.querySelectorAll('.ty-table__tbody .ty-table__row'); |
| 56 | + expect(rows[0].textContent).toContain('Bob'); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should handle row selection', () => { |
| 60 | + const onChange = jest.fn(); |
| 61 | + const { container } = render( |
| 62 | + <Table |
| 63 | + columns={columns} |
| 64 | + dataSource={dataSource} |
| 65 | + rowSelection={{ onChange }} |
| 66 | + pagination={false} |
| 67 | + /> |
| 68 | + ); |
| 69 | + const checkboxes = container.querySelectorAll('input[type="checkbox"]'); |
| 70 | + // first is select all, second is first row |
| 71 | + fireEvent.click(checkboxes[1]); |
| 72 | + expect(onChange).toHaveBeenCalled(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should show loading state', () => { |
| 76 | + const { getByText } = render(<Table columns={columns} dataSource={dataSource} loading />); |
| 77 | + expect(getByText('Loading...')).toBeInTheDocument(); |
| 78 | + }); |
| 79 | +}); |
0 commit comments