Skip to content

Commit ce61f3b

Browse files
committed
test: update test
1 parent 59d78ee commit ce61f3b

File tree

8 files changed

+56
-129
lines changed

8 files changed

+56
-129
lines changed

docs/examples/fixedColumns.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const columns: ColumnType<RecordType>[] = [
1515
{ title: 'title1', dataIndex: 'a', key: 'a', width: 100, fixed: 'left' },
1616
{ title: 'title2', dataIndex: 'b', key: 'b', width: 100, ellipsis: true },
1717
{ title: 'title3', dataIndex: 'c', key: 'c', fixed: 'left' },
18-
{ title: 'title4', dataIndex: 'b', key: 'd' },
18+
{ title: 'title4', dataIndex: 'b', key: 'd', fixed: 'left' },
1919
{ title: 'title5', dataIndex: 'b', key: 'e' },
2020
{ title: 'title6', dataIndex: 'b', key: 'f' },
2121
{

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,14 @@
6262
},
6363
"devDependencies": {
6464
"@rc-component/father-plugin": "^1.0.2",
65-
"@testing-library/jest-dom": "^5.16.5",
65+
"@testing-library/jest-dom": "^6.4.0",
6666
"@testing-library/react": "^12.1.5",
6767
"@types/enzyme": "^3.10.5",
68-
"@types/react": "^18.0.28",
6968
"@types/jest": "^29.5.0",
69+
"@types/react": "^18.0.28",
7070
"@types/react-dom": "^18.0.5",
7171
"@types/responselike": "^1.0.0",
7272
"@types/styled-components": "^5.1.32",
73-
"@types/testing-library__jest-dom": "^6.0.0",
7473
"@umijs/fabric": "^4.0.1",
7574
"@vitest/coverage-c8": "^0.31.0",
7675
"cross-env": "^7.0.0",
@@ -105,7 +104,7 @@
105104
"regenerator-runtime": "^0.14.0",
106105
"styled-components": "^6.1.1",
107106
"typescript": "~5.3.0",
108-
"vitest": "^0.31.0"
107+
"vitest": "^1.2.2"
109108
},
110109
"lint-staged": {
111110
"**/*.{js,jsx,tsx,ts,md,json}": [

src/hooks/useColumns/index.tsx

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -88,30 +88,6 @@ function flatColumns<RecordType>(
8888
}, []);
8989
}
9090

91-
function warningFixed(flattenColumns: readonly { fixed?: FixedType }[]) {
92-
let allFixLeft = true;
93-
for (let i = 0; i < flattenColumns.length; i += 1) {
94-
const col = flattenColumns[i];
95-
if (allFixLeft && col.fixed !== 'left') {
96-
allFixLeft = false;
97-
} else if (!allFixLeft && col.fixed === 'left') {
98-
warning(false, `Index ${i - 1} of \`columns\` missing \`fixed='left'\` prop.`);
99-
break;
100-
}
101-
}
102-
103-
let allFixRight = true;
104-
for (let i = flattenColumns.length - 1; i >= 0; i -= 1) {
105-
const col = flattenColumns[i];
106-
if (allFixRight && col.fixed !== 'right') {
107-
allFixRight = false;
108-
} else if (!allFixRight && col.fixed === 'right') {
109-
warning(false, `Index ${i + 1} of \`columns\` missing \`fixed='right'\` prop.`);
110-
break;
111-
}
112-
}
113-
}
114-
11591
function revertForRtl<RecordType>(columns: ColumnsType<RecordType>): ColumnsType<RecordType> {
11692
return columns.map(column => {
11793
const { fixed, ...restProps } = column;
@@ -294,11 +270,6 @@ function useColumns<RecordType>(
294270
return flatColumns(mergedColumns);
295271
}, [mergedColumns, direction, scrollWidth]);
296272

297-
// Only check out of production since it's waste for each render
298-
if (process.env.NODE_ENV !== 'production') {
299-
warningFixed(direction === 'rtl' ? flattenColumns.slice().reverse() : flattenColumns);
300-
}
301-
302273
// ========================= FillWidth ========================
303274
const [filledColumns, realScrollWidth] = useWidthColumns(
304275
flattenColumns,

src/hooks/useStickyOffsets.ts

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,48 +12,14 @@ function useStickyOffsets<RecordType>(
1212
const stickyOffsets: StickyOffsets = useMemo(() => {
1313
const columnCount = flattenColumns.length;
1414

15-
// const leftOffsets: number[] = [];
16-
// const rightOffsets: number[] = [];
17-
// let left = 0;
18-
// let right = 0;
19-
20-
// for (let start = 0; start < columnCount; start += 1) {
21-
// if (direction === 'rtl') {
22-
// // Left offset
23-
// rightOffsets[start] = right;
24-
// right += colWidths[start] || 0;
25-
26-
// // Right offset
27-
// const end = columnCount - start - 1;
28-
// leftOffsets[end] = left;
29-
// left += colWidths[end] || 0;
30-
// } else {
31-
// // Left offset
32-
// leftOffsets[start] = left;
33-
// left += colWidths[start] || 0;
34-
35-
// // Right offset
36-
// const end = columnCount - start - 1;
37-
// rightOffsets[end] = right;
38-
// right += colWidths[end] || 0;
39-
// }
40-
// }
41-
42-
// console.log('leftOffsets', leftOffsets);
43-
// console.log('rightOffsets', rightOffsets);
44-
// // // console.log('flattenColumns', flattenColumns);
45-
4615
const getOffsets = (startIndex: number, endIndex: number, offset: number) => {
4716
const offsets: number[] = [];
4817
let total = 0;
4918

5019
for (let i = startIndex; i !== endIndex; i += offset) {
5120
offsets.push(total);
5221

53-
const col = flattenColumns[i];
54-
console.log(i, col);
55-
56-
if (col.fixed) {
22+
if (flattenColumns[i].fixed) {
5723
total += colWidths[i] || 0;
5824
}
5925
}
@@ -64,10 +30,6 @@ function useStickyOffsets<RecordType>(
6430
const startOffsets = getOffsets(0, columnCount, 1);
6531
const endOffsets = getOffsets(columnCount - 1, -1, -1).reverse();
6632

67-
// console.log('flattenColumns', flattenColumns);
68-
// console.log('startOffsets', startOffsets);
69-
// console.log('endOffsets', endOffsets);
70-
7133
return direction === 'rtl'
7234
? {
7335
left: endOffsets,

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { EXPAND_COLUMN, INTERNAL_HOOKS } from './constant';
22
import { FooterComponents as Summary } from './Footer';
3-
import type { ColumnType, Reference } from './interface';
3+
import type { ColumnType, ColumnsType, Reference } from './interface';
44
import Column from './sugar/Column';
55
import ColumnGroup from './sugar/ColumnGroup';
66
import type { TableProps } from './Table';
@@ -23,6 +23,7 @@ export {
2323
type VirtualTableProps,
2424
type Reference,
2525
type ColumnType,
26+
type ColumnsType,
2627
};
2728

2829
export default Table;

tests/FixedColumn.spec.jsx renamed to tests/FixedColumn.spec.tsx

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1+
import React from 'react';
12
import { mount } from 'enzyme';
2-
import RcResizeObserver from 'rc-resize-observer';
3+
import { render } from '@testing-library/react';
4+
import RcResizeObserver, { _rs } from 'rc-resize-observer';
35
import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
4-
import { resetWarned } from 'rc-util/lib/warning';
56
import { act } from 'react-dom/test-utils';
6-
import Table from '../src';
7+
import Table, { type ColumnsType } from '../src';
78
import { safeAct } from './utils';
89

10+
function triggerResize(ele: HTMLElement) {
11+
_rs([{ target: ele }] as any);
12+
}
13+
914
describe('Table.FixedColumn', () => {
1015
let domSpy;
1116
beforeEach(() => {
@@ -26,7 +31,7 @@ describe('Table.FixedColumn', () => {
2631
domSpy.mockRestore();
2732
});
2833

29-
const columns = [
34+
const columns: ColumnsType = [
3035
{ title: 'title1', dataIndex: 'a', key: 'a', width: 100, fixed: 'left' },
3136
{
3237
title: 'title2',
@@ -86,7 +91,7 @@ describe('Table.FixedColumn', () => {
8691
{
8792
data: wrapper.find('table ResizeObserver').first().props().data,
8893
size: { width: 93, offsetWidth: 93 },
89-
},
94+
} as any,
9095
]);
9196
});
9297
await safeAct(wrapper);
@@ -131,7 +136,7 @@ describe('Table.FixedColumn', () => {
131136
scrollWidth: 200,
132137
clientWidth: 100,
133138
},
134-
});
139+
} as any);
135140
});
136141
wrapper.update();
137142
expect(wrapper.find('.rc-table').hasClass('rc-table-ping-left')).toBeTruthy();
@@ -148,7 +153,7 @@ describe('Table.FixedColumn', () => {
148153
scrollWidth: 200,
149154
clientWidth: 100,
150155
},
151-
});
156+
} as any);
152157
});
153158
wrapper.update();
154159
expect(wrapper.find('.rc-table').hasClass('rc-table-ping-left')).toBeFalsy();
@@ -165,7 +170,7 @@ describe('Table.FixedColumn', () => {
165170
scrollWidth: 200,
166171
clientWidth: 100,
167172
},
168-
});
173+
} as any);
169174
});
170175
wrapper.update();
171176
expect(wrapper.find('.rc-table').hasClass('rc-table-ping-left')).toBeTruthy();
@@ -182,49 +187,13 @@ describe('Table.FixedColumn', () => {
182187
scrollWidth: 100,
183188
clientWidth: 100,
184189
},
185-
});
190+
} as any);
186191
});
187192
wrapper.update();
188193
expect(wrapper.find('.rc-table').hasClass('rc-table-ping-left')).toBeFalsy();
189194
expect(wrapper.find('.rc-table').hasClass('rc-table-ping-right')).toBeFalsy();
190195
});
191196

192-
describe('warning if fixed not continue', () => {
193-
let errorSpy;
194-
195-
beforeAll(() => {
196-
errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
197-
});
198-
199-
beforeEach(() => {
200-
resetWarned();
201-
errorSpy.mockReset();
202-
});
203-
204-
afterAll(() => {
205-
errorSpy.mockRestore();
206-
});
207-
208-
it('left', async () => {
209-
mount(<Table columns={[{}, { fixed: 'left' }, {}]} />);
210-
expect(errorSpy).toHaveBeenCalledWith(
211-
"Warning: Index 0 of `columns` missing `fixed='left'` prop.",
212-
);
213-
});
214-
215-
it('right', () => {
216-
mount(<Table columns={[{}, { fixed: 'right' }, {}]} />);
217-
expect(errorSpy).toHaveBeenCalledWith(
218-
"Warning: Index 2 of `columns` missing `fixed='right'` prop.",
219-
);
220-
});
221-
222-
it('RTL', () => {
223-
mount(<Table columns={[{}, { fixed: 'right' }]} direction="rtl" />);
224-
expect(errorSpy).not.toHaveBeenCalled();
225-
});
226-
});
227-
228197
it('ellipsis will wrap additional dom', () => {
229198
const myColumns = [{ ...columns[0], ellipsis: true }];
230199
const wrapper = mount(<Table columns={myColumns} data={data} />);
@@ -282,4 +251,37 @@ describe('Table.FixedColumn', () => {
282251
await safeAct(wrapper);
283252
expect(wrapper.find('.rc-table-cell-fix-left-all')).toHaveLength(10);
284253
});
254+
255+
describe('cross fixed support', () => {
256+
it('left', async () => {
257+
const { container } = render(
258+
<Table
259+
columns={[{}, { fixed: 'left' }, { fixed: 'left' }]}
260+
data={[{}]}
261+
scroll={{ x: 200 }}
262+
/>,
263+
);
264+
265+
act(() => {
266+
Array.from(container.querySelectorAll<HTMLElement>('.rc-table-measure-row td')).forEach(
267+
td => {
268+
triggerResize(td);
269+
},
270+
);
271+
});
272+
273+
await act(async () => {
274+
vi.runAllTimers();
275+
await Promise.resolve();
276+
});
277+
278+
expect(container.querySelectorAll('tbody .rc-table-cell-fix-left')).toHaveLength(2);
279+
expect(container.querySelectorAll('thead td')[1]).toHaveStyle({
280+
left: '0px',
281+
});
282+
expect(container.querySelectorAll('thead td')[2]).toHaveStyle({
283+
left: '1000px',
284+
});
285+
});
286+
});
285287
});

tests/setup.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
import type { TestingLibraryMatchers } from '@testing-library/jest-dom/matchers';
2-
import matchers from '@testing-library/jest-dom/matchers';
3-
import { expect } from 'vitest';
4-
5-
declare module 'vitest' {
6-
interface Assertion<T = any> extends jest.Matchers<void, T>, TestingLibraryMatchers<T, void> {}
7-
}
8-
9-
expect.extend(matchers);
1+
import '@testing-library/jest-dom';
102

113
// https://github.com/nickcolley/jest-axe/issues/147#issuecomment-758804533
124
const { getComputedStyle } = window;

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"@@/*": [".dumi/tmp/*"],
1313
"rc-table": ["src/index.ts"]
1414
},
15-
"types": ["vitest/globals"]
15+
"types": ["vitest/globals", "@testing-library/jest-dom"]
1616
},
1717
"include": [".dumirc.ts", "**/*.ts", "**/*.tsx"]
1818
}

0 commit comments

Comments
 (0)