Files
dacli/tests/unit/field-builder.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

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')
})
})