Skip to content

Commit 8539643

Browse files
committed
test: add case
1 parent 3e393b8 commit 8539643

File tree

2 files changed

+93
-9
lines changed

2 files changed

+93
-9
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ React.render(<Table columns={columns} data={data} />, mountNode);
118118
| sticky | boolean \| {offsetHeader?: number, offsetScroll?: number, getContainer?: () => Window \| HTMLElement } | false | stick header and scroll bar |
119119
| summary | (data: readonly RecordType[]) => React.ReactNode | - | `summary` attribute in `table` component is used to define the summary row. |
120120
| rowHoverable | boolean | true | Table hover interaction |
121+
| columnResizable | boolean | false | Column resizable |
121122

122123
## Column Props
123124

@@ -132,6 +133,7 @@ React.render(<Table columns={columns} data={data} />, mountNode);
132133
| fixed | String \| Boolean | | this column will be fixed when table scroll horizontally: true or 'left' or 'right' |
133134
| align | String | | specify how cell content is aligned |
134135
| ellipsis | Boolean | | specify whether cell content be ellipsized |
136+
| resizable | Boolean \| { minWidth?: number } | | column resize config |
135137
| rowScope | 'row' \| 'rowgroup' | | Set scope attribute for all cells in this column |
136138
| onCell | Function(record, index) | | Set custom props per each cell. |
137139
| onHeaderCell | Function(record) | | Set custom props per each header cell. |

tests/Resizable.spec.jsx

Lines changed: 91 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ describe('Table.resizable', () => {
3535
const [widthMap, setWidthMap] = React.useState(new Map());
3636

3737
const columns = [
38-
{ key: 'a', dataIndex: 'a', width: 100 },
39-
{ key: 'b', dataIndex: 'b', width: 100 },
38+
{ key: 'a', dataIndex: 'a', width: 400 },
39+
{ key: 'b', dataIndex: 'b', width: 400 },
4040
].map(col => ({ ...col, width: widthMap.get(col.key ?? col.dataIndex) || col.width }));
4141

4242
return (
@@ -69,11 +69,11 @@ describe('Table.resizable', () => {
6969
await triggerResize([
7070
{
7171
data: wrapper.find('ResizeObserver').at(1).props().data,
72-
size: { width: 100, offsetWidth: 100 },
72+
size: { width: 400, offsetWidth: 400 },
7373
},
7474
{
7575
data: wrapper.find('ResizeObserver').at(2).props().data,
76-
size: { width: 100, offsetWidth: 100 },
76+
size: { width: 400, offsetWidth: 400 },
7777
},
7878
]);
7979

@@ -99,15 +99,15 @@ describe('Table.resizable', () => {
9999

100100
expect(onColumnResizeComplete).toHaveBeenCalledWith({
101101
columnKey: 'a',
102-
width: 200,
102+
width: 500,
103103
columnKeyWidths: [
104-
{ columnKey: 'a', width: 200 },
105-
{ columnKey: 'b', width: 100 },
104+
{ columnKey: 'a', width: 500 },
105+
{ columnKey: 'b', width: 400 },
106106
],
107107
});
108108

109-
expect(wrapper.find('colgroup col').at(0).props().style.width).toBe(200);
110-
expect(wrapper.find('colgroup col').at(1).props().style.width).toBe(100);
109+
expect(wrapper.find('colgroup col').at(0).props().style.width).toBe(500);
110+
expect(wrapper.find('colgroup col').at(1).props().style.width).toBe(400);
111111
});
112112

113113
it('columns total width < componentWidth', async () => {
@@ -194,4 +194,86 @@ describe('Table.resizable', () => {
194194
expect(wrapper.find('colgroup col').at(0).props().style.width).toBe(300);
195195
expect(wrapper.find('colgroup col').at(1).props().style.width).toBe(500);
196196
});
197+
198+
it('minWidth should be worked', async () => {
199+
const onColumnResizeComplete = vi.fn();
200+
201+
const App = () => {
202+
const [widthMap, setWidthMap] = React.useState(new Map());
203+
204+
const columns = [
205+
{ key: 'a', dataIndex: 'a', width: 800, resizable: { minWidth: 400 } },
206+
{ key: 'b', dataIndex: 'b', width: 800 },
207+
].map(col => ({ ...col, width: widthMap.get(col.key ?? col.dataIndex) || col.width }));
208+
209+
return (
210+
<Table
211+
columnResizable
212+
data={[{ a: '123', b: '123', key: '1' }]}
213+
columns={columns}
214+
scroll={{ x: columns.reduce((t, c) => t + c.width, 0) }}
215+
onColumnResizeComplete={info => {
216+
setWidthMap(prev => {
217+
const result = new Map(prev);
218+
info.columnKeyWidths.forEach(i => {
219+
result.set(i.columnKey, i.width);
220+
});
221+
return result;
222+
});
223+
onColumnResizeComplete(info);
224+
}}
225+
/>
226+
);
227+
};
228+
const wrapper = mount(<App />);
229+
230+
async function triggerResize(resizeList) {
231+
wrapper.find(RcResizeObserver.Collection).first().props().onBatchResize(resizeList);
232+
await safeAct(wrapper);
233+
wrapper.update();
234+
}
235+
236+
await triggerResize([
237+
{
238+
data: wrapper.find('ResizeObserver').at(1).props().data,
239+
size: { width: 800, offsetWidth: 800 },
240+
},
241+
{
242+
data: wrapper.find('ResizeObserver').at(2).props().data,
243+
size: { width: 800, offsetWidth: 800 },
244+
},
245+
]);
246+
247+
wrapper.find('.rc-table-cell-resize-handle').first().simulate('mousedown', { pageX: 0 });
248+
249+
const mousemoveEvent = new Event('mousemove');
250+
mousemoveEvent.pageX = -1000;
251+
252+
await act(async () => {
253+
document.body.dispatchEvent(mousemoveEvent);
254+
await Promise.resolve();
255+
wrapper.update();
256+
});
257+
258+
const mouseupEvent = new Event('mouseup');
259+
mouseupEvent.pageX = -1000;
260+
261+
await act(async () => {
262+
document.body.dispatchEvent(mouseupEvent);
263+
await Promise.resolve();
264+
wrapper.update();
265+
});
266+
267+
expect(onColumnResizeComplete).toHaveBeenCalledWith({
268+
columnKey: 'a',
269+
width: 400,
270+
columnKeyWidths: [
271+
{ columnKey: 'a', width: 400 },
272+
{ columnKey: 'b', width: 800 },
273+
],
274+
});
275+
276+
expect(wrapper.find('colgroup col').at(0).props().style.width).toBe(400);
277+
expect(wrapper.find('colgroup col').at(1).props().style.width).toBe(800);
278+
});
197279
});

0 commit comments

Comments
 (0)