Files
dacli/tests/unit/field-builder.test.ts
T
ssw c98ad4f425 init: TS 版 dacli v0.7 baseline (8 父命令/26+ 子命令, 0.4MB, .claude/skills/dacli-ts)
参考 boot/ 仓库结构初始化, 同步到 gitea:3000/ssw/dacli (private).

包括:
- src/  8 父命令 (form/menu/dm/dict/di/apis/doc/workflow + login/status) + core (client/crypto/help/log/types/config)
- src/schema/ FieldBuilder (从 JNPF web 源码提取, 避免 53 个手写 builder)
- tests/ vitest 单元 + playwright E2E (含 workSystem/developSystem baseline regression)
- scripts/ extract-all.mjs (从 jnpf-web 源码生成 schema) + seed-tds-baseline.ts
- doc/  architecture/plan/api/extractors/baseline/RESUME/help-system + v0.6/v0.7 changelog
- .claude/skills/dacli-ts/  Claude 集成 skill (SKILL.md + 5 references + 7 examples + 3 baseline)
- CLAUDE.md (项目说明) + README.md + prompt.md (开发任务说明书)
- 配置文件: package.json/lock, tsconfig, vitest.config, playwright.config
2026-06-12 18:26:39 +08:00

159 lines
5.6 KiB
TypeScript

/**
* tests/unit/field-builder.test.ts — FieldBuilder 单元测试
*
* 验证:
* 1. 60+ jnpfKey 都能从 component-map.json 找到
* 2. 39 控件在 field-schemas.json 都有 activeData 字段
* 3. 14 条 L1 正则都能加载
* 4. build('input', ...) 产生完整 JnpfField
* 5. build 未知 jnpfKey 抛错
* 6. regList 引用 L1 正则
* 7. layout 控件的默认 layout=rowFormItem
* 8. switch/radio 等默认值正确
*/
import { describe, it, expect } from 'vitest'
import { FieldBuilder, defaultFieldBuilder, loadComponentMap, loadFieldSchemas, loadValidationRules } from '../../src/schema/FieldBuilder.ts'
describe('JSON schema 加载', () => {
it('component-map.json 有 60+ jnpfKey', () => {
const m = loadComponentMap()
expect(Object.keys(m).length).toBeGreaterThanOrEqual(60)
expect(m.input).toBe('JnpfInput')
expect(m.select).toBe('JnpfSelect')
expect(m.relationForm).toBe('JnpfRelationForm')
})
it('field-schemas.json 有 39 控件字段', () => {
const s = loadFieldSchemas()
expect(Object.keys(s).length).toBeGreaterThanOrEqual(39)
expect(s.input).toContain('maxlength')
expect(s.input).toContain('clearable')
expect(s.switch).toContain('activeTxt')
expect(s.inputNumber).toContain('precision')
})
it('validation-rules.json 有 14 条 L1 正则', () => {
const v = loadValidationRules()
expect(Object.keys(v).length).toBeGreaterThanOrEqual(14)
expect(v.chinese.msg).toBe('请正确输入中文')
expect(v.email.rule).toMatch(/@/) // email regex contains @ literal
expect(v.iphone.rule).toMatch(/1\[3-9\]/)
})
})
describe('FieldBuilder.build()', () => {
const fb = new FieldBuilder()
it('input 控件: 完整 __config__ + 默认属性', () => {
const f = fb.build('input', { label: '姓名' })
expect(f.__config__.jnpfKey).toBe('input')
expect(f.__config__.tag).toBe('JnpfInput')
expect(f.__config__.label).toBe('姓名')
expect(f.__config__.layout).toBe('colFormItem')
expect(f.__config__.span).toBe(24)
expect(f.__config__.required).toBe(false)
expect(f.__config__.regList).toEqual([])
expect(f.__config__.trigger).toBe('blur')
expect(f.style).toEqual({ width: '100%' })
})
it('switch 控件: defaultValue=0, trigger=change', () => {
const f = fb.build('switch', { label: '启用' })
expect(f.__config__.jnpfKey).toBe('switch')
expect(f.__config__.tag).toBe('JnpfSwitch')
expect(f.__config__.trigger).toBe('change')
expect(f.defaultValue).toBe(0)
})
it('checkbox 控件: defaultValue=[]', () => {
const f = fb.build('checkbox', { label: '兴趣' })
expect(f.defaultValue).toEqual([])
})
it('用户提供的 __config__.required 覆盖默认 false', () => {
const f = fb.build('input', { label: 'X', __config__: { required: true } })
expect(f.__config__.required).toBe(true)
})
it('用户提供的 regList 走自定义', () => {
const f = fb.build('input', {
__config__: { regList: [{ pattern: 'chinese', message: '请输入中文' }] },
})
expect(f.__config__.regList).toEqual([{ pattern: 'chinese', message: '请输入中文' }])
})
it('layout 控件 (groupTitle): layout=rowFormItem, showLabel=false', () => {
const f = fb.build('groupTitle', { label: '基本信息' })
expect(f.__config__.layout).toBe('rowFormItem')
expect(f.__config__.showLabel).toBe(false)
expect(f.__config__.regList).toBeUndefined()
})
it('table 控件 (子表): layout=rowFormItem', () => {
const f = fb.build('table', { label: '子表' })
expect(f.__config__.layout).toBe('rowFormItem')
expect(f.__config__.tag).toBe('JnpfInputTable')
})
it('未知 jnpfKey 抛错', () => {
expect(() => fb.build('notExist', {})).toThrow(/Unknown jnpfKey/)
})
it('额外属性透传', () => {
const f = fb.build('input', { placeholder: '请输入姓名', maxlength: 50, addonBefore: '¥' })
expect(f.placeholder).toBe('请输入姓名')
expect(f.maxlength).toBe(50)
expect(f.addonBefore).toBe('¥')
})
})
describe('FieldBuilder.regList()', () => {
const fb = new FieldBuilder()
it('L1 正则 chinese', () => {
const r = fb.regList('chinese')
expect(r.pattern).toBe('chinese')
expect(r.message).toBe('请正确输入中文')
})
it('L1 正则 email 自定义 message', () => {
const r = fb.regList('email', '邮箱格式不对')
expect(r.message).toBe('邮箱格式不对')
})
it('L1 正则 chinese 默认 message', () => {
const r = fb.regList('chinese')
expect(r.pattern).toBe('chinese')
expect(r.message).toBe('请正确输入中文')
})
it('未知正则抛错', () => {
expect(() => fb.regList('notARule')).toThrow(/Unknown L1 rule/)
})
})
describe('defaultFieldBuilder() 单例', () => {
it('多次调用返回同一实例', () => {
const a = defaultFieldBuilder()
const b = defaultFieldBuilder()
expect(a).toBe(b)
})
})
describe('FieldBuilder 业务场景: 多字段表单', () => {
it('请假单 (姓名/类型/日期/说明)', () => {
const fb = new FieldBuilder()
const fields = [
fb.build('input', { label: '姓名' }),
fb.build('select', { label: '类型', options: [{ id: '1', fullName: '事假' }, { id: '2', fullName: '病假' }] }),
fb.build('datePicker', { label: '开始日期' }),
fb.build('textarea', { label: '说明' }),
]
expect(fields).toHaveLength(4)
expect(fields[0].__config__.tag).toBe('JnpfInput')
expect(fields[1].__config__.tag).toBe('JnpfSelect')
expect(fields[2].__config__.tag).toBe('JnpfDatePicker')
expect(fields[3].__config__.tag).toBe('JnpfTextarea')
})
})