|
| 1 | +import { describe, it, expect, render, vi, fireEvent, screen, act, waitFor } from '@test/utils'; |
| 2 | +import React from 'react'; |
| 3 | +import { Table, BaseTableRef, TableRowData, BaseTableCol } from '../index'; |
| 4 | + |
| 5 | +const prefix = 't'; |
| 6 | +const name = `.${prefix}-table`; |
| 7 | + |
| 8 | +const data = []; |
| 9 | +const total = 5; |
| 10 | +for (let i = 0; i < total; i++) { |
| 11 | + data.push({ |
| 12 | + index: i + 1, |
| 13 | + applicant: ['内容', '内容', '内容'][i % 3], |
| 14 | + status: ['内容', '内容', '内容'][i % 3], |
| 15 | + channel: ['内容', '内容', '内容'][i % 3], |
| 16 | + detail: { |
| 17 | + email: ['内容', '内容', '内容内容内容'][i % 3], |
| 18 | + }, |
| 19 | + }); |
| 20 | +} |
| 21 | + |
| 22 | +const columns: BaseTableCol<TableRowData>[] = [ |
| 23 | + { colKey: 'serial-number', title: () => '序号', width: 60 }, |
| 24 | + { colKey: 'applicant', title: <div>标题</div>, ellipsis: true, cell: 'type-slot-name', minWidth: 50 }, |
| 25 | + { |
| 26 | + colKey: 'status', |
| 27 | + title: '标题', |
| 28 | + ellipsis: true, |
| 29 | + }, |
| 30 | + { |
| 31 | + colKey: 'channel', |
| 32 | + title: '标题', |
| 33 | + cell: ({ col, row }) => row[col.colKey], |
| 34 | + ellipsis: true, |
| 35 | + }, |
| 36 | + { |
| 37 | + colKey: 'detail.email', |
| 38 | + title: '标题', |
| 39 | + cell: () => '内容', |
| 40 | + ellipsis: true, |
| 41 | + }, |
| 42 | +]; |
| 43 | + |
| 44 | +describe('table', () => { |
| 45 | + describe(' props ', () => { |
| 46 | + it(': tableLayout', () => { |
| 47 | + const { container, rerender } = render(<Table rowKey="index" columns={columns} data={data} />); |
| 48 | + expect(container.querySelector(`${name}--layout-fixed`)).toBeTruthy(); |
| 49 | + |
| 50 | + rerender(<Table rowKey="index" columns={columns} data={data} tableLayout="auto" />); |
| 51 | + expect(container.querySelector(`${name}--layout-auto`)).toBeTruthy(); |
| 52 | + }); |
| 53 | + |
| 54 | + it(': bordered', () => { |
| 55 | + const { container } = render(<Table rowKey="index" columns={columns} data={data} bordered />); |
| 56 | + expect(container.querySelector(`${name}--bordered`)).toBeTruthy(); |
| 57 | + }); |
| 58 | + |
| 59 | + it(': stripe', () => { |
| 60 | + const { container } = render(<Table rowKey="index" columns={columns} data={data} stripe bordered />); |
| 61 | + expect(container.querySelector(`${name}--striped`)).toBeTruthy(); |
| 62 | + }); |
| 63 | + |
| 64 | + it(': BaseTableCol colkey', () => { |
| 65 | + const columns = [{ title: '姓名' }]; |
| 66 | + const data = [{ name: 'Tom' }]; |
| 67 | + const { container } = render(<Table rowKey="index" columns={columns} data={data} />); |
| 68 | + const th = container.querySelector('th'); |
| 69 | + expect(th.className).not.toContain('t-table__th-'); |
| 70 | + }); |
| 71 | + |
| 72 | + it(': BaseTableCol align', () => { |
| 73 | + columns[0].align = 'center'; |
| 74 | + columns[1].align = 'right'; |
| 75 | + const { container } = render(<Table style={{ width: '200px' }} rowKey="index" columns={columns} data={data} />); |
| 76 | + expect(container.querySelector('.t-align-center')).toBeTruthy(); |
| 77 | + expect(container.querySelector('.t-align-right')).toBeTruthy(); |
| 78 | + }); |
| 79 | + |
| 80 | + it(': BaseTableCol fixed', async () => { |
| 81 | + const onScroll = vi.fn(); |
| 82 | + columns[0].fixed = 'left'; |
| 83 | + columns[3].fixed = 'right'; |
| 84 | + const { container } = render( |
| 85 | + <Table style={{ width: '200px' }} rowKey="index" columns={columns} data={data} onScroll={onScroll} />, |
| 86 | + ); |
| 87 | + |
| 88 | + await waitFor(() => { |
| 89 | + expect(container.querySelector(`${name}__cell--fixed-left`)).toBeTruthy(); |
| 90 | + expect(container.querySelector(`${name}__cell--fixed-right`)).toBeTruthy(); |
| 91 | + }); |
| 92 | + |
| 93 | + const tableContent = container.querySelector(`${name}__content`); |
| 94 | + Object.defineProperty(tableContent, 'scrollWidth', { value: 500 }); |
| 95 | + fireEvent.scroll(tableContent); |
| 96 | + |
| 97 | + await waitFor(() => { |
| 98 | + expect(container.querySelector(`${name}__content--scrollable-to-right`)).toBeTruthy(); |
| 99 | + }); |
| 100 | + |
| 101 | + fireEvent.scroll(tableContent, { target: { scrollLeft: 0 } }); |
| 102 | + await waitFor(() => { |
| 103 | + expect(container.querySelector(`${name}__content--scrollable-to-left`)).toBeFalsy(); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + it(': empty', () => { |
| 108 | + const { container } = render(<Table rowKey="index" columns={columns} data={[]} empty="暂无数据" />); |
| 109 | + expect(container.querySelector(`${name}__empty`).textContent).toBe('暂无数据'); |
| 110 | + }); |
| 111 | + |
| 112 | + it(': cellEmptyContent', () => { |
| 113 | + const columns = [ |
| 114 | + { colKey: 'id', title: 'ID' }, |
| 115 | + { colKey: 'name', title: '姓名' }, |
| 116 | + { colKey: 'age', title: '年龄' }, |
| 117 | + ]; |
| 118 | + const data = [ |
| 119 | + { id: 1, name: '' }, |
| 120 | + { id: 2, age: undefined }, |
| 121 | + ]; |
| 122 | + const { container, rerender } = render( |
| 123 | + <Table rowKey="index" columns={columns} data={data} cellEmptyContent="暂无数据" />, |
| 124 | + ); |
| 125 | + const cellEmptyContent = screen.getAllByText('暂无数据'); |
| 126 | + expect(cellEmptyContent).toHaveLength(4); |
| 127 | + |
| 128 | + rerender( |
| 129 | + <Table |
| 130 | + rowKey="index" |
| 131 | + columns={columns} |
| 132 | + data={data} |
| 133 | + cellEmptyContent={() => <div className="cell-empty-test">暂无数据</div>} |
| 134 | + />, |
| 135 | + ); |
| 136 | + expect(container.querySelector('.cell-empty-test')).toBeTruthy(); |
| 137 | + }); |
| 138 | + |
| 139 | + it(': loading', () => { |
| 140 | + const { container } = render(<Table rowKey="index" columns={columns} data={data} loading />); |
| 141 | + expect(container.querySelector(`${name}__loading--full`)).toBeTruthy(); |
| 142 | + }); |
| 143 | + |
| 144 | + it(': showHeader', () => { |
| 145 | + const { container } = render(<Table rowKey="index" columns={columns} data={data} showHeader={false} />); |
| 146 | + expect(container.querySelector(`${name}__header`)).toBeNull(); |
| 147 | + }); |
| 148 | + |
| 149 | + it(': fixedRows', () => { |
| 150 | + const { container } = render(<Table rowKey="index" columns={columns} data={data} fixedRows={[1, 2]} />); |
| 151 | + expect(container.querySelector(`${name}__row--fixed-top`)).toBeTruthy(); |
| 152 | + expect(container.querySelector(`${name}__row--fixed-bottom`)).toBeTruthy(); |
| 153 | + expect(container.querySelector(`${name}__row--fixed-bottom-first`)).toBeTruthy(); |
| 154 | + }); |
| 155 | + |
| 156 | + it(': height', () => { |
| 157 | + const { container, rerender } = render(<Table rowKey="index" columns={columns} data={data} height={200} />); |
| 158 | + expect(container.querySelector(`${name}__content`)).toHaveStyle('height: 200px;'); |
| 159 | + expect(container.querySelector(`${name}__header--fixed`)).toBeTruthy(); |
| 160 | + |
| 161 | + rerender(<Table rowKey="index" columns={columns} data={data} height="100%" />); |
| 162 | + expect(container.querySelector(`${name}__content`)).toHaveStyle('height: 100%'); |
| 163 | + }); |
| 164 | + |
| 165 | + it(': verticalAlign', () => { |
| 166 | + const verticalAligns = ['top', 'middle', 'bottom'] as const; |
| 167 | + verticalAligns.forEach((verticalAlign) => { |
| 168 | + const { container } = render( |
| 169 | + <Table rowKey="index" columns={columns} data={data} verticalAlign={verticalAlign} />, |
| 170 | + ); |
| 171 | + expect(container.querySelector(`.t-vertical-align-${verticalAlign}`)).toBeTruthy(); |
| 172 | + }); |
| 173 | + }); |
| 174 | + |
| 175 | + it(': rowAttributes', () => { |
| 176 | + const { container, rerender } = render( |
| 177 | + <Table rowKey="index" columns={columns} data={data} rowAttributes={{ draggable: 'true' }} />, |
| 178 | + ); |
| 179 | + const row = container.querySelector('tbody tr'); |
| 180 | + expect(row).toHaveAttribute('draggable', 'true'); |
| 181 | + |
| 182 | + rerender( |
| 183 | + <Table |
| 184 | + rowKey="index" |
| 185 | + columns={columns} |
| 186 | + data={data} |
| 187 | + rowAttributes={[{ draggable: 'true' }, { title: '超出省略显示' }]} |
| 188 | + />, |
| 189 | + ); |
| 190 | + expect(row).toHaveAttribute('draggable', 'true'); |
| 191 | + expect(row).toHaveAttribute('title', '超出省略显示'); |
| 192 | + |
| 193 | + rerender(<Table rowKey="index" columns={columns} data={data} rowAttributes={() => ({ draggable: 'true' })} />); |
| 194 | + expect(row).toHaveAttribute('draggable', 'true'); |
| 195 | + }); |
| 196 | + |
| 197 | + it(': rowClassName', () => { |
| 198 | + const { container, rerender } = render( |
| 199 | + <Table rowKey="index" columns={columns} data={data} rowClassName="row-test" />, |
| 200 | + ); |
| 201 | + expect(container.querySelector('.row-test')).toHaveClass('row-test'); |
| 202 | + |
| 203 | + rerender(<Table rowKey="index" columns={columns} data={data} rowClassName={{ 'row-test': true }} />); |
| 204 | + expect(container.querySelector('.row-test')).toHaveClass('row-test'); |
| 205 | + |
| 206 | + rerender(<Table rowKey="index" columns={columns} data={data} rowClassName={() => 'row-test'} />); |
| 207 | + expect(container.querySelector('.row-test')).toHaveClass('row-test'); |
| 208 | + }); |
| 209 | + }); |
| 210 | + |
| 211 | + describe('events', () => { |
| 212 | + const columns = [ |
| 213 | + { colKey: 'id', title: 'ID', stopPropagation: true }, |
| 214 | + { colKey: 'name', title: '姓名', stopPropagation: true }, |
| 215 | + { colKey: 'age', title: '年龄', stopPropagation: true }, |
| 216 | + ]; |
| 217 | + const data = [ |
| 218 | + { id: 1, name: '' }, |
| 219 | + { id: 2, age: undefined }, |
| 220 | + ]; |
| 221 | + it(': onCellClick', () => { |
| 222 | + const onCellClick = vi.fn(); |
| 223 | + const { container } = render(<Table rowKey="index" columns={columns} data={data} onCellClick={onCellClick} />); |
| 224 | + fireEvent.click(container.querySelector('td')); |
| 225 | + expect(onCellClick).toHaveBeenCalled(); |
| 226 | + }); |
| 227 | + |
| 228 | + it(': onRowClick', () => { |
| 229 | + const onRowClick = vi.fn(); |
| 230 | + const { container } = render(<Table rowKey="index" columns={columns} data={data} onRowClick={onRowClick} />); |
| 231 | + fireEvent.click(container.querySelector('tbody tr')); |
| 232 | + expect(onRowClick).toHaveBeenCalled(); |
| 233 | + }); |
| 234 | + |
| 235 | + it(': onScroll', () => { |
| 236 | + const onScroll = vi.fn(); |
| 237 | + const { container } = render(<Table rowKey="index" columns={columns} data={data} onScroll={onScroll} />); |
| 238 | + fireEvent.scroll(container.querySelector(`${name}__content`)); |
| 239 | + expect(onScroll).toHaveBeenCalled(); |
| 240 | + }); |
| 241 | + |
| 242 | + it(': refreshTable', async () => { |
| 243 | + const ref = React.createRef<BaseTableRef>(); |
| 244 | + render(<Table ref={ref} rowKey="index" columns={columns} data={data} />); |
| 245 | + expect(ref.current).toBeTruthy(); |
| 246 | + |
| 247 | + await act(async () => { |
| 248 | + expect(() => ref.current?.refreshTable()).not.toThrow(); |
| 249 | + }); |
| 250 | + }); |
| 251 | + |
| 252 | + it(': resize', async () => { |
| 253 | + const { container } = render(<Table rowKey="index" columns={columns} data={data} />); |
| 254 | + const resizeEvent = new Event('resize'); |
| 255 | + window.dispatchEvent(resizeEvent); |
| 256 | + await waitFor(async () => { |
| 257 | + expect(container.querySelector(`${name}`)).toBeTruthy(); |
| 258 | + }); |
| 259 | + }); |
| 260 | + }); |
| 261 | +}); |
0 commit comments