|
| 1 | +import { render, fireEvent } from '@testing-library/react'; |
| 2 | +import React from 'react'; |
| 3 | +import Table, { TableProps } from '../src'; |
| 4 | +describe('support classNames and styles', () => { |
| 5 | + const columns: TableProps['columns'] = [ |
| 6 | + { |
| 7 | + title: 'title1', |
| 8 | + dataIndex: 'a', |
| 9 | + className: 'a', |
| 10 | + key: 'a', |
| 11 | + width: 100, |
| 12 | + }, |
| 13 | + { |
| 14 | + title: 'title2', |
| 15 | + dataIndex: 'b', |
| 16 | + className: 'b', |
| 17 | + key: 'b', |
| 18 | + width: 100, |
| 19 | + }, |
| 20 | + { |
| 21 | + title: 'title3', |
| 22 | + dataIndex: 'c', |
| 23 | + className: 'c', |
| 24 | + key: 'c', |
| 25 | + width: 200, |
| 26 | + }, |
| 27 | + { |
| 28 | + title: 'Operations', |
| 29 | + dataIndex: '', |
| 30 | + className: 'd', |
| 31 | + key: 'd', |
| 32 | + render() { |
| 33 | + return <a href="#">Operations</a>; |
| 34 | + }, |
| 35 | + }, |
| 36 | + ]; |
| 37 | + |
| 38 | + const data = [ |
| 39 | + { a: '123', key: '1' }, |
| 40 | + { a: 'cdd', b: 'edd', key: '2' }, |
| 41 | + { a: '1333', c: 'eee', d: 2, key: '3' }, |
| 42 | + ]; |
| 43 | + const commonTableProps = { |
| 44 | + columns: columns, |
| 45 | + rowClassName: (record, i) => `row-${i}`, |
| 46 | + expandedRowRender: (record) => <p>extra: {record.a}</p>, |
| 47 | + expandedRowClassName: (record, i) => `ex-row-${i}`, |
| 48 | + className: "table", |
| 49 | + title: () => <span>title</span>, |
| 50 | + footer: () => <span>footer</span>, |
| 51 | + }; |
| 52 | + it('should support className and style', () => { |
| 53 | + const testClassNames = { |
| 54 | + section: 'test-section', |
| 55 | + header: 'test-header', |
| 56 | + title: 'test-title', |
| 57 | + body: 'test-body', |
| 58 | + footer: 'test-footer', |
| 59 | + content: 'test-content', |
| 60 | + item: 'test-item', |
| 61 | + } |
| 62 | + const testStyles = { |
| 63 | + section: { background: 'red' }, |
| 64 | + header: { background: 'blue' }, |
| 65 | + title: { background: 'green' }, |
| 66 | + body: { background: 'yellow' }, |
| 67 | + footer: { background: 'pink' }, |
| 68 | + content: { background: 'purple' }, |
| 69 | + item: { background: 'orange' }, |
| 70 | + } |
| 71 | + const { container } = render( |
| 72 | + <Table |
| 73 | + {...commonTableProps} |
| 74 | + classNames={testClassNames} |
| 75 | + styles={testStyles} |
| 76 | + data={data} |
| 77 | + /> |
| 78 | + ) |
| 79 | + const section = container.querySelector('.rc-table-container'); |
| 80 | + const title = container.querySelector('.rc-table-title'); |
| 81 | + const footer = container.querySelector('.rc-table-footer'); |
| 82 | + const content = container.querySelector('.rc-table-content'); |
| 83 | + const item = container.querySelector('.rc-table-cell'); |
| 84 | + expect(section).toHaveClass(testClassNames.section); |
| 85 | + expect(section).toHaveStyle(testStyles.section); |
| 86 | + expect(title).toHaveClass(testClassNames.title); |
| 87 | + expect(title).toHaveStyle(testStyles.title); |
| 88 | + expect(footer).toHaveClass(testClassNames.footer); |
| 89 | + expect(footer).toHaveStyle(testStyles.footer); |
| 90 | + expect(content).toHaveClass(testClassNames.content); |
| 91 | + expect(content).toHaveStyle(testStyles.content); |
| 92 | + expect(item).toHaveClass(testClassNames.item); |
| 93 | + expect(item).toHaveStyle(testStyles.item); |
| 94 | + |
| 95 | + const { container: scrollContainer } = render( |
| 96 | + <Table |
| 97 | + {...commonTableProps} |
| 98 | + classNames={testClassNames} |
| 99 | + styles={testStyles} |
| 100 | + data={data} |
| 101 | + scroll={{ y: 200 }} |
| 102 | + /> |
| 103 | + ) |
| 104 | + const header = scrollContainer.querySelector('.rc-table-header'); |
| 105 | + const body = scrollContainer.querySelector('.rc-table-body'); |
| 106 | + expect(header).toHaveClass(testClassNames.header); |
| 107 | + expect(header).toHaveStyle(testStyles.header); |
| 108 | + expect(body).toHaveClass(testClassNames.body); |
| 109 | + expect(body).toHaveStyle(testStyles.body); |
| 110 | + }) |
| 111 | +}) |
| 112 | + |
0 commit comments