Files
dacli/tests/unit/form_preview.test.ts
sushub f403a7e80e import: ssw/dacli@eea0ebc (2026-08-12) 起点版本
本仓 2026-08-12 从 ssw main (192.168.1.18:13300/ssw/ssw-mvp) 的
dacli/ subpath 抽取,作为独立仓托管到 gitea:3000/ssw/dacli.git。

- 工作树为 ssw main dacli subpath 最新版 (110 commit, 08-12)
- 历史未带 ssw main 整体历史(避免大仓污染)
- 此为新仓基线 commit,未来以本仓历史为主
2026-08-12 15:58:06 +08:00

77 lines
3.1 KiB
TypeScript

/**
* tests/unit/form_preview.test.ts — P0-6 --preview 渲染 + 展平单元测试
*/
import { describe, it, expect } from 'vitest'
import {
strWidth,
collectPreviewRows,
renderFormPreview,
confirmApply,
} from '../../src/commands/form/_preview.ts'
const sampleFormData = {
fields: [
{ __vModel__: 'f_leave_type', options: [], __config__: { jnpfKey: 'select', label: '请假类型', required: true, dataType: 'dictionary', dictionaryType: '850666694069192837' } },
{ __vModel__: 'f_start_date', format: 'yyyy-MM-dd', __config__: { jnpfKey: 'datePicker', label: '开始日期', required: false } },
{ __vModel__: 'f_days', min: 1, max: 365, __config__: { jnpfKey: 'inputNumber', label: '请假天数', required: false } },
{ __vModel__: 'f_reason', __config__: { jnpfKey: 'textarea', label: '请假事由', required: false } },
// 布局容器: 不产生行, 但递归子字段
{ __config__: { jnpfKey: 'row', label: '', children: [
{ __vModel__: 'f_agent', __config__: { jnpfKey: 'input', label: '代理人', required: false } },
] } },
],
}
describe('P0-6 _preview: strWidth (CJK 宽度)', () => {
it('ASCII 1 宽, CJK 2 宽', () => {
expect(strWidth('abc')).toBe(3)
expect(strWidth('请假')).toBe(4)
expect(strWidth('a请b')).toBe(4)
})
})
describe('P0-6 _preview: collectPreviewRows', () => {
it('展平字段, 跳过布局容器但递归其子字段', () => {
const rows = collectPreviewRows(sampleFormData)
const names = rows.map(r => r.name)
expect(names).toContain('f_leave_type')
expect(names).toContain('f_agent') // 来自 row 容器内
expect(names).not.toContain('') // row 容器本身不入行
expect(rows.length).toBe(5)
})
it('提取 label/component/required', () => {
const rows = collectPreviewRows(sampleFormData)
const leave = rows.find(r => r.name === 'f_leave_type')!
expect(leave.label).toBe('请假类型')
expect(leave.component).toBe('select')
expect(leave.required).toBe(true)
})
it('describeExtra: 字典 / 数值范围 / 日期格式', () => {
const rows = collectPreviewRows(sampleFormData)
expect(rows.find(r => r.name === 'f_leave_type')!.extra).toContain('字典')
expect(rows.find(r => r.name === 'f_days')!.extra).toContain('范围')
expect(rows.find(r => r.name === 'f_start_date')!.extra).toContain('yyyy-MM-dd')
})
})
describe('P0-6 _preview: renderFormPreview', () => {
it('渲染盒线表格, 含表头与字段名', () => {
const table = renderFormPreview(sampleFormData, { fullName: '请假申请', enCode: 'leaveForm', webType: '2', autoRelease: true })
expect(table).toContain('leaveForm')
expect(table).toContain('字段标签')
expect(table).toContain('f_leave_type')
expect(table).toContain('┌') // 盒线
expect(table).toContain('└')
expect(table).toContain('字段数=5')
})
})
describe('P0-6 _preview: confirmApply', () => {
it('非 TTY 环境返回 false (dry-run 安全默认)', async () => {
// vitest 环境 process.stdin.isTTY 为 undefined/false
expect(await confirmApply()).toBe(false)
})
})