Files
order_site/apps/frontend/tests/percent-table-layout.test.ts
T

78 lines
2.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import assert from 'node:assert/strict'
import fs from 'node:fs'
import path from 'node:path'
import test from 'node:test'
import type { TableColumnsType } from 'antd'
import { normalizePercentColumns } from '../src/utils/percent-table-layout.ts'
test('百分比表格:按原列宽权重归一化到 100%', () => {
const columns: TableColumnsType<Record<string, unknown>> = [
{ title: 'A', dataIndex: 'a', width: 100 },
{ title: 'B', dataIndex: 'b', width: 300 },
]
const result = normalizePercentColumns(columns)
assert.deepEqual(
result.map((column) => column.width),
['25.0000%', '75.0000%'],
)
assert.ok(result.every((column) => column.align === 'center'))
})
test('百分比表格:没有列宽时平均分配', () => {
const columns: TableColumnsType<Record<string, unknown>> = [
{ title: 'A', dataIndex: 'a' },
{ title: 'B', dataIndex: 'b' },
]
assert.deepEqual(
normalizePercentColumns(columns).map((column) => column.width),
['50.0000%', '50.0000%'],
)
})
test('百分比表格:选择列预留 4%', () => {
const columns: TableColumnsType<Record<string, unknown>> = [
{ title: 'A', dataIndex: 'a', width: 100 },
{ title: 'B', dataIndex: 'b', width: 100 },
]
const result = normalizePercentColumns(columns, { reservedPercent: 4 })
assert.deepEqual(
result.map((column) => column.width),
['48.0000%', '48.0000%'],
)
})
test('百分比表格:分组列宽等于子列之和', () => {
const columns: TableColumnsType<Record<string, unknown>> = [
{
title: '分组',
children: [
{ title: 'A', dataIndex: 'a', width: 100 },
{ title: 'B', dataIndex: 'b', width: 100 },
],
},
]
const result = normalizePercentColumns(columns)
assert.equal(result[0]?.width, '100.0000%')
})
test('全项目表格必须统一使用 PercentTable', () => {
const offenders: string[] = []
for (const file of listTsxFiles(path.join(process.cwd(), 'src'))) {
const source = fs.readFileSync(file, 'utf8')
if (/<Table\b/.test(source)) {
offenders.push(path.relative(process.cwd(), file))
}
}
assert.deepEqual(offenders, [], `仍有表格直接使用 AntD Table:${offenders.join(', ')}`)
})
function listTsxFiles(dir: string): string[] {
return fs.readdirSync(dir, { withFileTypes: true }).flatMap((entry) => {
const fullPath = path.join(dir, entry.name)
if (entry.isDirectory()) return listTsxFiles(fullPath)
return entry.isFile() && entry.name.endsWith('.tsx') ? [fullPath] : []
})
}