|
| 1 | +import React from 'react'; |
| 2 | +import { render, act } from '@testing-library/react'; |
| 3 | +import Table, { type ColumnsType } from '../src'; |
| 4 | +import { spyElementPrototypes } from '@rc-component/util/lib/test/domHook'; |
| 5 | + |
| 6 | +describe('Table.Expanded', () => { |
| 7 | + let domSpy: ReturnType<typeof spyElementPrototypes>; |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + vi.useFakeTimers(); |
| 11 | + }); |
| 12 | + |
| 13 | + beforeAll(() => { |
| 14 | + domSpy = spyElementPrototypes(HTMLElement, { |
| 15 | + offsetParent: { |
| 16 | + get: () => ({}), |
| 17 | + }, |
| 18 | + offsetWidth: { |
| 19 | + get: () => 1000, |
| 20 | + }, |
| 21 | + }); |
| 22 | + }); |
| 23 | + |
| 24 | + afterAll(() => { |
| 25 | + domSpy.mockRestore(); |
| 26 | + }); |
| 27 | + |
| 28 | + const columns: ColumnsType = [ |
| 29 | + { |
| 30 | + title: 'key', |
| 31 | + dataIndex: 'key', |
| 32 | + width: 100, |
| 33 | + onCell: (_, index) => { |
| 34 | + const props: React.TdHTMLAttributes<HTMLTableCellElement> = {}; |
| 35 | + if (index === 0) props.rowSpan = 1; |
| 36 | + if (index === 1) props.rowSpan = 2; |
| 37 | + if (index === 2) props.rowSpan = 0; |
| 38 | + if (index === 3) props.rowSpan = undefined; |
| 39 | + return props; |
| 40 | + }, |
| 41 | + }, |
| 42 | + Table.EXPAND_COLUMN, |
| 43 | + { title: 'key2', dataIndex: 'key2', width: 100 }, |
| 44 | + ]; |
| 45 | + const data = [{ key: 'a' }, { key: 'b' }, { key: 'c' }, { key: 'd' }]; |
| 46 | + |
| 47 | + it('expanded + rowSpan', async () => { |
| 48 | + const { container } = render( |
| 49 | + <Table<Record<string, any>> |
| 50 | + columns={columns} |
| 51 | + data={data} |
| 52 | + expandable={{ |
| 53 | + defaultExpandAllRows: true, |
| 54 | + expandedRowRender: record => <div>{record.key}</div>, |
| 55 | + }} |
| 56 | + />, |
| 57 | + ); |
| 58 | + |
| 59 | + await act(async () => { |
| 60 | + vi.runAllTimers(); |
| 61 | + await Promise.resolve(); |
| 62 | + }); |
| 63 | + const trList = container.querySelector('tbody').querySelectorAll('tr'); |
| 64 | + // row 1 |
| 65 | + expect(trList[0].querySelectorAll('td').length).toBe(3); |
| 66 | + expect(trList[0].querySelectorAll('td')[0].getAttribute('rowspan')).toBe('2'); |
| 67 | + // expand 1 |
| 68 | + expect(trList[1].querySelectorAll('td').length).toBe(1); |
| 69 | + expect(trList[1].querySelectorAll('td')[0].getAttribute('colspan')).toBe('2'); |
| 70 | + // row 2 |
| 71 | + expect(trList[2].querySelectorAll('td').length).toBe(3); |
| 72 | + expect(trList[2].querySelectorAll('td')[0].getAttribute('rowspan')).toBe('4'); |
| 73 | + // expand 2 |
| 74 | + expect(trList[3].querySelectorAll('td').length).toBe(1); |
| 75 | + expect(trList[3].querySelectorAll('td')[0].getAttribute('colspan')).toBe('2'); |
| 76 | + // row 3 |
| 77 | + expect(trList[4].querySelectorAll('td').length).toBe(2); |
| 78 | + // expand 3 |
| 79 | + expect(trList[5].querySelectorAll('td').length).toBe(1); |
| 80 | + expect(trList[5].querySelectorAll('td')[0].getAttribute('colspan')).toBe('2'); |
| 81 | + // row 4 |
| 82 | + expect(trList[6].querySelectorAll('td').length).toBe(3); |
| 83 | + // expand 4 |
| 84 | + expect(trList[7].querySelectorAll('td').length).toBe(1); |
| 85 | + expect(trList[7].querySelectorAll('td')[0].getAttribute('colspan')).toBe('3'); |
| 86 | + }); |
| 87 | +}); |
0 commit comments