93 lines
3.0 KiB
TypeScript
93 lines
3.0 KiB
TypeScript
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.equal(result[0]?.align, 'left')
|
||
assert.equal(result[1]?.align, 'center')
|
||
assert.match(String(result[0]?.className), /percent-table-first-column/)
|
||
})
|
||
|
||
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('百分比表格:minWidth 作为比例权重并移除最小宽度约束', () => {
|
||
const columns: TableColumnsType<Record<string, unknown>> = [
|
||
{ title: 'A', dataIndex: 'a', minWidth: 100 },
|
||
{ title: 'B', dataIndex: 'b', minWidth: 300 },
|
||
]
|
||
const result = normalizePercentColumns(columns)
|
||
assert.deepEqual(
|
||
result.map((column) => column.width),
|
||
['25.0000%', '75.0000%'],
|
||
)
|
||
assert.ok(result.every((column) => !('minWidth' in column)))
|
||
})
|
||
|
||
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] : []
|
||
})
|
||
}
|