151 lines
5.7 KiB
JavaScript
151 lines
5.7 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 { readFileSync } from 'node:fs';
|
|
import test from 'node:test';
|
|
import postcss from 'postcss';
|
|
import { readFile } from 'node:fs/promises';
|
|
import { runInNewContext } from 'node:vm';
|
|
import React from 'react';
|
|
import { renderToStaticMarkup } from 'react-dom/server';
|
|
import * as Ant from 'antd';
|
|
import { transformWithEsbuild } from 'vite';
|
|
|
|
const css = readFileSync(new URL('../src/styles/prototype.css', import.meta.url), 'utf8');
|
|
const root = postcss.parse(css);
|
|
|
|
const normalizeSelector = (selector) => selector.replace(/\s+/g, ' ').trim();
|
|
|
|
const findRule = (selector) => {
|
|
const expected = normalizeSelector(selector);
|
|
let match;
|
|
root.walkRules((rule) => {
|
|
if (
|
|
rule.selectors?.some(
|
|
(candidate) => normalizeSelector(candidate) === expected,
|
|
)
|
|
) {
|
|
match = rule;
|
|
}
|
|
});
|
|
assert.ok(match, `Missing CSS rule for ${selector}`);
|
|
return match;
|
|
};
|
|
|
|
const declarations = (rule) =>
|
|
Object.fromEntries(
|
|
rule.nodes
|
|
.filter((node) => node.type === 'decl')
|
|
.map((node) => [node.prop, node.value]),
|
|
);
|
|
|
|
test('standard modal bodies only scroll vertically', () => {
|
|
const rule = findRule(
|
|
'.ant-modal:not(.viewport-preview-modal) .ant-modal-body',
|
|
);
|
|
assert.deepEqual(
|
|
{
|
|
overflowX: declarations(rule)['overflow-x'],
|
|
overflowY: declarations(rule)['overflow-y'],
|
|
},
|
|
{ overflowX: 'hidden', overflowY: 'auto' },
|
|
);
|
|
});
|
|
|
|
test('fixed-width modal shells stay inside the viewport', () => {
|
|
assert.equal(declarations(findRule('.ant-modal'))['max-width'], 'calc(100vw - 32px)');
|
|
assert.equal(declarations(findRule('.viewport-preview-modal.ant-modal'))['max-width'], '100vw');
|
|
});
|
|
|
|
test('headerless viewport previews override standard body overflow', () => {
|
|
const rule = findRule(
|
|
'.viewport-preview-modal .ant-modal-body',
|
|
);
|
|
assert.deepEqual(
|
|
{
|
|
maxWidth: declarations(rule)['max-width'],
|
|
overflowX: declarations(rule)['overflow-x'],
|
|
overflowY: declarations(rule)['overflow-y'],
|
|
},
|
|
{ maxWidth: 'none', overflowX: 'visible', overflowY: 'visible' },
|
|
);
|
|
});
|
|
|
|
const file = new URL('../src/components/ui/form.jsx', import.meta.url);
|
|
const { code } = await transformWithEsbuild(await readFile(file, 'utf8'), file.pathname, {
|
|
loader: 'jsx', format: 'cjs', jsx: 'transform',
|
|
});
|
|
const module = { exports: {} };
|
|
runInNewContext(code, {
|
|
module, exports: module.exports,
|
|
require(name) {
|
|
if (name === 'react') return React;
|
|
if (name === 'antd') return Ant;
|
|
if (name === './controls') return Object.fromEntries(
|
|
['Input', 'TextArea', 'InputNumber', 'Select', 'DatePicker', 'TimePicker', 'RadioGroup', 'CheckboxGroup', 'TagInput', 'TreeSelect', 'AutoComplete', 'Upload', 'Switch'].map(key => [key, Ant.Input]),
|
|
);
|
|
throw new Error(`Unexpected dependency: ${name}`);
|
|
},
|
|
});
|
|
const { Form } = module.exports;
|
|
const render = (props) => renderToStaticMarkup(
|
|
React.createElement(Form, { labelPosition: 'left', ...props },
|
|
React.createElement(Form.Input, { field: 'sample', label: 'Setting label' })),
|
|
);
|
|
|
|
for (const [width, css] of [[180, '180px'], ['120px', '120px'], ['25%', '25%']]) {
|
|
test(`form label width ${width} keeps its unit and cannot grow across the row`, () => {
|
|
const html = render({ labelWidth: width });
|
|
assert.ok(html.includes(`flex:0 0 ${css}`));
|
|
assert.ok(html.includes('flex:1 1 0'));
|
|
});
|
|
}
|
|
test('explicit Ant Design column settings take precedence over legacy labelWidth', () => {
|
|
const html = render({ labelWidth: 180, labelCol: { flex: '0 0 96px' }, wrapperCol: { flex: '1 1 240px' } });
|
|
assert.ok(html.includes('flex:0 0 96px'));
|
|
assert.ok(html.includes('flex:1 1 240px'));
|
|
assert.ok(!html.includes('flex:0 0 180px'));
|
|
});
|
|
test('vertical forms without label widths keep the default Ant layout', () => {
|
|
const html = render({ labelPosition: 'top' });
|
|
assert.match(html, /ant-form-vertical/);
|
|
assert.ok(!html.includes('flex:0 0'));
|
|
});
|
|
|
|
const stylesheet = postcss.parse(await readFile(new URL('../src/pages/Setting/Setting.css', import.meta.url), 'utf8'));
|
|
test('settings labels stack on narrow screens without overriding inline flex declarations', () => {
|
|
let mobileRule;
|
|
stylesheet.walkRules(rule => {
|
|
if (rule.selectors.includes('.settings-page .ant-form-horizontal .ant-form-item-row')) mobileRule = rule;
|
|
});
|
|
assert.ok(mobileRule.selectors.includes('.settings-form-dialog .ant-form-horizontal .ant-form-item-row'));
|
|
assert.match(mobileRule.parent.params, /max-width: 767px/);
|
|
assert.ok(mobileRule.nodes.some(node => node.prop === 'display' && node.value === 'block'));
|
|
});
|
|
test('settings layout rules stay scoped and use the existing scrollbar policy', () => {
|
|
stylesheet.walkRules(rule => {
|
|
assert.ok(rule.selectors.every(selector => selector.startsWith('.settings-page') || selector.startsWith('.settings-form-dialog')));
|
|
rule.walkDecls(decl => {
|
|
assert.equal(Boolean(decl.important), false);
|
|
assert.ok(!['scrollbar-width', 'scrollbar-color'].includes(decl.prop));
|
|
});
|
|
});
|
|
});
|