Files
monkey-ai-web/tests/modal-overflow.test.js
zhanjunhao 2aa9d73325 chore(repo): 移除重复版权文件头并纳入共享单测
集中保留原版权声明,移除文件头检查和全局测试忽略规则。验证:全量单测、受影响用例复测及静态检查通过。
2026-11-20 11:23:49 +08:00

203 lines
5.5 KiB
JavaScript

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 === '../console/ConsoleContext')
return { useConsolePage: () => null };
if (name === '../console/ConsolePrimitives') return {};
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));
});
});
});