cb9d836227
集中保留原版权声明,移除文件头检查和全局测试忽略规则。验证:全量单测、受影响用例复测及静态检查通过。
227 lines
7.1 KiB
JavaScript
227 lines
7.1 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import { readFile } from 'node:fs/promises';
|
|
import test from 'node:test';
|
|
import React from 'react';
|
|
import { transformWithEsbuild } from 'vite';
|
|
|
|
async function load(relativePath, mocks) {
|
|
const filename = new URL(relativePath, import.meta.url);
|
|
const { code } = await transformWithEsbuild(
|
|
await readFile(filename, 'utf8'),
|
|
filename.pathname,
|
|
{ loader: 'jsx', format: 'cjs' },
|
|
);
|
|
const module = { exports: {} };
|
|
new Function('require', 'module', 'exports', code)(
|
|
(name) => {
|
|
assert.ok(Object.hasOwn(mocks, name), `Missing dependency: ${name}`);
|
|
return mocks[name];
|
|
},
|
|
module,
|
|
module.exports,
|
|
);
|
|
return module.exports;
|
|
}
|
|
|
|
const Input = React.forwardRef(function Input() {
|
|
return null;
|
|
});
|
|
const { Form } = await load('../../src/components/ui/form.jsx', {
|
|
react: { ...React, useContext: () => null },
|
|
antd: { Form: { Item: 'form-item' }, Checkbox: { Group: Input } },
|
|
'./controls': Object.fromEntries(
|
|
[
|
|
'Input',
|
|
'TextArea',
|
|
'InputNumber',
|
|
'Select',
|
|
'DatePicker',
|
|
'TimePicker',
|
|
'RadioGroup',
|
|
'TagInput',
|
|
'TreeSelect',
|
|
'AutoComplete',
|
|
'Upload',
|
|
'Switch',
|
|
].map((name) => [name, Input]),
|
|
),
|
|
'../console/ConsoleContext': {},
|
|
'../console/ConsolePrimitives': {},
|
|
});
|
|
const { ConsoleFilterField } = await load(
|
|
'../../src/components/console/ConsoleModuleFilters.jsx',
|
|
{
|
|
react: { ...React, useId: () => 'generated-filter-id' },
|
|
antd: {},
|
|
'react-i18next': {},
|
|
'../common/table/FilterSearchButton': {},
|
|
'./module-alignment.css': {},
|
|
},
|
|
);
|
|
|
|
test('form labels and controls share explicit IDs and legacy label names', () => {
|
|
for (const props of [
|
|
{ field: 'name', label: 'Name', id: 'custom-name' },
|
|
{ field: 'name', label: { text: 'Name', name: 'legacy-name' } },
|
|
{
|
|
field: 'name',
|
|
label: { text: 'Name', name: 'legacy-name' },
|
|
id: 'explicit-name',
|
|
},
|
|
]) {
|
|
const item = Form.Input.render(props, null);
|
|
assert.equal(item.props.htmlFor, props.id ?? props.label.name);
|
|
assert.equal(item.props.children.props.id, item.props.htmlFor);
|
|
assert.deepEqual(item.props.name, ['name']);
|
|
}
|
|
});
|
|
|
|
test('normal fields keep Ant Form automatic IDs and unnamed slots retain child IDs', () => {
|
|
const normal = Form.Input.render({ field: 'name', label: 'Name' }, null);
|
|
assert.equal(normal.props.htmlFor, undefined);
|
|
assert.equal(Object.hasOwn(normal.props, 'htmlFor'), false);
|
|
assert.equal(normal.props.children.props.id, undefined);
|
|
const child = React.createElement(Input, { id: 'slot-input' });
|
|
const slot = Form.Input.render({ label: 'Slot', children: child }, null);
|
|
assert.equal(slot.props.htmlFor, 'slot-input');
|
|
assert.equal(slot.props.children, child);
|
|
});
|
|
|
|
test('radio group labels target an enabled input and preserve value callbacks', () => {
|
|
const values = [];
|
|
const item = Form.RadioGroup.render(
|
|
{
|
|
field: 'mode',
|
|
label: 'Mode',
|
|
onChange: (value) => values.push(value),
|
|
options: [
|
|
{ label: 'Disabled', value: 'disabled', disabled: true },
|
|
{ label: 'Observe', value: 'observe' },
|
|
],
|
|
},
|
|
null,
|
|
);
|
|
const control = item.props.children;
|
|
const group = control.type.render({ ...control.props, id: 'mode' }, null);
|
|
assert.equal(group.props.id, 'mode-group');
|
|
assert.equal(group.props.options[1].id, 'mode');
|
|
assert.equal(group.props.options[0].id, undefined);
|
|
assert.equal(group.props['aria-label'], 'Mode');
|
|
group.props.onChange({ target: { value: 'observe' } });
|
|
assert.deepEqual(values, ['observe']);
|
|
});
|
|
|
|
test('single filter labels preserve existing control IDs', () => {
|
|
for (const id of [undefined, 'existing-input']) {
|
|
const element = ConsoleFilterField({
|
|
label: 'Search',
|
|
children: React.createElement(Input, { id }),
|
|
});
|
|
const [label, control] = element.props.children;
|
|
assert.equal(label.type, 'label');
|
|
assert.equal(label.props.htmlFor, id ?? 'generated-filter-id');
|
|
assert.equal(control.props.id, label.props.htmlFor);
|
|
}
|
|
});
|
|
|
|
test('composite filters use an accessible group instead of labeling a container', () => {
|
|
for (const type of ['div', React.Fragment]) {
|
|
const children = React.createElement(type, null, [
|
|
React.createElement(Input, { key: 'min' }),
|
|
React.createElement(Input, { key: 'max' }),
|
|
]);
|
|
const element = ConsoleFilterField({ label: 'Amount', children });
|
|
assert.equal(element.props.role, 'group');
|
|
const [label, control] = element.props.children;
|
|
assert.equal(label.type, 'span');
|
|
assert.equal(label.props.htmlFor, undefined);
|
|
assert.equal(element.props['aria-labelledby'], label.props.id);
|
|
assert.equal(control, children);
|
|
}
|
|
});
|
|
|
|
test('static forms rely on field subscriptions while render props receive updates', async () => {
|
|
for (const renderProp of [false, true]) {
|
|
let renders = 0;
|
|
let api;
|
|
let store = {};
|
|
const updates = [];
|
|
const effects = [];
|
|
const form = {
|
|
getFieldsValue: () => store,
|
|
getFieldValue: (path) => store[path[0]],
|
|
setFieldsValue: (next) => {
|
|
store = { ...store, ...next };
|
|
},
|
|
resetFields: () => {
|
|
store = {};
|
|
},
|
|
};
|
|
const { Form: TestForm } = await load('../../src/components/ui/form.jsx', {
|
|
react: {
|
|
...React,
|
|
useContext: () => null,
|
|
useReducer: () => [
|
|
0,
|
|
() => {
|
|
renders++;
|
|
},
|
|
],
|
|
useRef: (current) => ({ current }),
|
|
useMemo: (factory) => factory(),
|
|
useEffect: (effect) => effects.push(effect),
|
|
useImperativeHandle: () => {},
|
|
},
|
|
antd: {
|
|
Form: Object.assign(() => null, { useForm: () => [form] }),
|
|
Checkbox: { Group: Input },
|
|
},
|
|
'./controls': Object.fromEntries(
|
|
[
|
|
'Input',
|
|
'TextArea',
|
|
'InputNumber',
|
|
'Select',
|
|
'DatePicker',
|
|
'TimePicker',
|
|
'RadioGroup',
|
|
'TagInput',
|
|
'TreeSelect',
|
|
'AutoComplete',
|
|
'Upload',
|
|
'Switch',
|
|
].map((name) => [name, Input]),
|
|
),
|
|
'../console/ConsoleContext': { useConsolePage: () => null },
|
|
'../console/ConsolePrimitives': {},
|
|
});
|
|
const tree = TestForm.render(
|
|
{
|
|
children: renderProp ? () => null : React.createElement('span'),
|
|
values: { initial: true },
|
|
getFormApi: (next) => {
|
|
api = next;
|
|
},
|
|
onValueChange: (values) => updates.push(values),
|
|
labelAlign: 'left',
|
|
},
|
|
null,
|
|
);
|
|
effects.forEach((effect) => effect());
|
|
assert.equal(tree.props.children.props.labelAlign, 'start');
|
|
assert.deepEqual(api.getValues(), { initial: true });
|
|
api.setValue('name', 'first');
|
|
api.setValues({ name: 'second' });
|
|
assert.equal(api.getValue('name'), 'second');
|
|
api.reset();
|
|
tree.props.children.props.onValuesChange({}, { name: 'typed' });
|
|
assert.deepEqual(updates, [
|
|
{ initial: true, name: 'first' },
|
|
{ initial: true, name: 'second' },
|
|
{},
|
|
{ name: 'typed' },
|
|
]);
|
|
assert.equal(renders, renderProp ? 6 : 0);
|
|
}
|
|
});
|