Skip to content

Commit b73f79c

Browse files
authored
chore: optimize test case (#935)
1 parent 1c0d9cd commit b73f79c

File tree

4 files changed

+57
-48
lines changed

4 files changed

+57
-48
lines changed

tests/FixedColumn.spec.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ import RcResizeObserver from 'rc-resize-observer';
33
import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
44
import { resetWarned } from 'rc-util/lib/warning';
55
import { act } from 'react-dom/test-utils';
6+
import { safeAct } from './utils';
67
import Table from '../src';
78

89
describe('Table.FixedColumn', () => {
910
let domSpy;
10-
11+
beforeEach(() => {
12+
jest.useFakeTimers();
13+
});
1114
beforeAll(() => {
1215
domSpy = spyElementPrototypes(HTMLElement, {
1316
offsetParent: {
@@ -86,18 +89,14 @@ describe('Table.FixedColumn', () => {
8689
},
8790
]);
8891
});
89-
await act(async () => {
90-
jest.runAllTimers();
91-
await Promise.resolve();
92-
wrapper.update();
93-
});
92+
await safeAct(wrapper);
9493
expect(wrapper.render()).toMatchSnapshot();
9594
jest.useRealTimers();
9695
});
9796
});
9897
});
9998

100-
it('all column has width should use it', () => {
99+
it('all column has width should use it', async () => {
101100
const wrapper = mount(
102101
<Table
103102
columns={[
@@ -108,16 +107,19 @@ describe('Table.FixedColumn', () => {
108107
scroll={{ x: 'max-content' }}
109108
/>,
110109
);
110+
111+
await safeAct(wrapper);
111112

112113
expect(wrapper.find('colgroup').render()).toMatchSnapshot();
113114
});
114115
});
115116

116-
it('has correct scroll classNames when table resize', () => {
117+
it('has correct scroll classNames when table resize', async () => {
117118
const wrapper = mount(
118119
<Table columns={columns} data={data} scroll={{ x: true }} style={{ width: 2000 }} />,
119120
);
120121

122+
await safeAct(wrapper);
121123
// Use `onScroll` directly since simulate not support `currentTarget`
122124
act(() => {
123125
wrapper
@@ -203,7 +205,7 @@ describe('Table.FixedColumn', () => {
203205
errorSpy.mockRestore();
204206
});
205207

206-
it('left', () => {
208+
it('left', async () => {
207209
mount(<Table columns={[{}, { fixed: 'left' }, {}]} />);
208210
expect(errorSpy).toHaveBeenCalledWith(
209211
"Warning: Index 0 of `columns` missing `fixed='left'` prop.",
@@ -231,11 +233,12 @@ describe('Table.FixedColumn', () => {
231233
expect(wrapper.find('tr td').find('.rc-table-cell-content')).toHaveLength(data.length);
232234
});
233235

234-
it('fixed column renders correctly RTL', () => {
236+
it('fixed column renders correctly RTL', async () => {
235237
const wrapper = mount(
236238
<Table columns={columns} data={data} direction="rtl" scroll={{ x: 1 }} />,
237239
);
238240
expect(wrapper.render()).toMatchSnapshot();
241+
await safeAct(wrapper);
239242
});
240243

241244
it('has correct scroll classNames when table direction is RTL', () => {
@@ -264,17 +267,19 @@ describe('Table.FixedColumn', () => {
264267
).toBeTruthy();
265268
});
266269

267-
it('not break measure count', () => {
270+
it('not break measure count', async () => {
268271
const wrapper = mount(<Table columns={columns.slice(0, 5)} data={data} scroll={{ x: 1000 }} />);
272+
await safeAct(wrapper);
269273
expect(wrapper.find('.rc-table-measure-row td')).toHaveLength(5);
270274

271275
wrapper.setProps({ columns: columns.slice(0, 4) });
272276
wrapper.update();
273277
expect(wrapper.find('.rc-table-measure-row td')).toHaveLength(4);
274278
});
275279

276-
it('when all columns fixed left,cell should has classname rc-table-cell-fix-left-all', () => {
280+
it('when all columns fixed left,cell should has classname rc-table-cell-fix-left-all', async () => {
277281
const wrapper = mount(<Table columns={columns.slice(0, 2)} data={data} scroll={{ x: 1000 }} />);
282+
await safeAct(wrapper);
278283
expect(wrapper.find('.rc-table-cell-fix-left-all')).toHaveLength(10);
279284
});
280285
});

tests/FixedHeader.spec.js

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import RcResizeObserver from 'rc-resize-observer';
33
import { spyElementPrototype } from 'rc-util/lib/test/domHook';
44
import React from 'react';
55
import { act } from 'react-dom/test-utils';
6+
import { safeAct } from './utils';
67
import Table, { INTERNAL_COL_DEFINE } from '../src';
78

89
describe('Table.FixedHeader', () => {
@@ -16,6 +17,7 @@ describe('Table.FixedHeader', () => {
1617
});
1718

1819
beforeEach(() => {
20+
jest.useFakeTimers();
1921
visible = true;
2022
});
2123

@@ -24,7 +26,6 @@ describe('Table.FixedHeader', () => {
2426
});
2527

2628
it('should work', async () => {
27-
jest.useFakeTimers();
2829
const col1 = { dataIndex: 'light', width: 100 };
2930
const col2 = { dataIndex: 'bamboo', width: 200 };
3031
const col3 = { dataIndex: 'empty', width: 0 };
@@ -35,7 +36,6 @@ describe('Table.FixedHeader', () => {
3536
scroll={{ y: 10 }}
3637
/>,
3738
);
38-
3939
wrapper
4040
.find(RcResizeObserver.Collection)
4141
.first()
@@ -54,12 +54,7 @@ describe('Table.FixedHeader', () => {
5454
size: { width: 0, offsetWidth: 0 },
5555
},
5656
]);
57-
58-
await act(async () => {
59-
jest.runAllTimers();
60-
await Promise.resolve();
61-
wrapper.update();
62-
});
57+
await safeAct(wrapper);
6358

6459
expect(wrapper.find('.rc-table-header table').props().style.visibility).toBeFalsy();
6560

@@ -78,7 +73,7 @@ describe('Table.FixedHeader', () => {
7873
jest.useRealTimers();
7974
});
8075

81-
it('INTERNAL_COL_DEFINE', () => {
76+
it('INTERNAL_COL_DEFINE', async () => {
8277
const col1 = {
8378
dataIndex: 'light',
8479
width: 100,
@@ -92,6 +87,7 @@ describe('Table.FixedHeader', () => {
9287
scroll={{ y: 10 }}
9388
/>,
9489
);
90+
await safeAct(wrapper);
9591

9692
expect(wrapper.find('table').last().find('colgroup col').first().props().className).toEqual(
9793
'test-internal',
@@ -101,7 +97,7 @@ describe('Table.FixedHeader', () => {
10197
);
10298
});
10399

104-
it('show header when data is null', () => {
100+
it('show header when data is null', async () => {
105101
const columns = [
106102
{
107103
title: 'Name',
@@ -125,13 +121,14 @@ describe('Table.FixedHeader', () => {
125121
}}
126122
/>,
127123
);
128-
124+
125+
await safeAct(wrapper);
129126
expect(wrapper.find('.rc-table-header table').props().style).toEqual(
130127
expect.objectContaining({ visibility: null }),
131128
);
132129
});
133130

134-
it('rtl', () => {
131+
it('rtl', async () => {
135132
const wrapper = mount(
136133
<Table
137134
columns={[{ dataIndex: 'light', width: 100 }]}
@@ -142,6 +139,7 @@ describe('Table.FixedHeader', () => {
142139
}}
143140
/>,
144141
);
142+
await safeAct(wrapper);
145143

146144
expect(wrapper.find('Header').props().stickyOffsets).toEqual(
147145
expect.objectContaining({
@@ -152,8 +150,6 @@ describe('Table.FixedHeader', () => {
152150
});
153151

154152
it('invisible should not change width', async () => {
155-
jest.useFakeTimers();
156-
157153
const col1 = { dataIndex: 'light', width: 93 };
158154
const wrapper = mount(
159155
<Table
@@ -173,11 +169,7 @@ describe('Table.FixedHeader', () => {
173169
size: { width: 93, offsetWidth: 93 },
174170
},
175171
]);
176-
await act(async () => {
177-
jest.runAllTimers();
178-
await Promise.resolve();
179-
wrapper.update();
180-
});
172+
await safeAct(wrapper);
181173

182174
expect(wrapper.find('FixedHolder col').first().props().style).toEqual(
183175
expect.objectContaining({ width: 93 }),
@@ -209,7 +201,7 @@ describe('Table.FixedHeader', () => {
209201
jest.useRealTimers();
210202
});
211203

212-
it('do not mask as ant-table-cell-fix-left-last in nested table parent cell', () => {
204+
it('do not mask as ant-table-cell-fix-left-last in nested table parent cell', async () => {
213205
const columns = [
214206
{
215207
title: '父表头右侧的阴影导致整个表格最右侧有空隙',
@@ -262,6 +254,7 @@ describe('Table.FixedHeader', () => {
262254
scroll={{ x: true }}
263255
/>,
264256
);
257+
await safeAct(wrapper);
265258
expect(wrapper.find('td').at(9).props().className).toContain('rc-table-cell-fix-left-last');
266259
expect(wrapper.find('th').first().props().className).not.toContain('rc-table-cell-fix-left-last');
267260

tests/Sticky.spec.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ import { act } from 'react-dom/test-utils';
33
import { mount } from 'enzyme';
44
import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
55
import Table from '../src';
6+
import { safeAct } from './utils';
67

78
describe('Table.Sticky', () => {
8-
it('Sticky Header', () => {
9+
beforeEach(() => {
910
jest.useFakeTimers();
11+
});
12+
it('Sticky Header', async () => {
1013
const col1 = { dataIndex: 'light', width: 100 };
1114
const col2 = { dataIndex: 'bamboo', width: 200 };
1215

@@ -37,10 +40,12 @@ describe('Table.Sticky', () => {
3740
'rc-table-header rc-table-sticky-holder',
3841
);
3942

40-
wrapper.setProps({
41-
sticky: {
42-
offsetHeader: 10,
43-
},
43+
await safeAct(wrapper, () => {
44+
wrapper.setProps({
45+
sticky: {
46+
offsetHeader: 10,
47+
},
48+
});
4449
});
4550

4651
expect(wrapper.find('.rc-table-header').last().prop('style')).toEqual({
@@ -52,7 +57,6 @@ describe('Table.Sticky', () => {
5257
});
5358

5459
it('Sticky scroll', async () => {
55-
jest.useFakeTimers();
5660
window.pageYOffset = 900;
5761
document.documentElement.scrollTop = 200;
5862
let scrollLeft = 100;
@@ -208,8 +212,7 @@ describe('Table.Sticky', () => {
208212
jest.useRealTimers();
209213
});
210214

211-
it('Sticky Header with border classname', () => {
212-
jest.useFakeTimers();
215+
it('Sticky Header with border classname', async () => {
213216

214217
const TableDemo = props => {
215218
return (
@@ -239,21 +242,19 @@ describe('Table.Sticky', () => {
239242
);
240243
};
241244
const wrapper = mount(<TableDemo />);
242-
245+
await safeAct(wrapper);
243246
expect(
244247
wrapper.find('.rc-table-cell-fix-right-first.rc-table-cell-fix-sticky').prop('style'),
245248
).toEqual({
246249
position: 'sticky',
247250
right: 0,
248251
});
249-
250252
expect(wrapper.find('.rc-table-cell-fix-sticky')).not.toBe(undefined);
251253

252254
jest.useRealTimers();
253255
});
254256

255-
it('Sticky Header with scroll-y', () => {
256-
jest.useFakeTimers();
257+
it('Sticky Header with scroll-y', async () => {
257258

258259
const TableDemo = props => {
259260
return (
@@ -284,7 +285,7 @@ describe('Table.Sticky', () => {
284285
);
285286
};
286287
const wrapper = mount(<TableDemo />);
287-
288+
await safeAct(wrapper);
288289
expect(
289290
wrapper.find('.rc-table-cell-fix-right-first.rc-table-cell-fix-sticky').prop('style'),
290291
).toEqual({
@@ -296,10 +297,10 @@ describe('Table.Sticky', () => {
296297
});
297298

298299
it('Sticky scroll with getContainer', async () => {
299-
jest.useFakeTimers();
300+
300301
window.pageYOffset = 900;
301302
document.documentElement.scrollTop = 200;
302-
const container = document.createElement('p');
303+
const container = document.createElement('ol');
303304
container.style = 'height: 500px;overflow: scroll';
304305
document.body.appendChild(container);
305306
let scrollLeft = 100;
@@ -324,7 +325,7 @@ describe('Table.Sticky', () => {
324325
},
325326
});
326327

327-
const sectionSpy = spyElementPrototypes(HTMLParagraphElement, {
328+
const sectionSpy = spyElementPrototypes(HTMLOListElement, {
328329
scrollLeft: {
329330
get: () => scrollLeft,
330331
set: left => {

tests/utils.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { act } from 'react-dom/test-utils';
2+
3+
export function safeAct(wrapper, cb) {
4+
return act(async () => {
5+
cb && cb();
6+
jest.runAllTimers();
7+
await Promise.resolve();
8+
wrapper.update();
9+
});
10+
}

0 commit comments

Comments
 (0)