Skip to content

Commit 3462c8f

Browse files
chenJJ-88claude
andcommitted
fix: 修复测试基础设施和 biome 警告,确保 CI 通过
- 添加 afterEach(cleanup) 防止 DOM 泄漏 - 添加 window.matchMedia mock 支持 antd - 修复验证测试使用组件内置错误渲染 - 移除未使用的导入 (fireEvent, within, React) - 替换非空断言 ref.current! 为可选链 ref.current? - 修复 biome 格式化问题 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent 3a0e778 commit 3462c8f

7 files changed

Lines changed: 65 additions & 59 deletions

File tree

packages/editable-table/package.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,23 @@
1212
},
1313
"./style.css": "./dist/EditableTable.css"
1414
},
15-
"files": ["dist"],
15+
"files": [
16+
"dist"
17+
],
1618
"scripts": {
1719
"build": "tsup",
1820
"prepublishOnly": "pnpm run build",
1921
"type-check": "tsc --noEmit",
2022
"test": "vitest",
2123
"coverage": "vitest run --coverage"
2224
},
23-
"keywords": ["react", "editable-table", "table", "virtual-scroll", "formily"],
25+
"keywords": [
26+
"react",
27+
"editable-table",
28+
"table",
29+
"virtual-scroll",
30+
"formily"
31+
],
2432
"author": "chenJJ-88",
2533
"license": "MIT",
2634
"repository": {

packages/editable-table/src/components/EditableTable/__tests__/EditableTable.test.tsx

Lines changed: 13 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
* - Chinese labels (保存/取消/编辑/提交) match the component's own button text.
66
* - `userEvent` is used for realistic interactions; `fireEvent` only where needed.
77
*/
8-
import { act, fireEvent, render, screen, within } from '@testing-library/react';
8+
import { act, render, screen } from '@testing-library/react';
99
import userEvent from '@testing-library/user-event';
10-
import React, { createRef } from 'react';
10+
import { createRef } from 'react';
1111
import { describe, expect, it, vi } from 'vitest';
1212
import EditableTable, { type EditableColumn, type EditableTableInstance } from '../index';
1313

@@ -31,22 +31,14 @@ const BASE_COLUMNS: EditableColumn<Person>[] = [
3131
title: '姓名',
3232
dataIndex: 'name',
3333
editRender: ({ value, onChange }) => (
34-
<input
35-
aria-label="name-input"
36-
value={String(value ?? '')}
37-
onChange={(e) => onChange(e.target.value)}
38-
/>
34+
<input aria-label="name-input" value={String(value ?? '')} onChange={(e) => onChange(e.target.value)} />
3935
),
4036
},
4137
{
4238
title: '年龄',
4339
dataIndex: 'age',
4440
editRender: ({ value, onChange }) => (
45-
<input
46-
aria-label="age-input"
47-
value={String(value ?? '')}
48-
onChange={(e) => onChange(e.target.value)}
49-
/>
41+
<input aria-label="age-input" value={String(value ?? '')} onChange={(e) => onChange(e.target.value)} />
5042
),
5143
},
5244
];
@@ -228,15 +220,8 @@ describe('validation', () => {
228220
title: '姓名',
229221
dataIndex: 'name',
230222
rules: [{ required: true, message: '姓名为必填项' }],
231-
editRender: ({ value, onChange, error }) => (
232-
<div>
233-
<input
234-
aria-label="name-input"
235-
value={String(value ?? '')}
236-
onChange={(e) => onChange(e.target.value)}
237-
/>
238-
{error && <span role="alert">{error}</span>}
239-
</div>
223+
editRender: ({ value, onChange }) => (
224+
<input aria-label="name-input" value={String(value ?? '')} onChange={(e) => onChange(e.target.value)} />
240225
),
241226
},
242227
];
@@ -254,8 +239,7 @@ describe('validation', () => {
254239

255240
await userEvent.click(screen.getByRole('button', { name: '提交' }));
256241

257-
expect(await screen.findAllByRole('alert')).not.toHaveLength(0);
258-
expect(screen.getByText('姓名为必填项')).toBeInTheDocument();
242+
expect(await screen.findByText('姓名为必填项')).toBeInTheDocument();
259243
});
260244

261245
it('shows validation error on change when validateTrigger is change', async () => {
@@ -302,17 +286,11 @@ describe('ref methods', () => {
302286
it('addRow appends a new row', async () => {
303287
const ref = createRef<EditableTableInstance<Person>>();
304288
render(
305-
<EditableTable<Person>
306-
ref={ref}
307-
rowKey="id"
308-
columns={BASE_COLUMNS}
309-
dataSource={DATA}
310-
editableMode="all"
311-
/>,
289+
<EditableTable<Person> ref={ref} rowKey="id" columns={BASE_COLUMNS} dataSource={DATA} editableMode="all" />,
312290
);
313291

314292
act(() => {
315-
ref.current!.addRow({ id: '99', name: '新行', age: 0 });
293+
ref.current?.addRow({ id: '99', name: '新行', age: 0 });
316294
});
317295

318296
const nameInputs = screen.getAllByLabelText('name-input');
@@ -322,17 +300,11 @@ describe('ref methods', () => {
322300
it('removeRow removes the correct row', async () => {
323301
const ref = createRef<EditableTableInstance<Person>>();
324302
render(
325-
<EditableTable<Person>
326-
ref={ref}
327-
rowKey="id"
328-
columns={BASE_COLUMNS}
329-
dataSource={DATA}
330-
editableMode="all"
331-
/>,
303+
<EditableTable<Person> ref={ref} rowKey="id" columns={BASE_COLUMNS} dataSource={DATA} editableMode="all" />,
332304
);
333305

334306
act(() => {
335-
ref.current!.removeRow(0);
307+
ref.current?.removeRow(0);
336308
});
337309

338310
const nameInputs = screen.getAllByLabelText('name-input') as HTMLInputElement[];
@@ -344,15 +316,9 @@ describe('ref methods', () => {
344316
it('getData returns the current data', () => {
345317
const ref = createRef<EditableTableInstance<Person>>();
346318
render(
347-
<EditableTable<Person>
348-
ref={ref}
349-
rowKey="id"
350-
columns={BASE_COLUMNS}
351-
dataSource={DATA}
352-
editableMode="all"
353-
/>,
319+
<EditableTable<Person> ref={ref} rowKey="id" columns={BASE_COLUMNS} dataSource={DATA} editableMode="all" />,
354320
);
355321

356-
expect(ref.current!.getData()).toEqual(DATA);
322+
expect(ref.current?.getData()).toEqual(DATA);
357323
});
358324
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1+
import { cleanup } from '@testing-library/react';
12
import '@testing-library/jest-dom/vitest';
3+
import { afterEach } from 'vitest';
4+
5+
afterEach(cleanup);

packages/editable-table/src/utils/__tests__/validate.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,7 @@ describe('validateRow', () => {
173173
});
174174

175175
it('ignores columns without rules', () => {
176-
const noRuleColumns: EditableColumn<Row>[] = [
177-
{ title: '邮箱', dataIndex: 'email' },
178-
];
176+
const noRuleColumns: EditableColumn<Row>[] = [{ title: '邮箱', dataIndex: 'email' }];
179177
const row: Row = { name: '', age: null, email: '' };
180178
expect(validateRow(row, noRuleColumns)).toEqual({});
181179
});

packages/fast-editable-table/package.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,23 @@
1111
"types": "./dist/index.d.ts"
1212
}
1313
},
14-
"files": ["dist"],
14+
"files": [
15+
"dist"
16+
],
1517
"scripts": {
1618
"build": "tsup",
1719
"prepublishOnly": "pnpm run build",
1820
"type-check": "tsc --noEmit",
1921
"test": "vitest",
2022
"coverage": "vitest run --coverage"
2123
},
22-
"keywords": ["react", "editable-table", "formily", "antd", "table"],
24+
"keywords": [
25+
"react",
26+
"editable-table",
27+
"formily",
28+
"antd",
29+
"table"
30+
],
2331
"author": "chenJJ-88",
2432
"license": "MIT",
2533
"repository": {

packages/fast-editable-table/src/__tests__/FormilyEditableTable.test.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
*/
77
import { createForm } from '@formily/core';
88
import { FormProvider } from '@formily/react';
9-
import { act, render, screen, within } from '@testing-library/react';
9+
import { act, render, screen } from '@testing-library/react';
1010
import userEvent from '@testing-library/user-event';
11-
import * as React from 'react';
11+
import type * as React from 'react';
1212
import { describe, expect, it } from 'vitest';
1313
import { FormilyEditableTable } from '../FormilyEditableTable';
1414
import type { IColumn } from '../types';
@@ -29,7 +29,7 @@ function makeColumns(): IColumn[] {
2929
return [
3030
{
3131
title: '姓名',
32-
render: ({ index, field }) => (
32+
render: ({ index, field: _field }) => (
3333
<FormilyEditableTable.Field name="name">
3434
<input aria-label={`name-${index}`} />
3535
</FormilyEditableTable.Field>
@@ -42,7 +42,10 @@ function makeColumns(): IColumn[] {
4242
];
4343
}
4444

45-
function renderTable(form: ReturnType<typeof createForm>, overrides: Partial<React.ComponentProps<typeof FormilyEditableTable>> = {}) {
45+
function renderTable(
46+
form: ReturnType<typeof createForm>,
47+
overrides: Partial<React.ComponentProps<typeof FormilyEditableTable>> = {},
48+
) {
4649
return render(
4750
<FormProvider form={form}>
4851
<FormilyEditableTable
@@ -169,7 +172,7 @@ describe('remove row', () => {
169172
arrayField.remove(0);
170173
});
171174

172-
const remaining = await screen.findByLabelText('name-0') as HTMLInputElement;
175+
const remaining = (await screen.findByLabelText('name-0')) as HTMLInputElement;
173176
expect(remaining.value).toBe('李四');
174177
expect(screen.queryByLabelText('name-1')).not.toBeInTheDocument();
175178
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,20 @@
1+
import { cleanup } from '@testing-library/react';
12
import '@testing-library/jest-dom/vitest';
3+
import { afterEach, vi } from 'vitest';
4+
5+
// antd requires matchMedia
6+
Object.defineProperty(window, 'matchMedia', {
7+
writable: true,
8+
value: vi.fn().mockImplementation((query: string) => ({
9+
matches: false,
10+
media: query,
11+
onchange: null,
12+
addListener: vi.fn(),
13+
removeListener: vi.fn(),
14+
addEventListener: vi.fn(),
15+
removeEventListener: vi.fn(),
16+
dispatchEvent: vi.fn(),
17+
})),
18+
});
19+
20+
afterEach(cleanup);

0 commit comments

Comments
 (0)