137 lines
4.6 KiB
JavaScript
137 lines
4.6 KiB
JavaScript
/*
|
|
Copyright (C) 2025 QuantumNous
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Affero General Public License as
|
|
published by the Free Software Foundation, either version 3 of the
|
|
License, or (at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Affero General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
For commercial licensing, please contact support@quantumnous.com
|
|
*/
|
|
|
|
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('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);
|
|
}
|
|
});
|