Files
dacli/tests/unit/field-builder-bs54.test.ts
T
sswandClaude a875437d77 feat(field-builder): BS-54 select/relationForm 默认 + 抽 field-defaults.ts
TDD 红绿重构:
- 红: tests/unit/field-builder-bs54.test.ts 6 FAIL
- 绿: FieldBuilder.build() select/relationForm 自动补默认字段
  - select: dataType='static', dictionaryType='', propsUrl='', options=[2项占位], props={label, value}
  - relationForm: modelId='' 占位 (用户必填)
- 重构: 抽通用占位 → src/schema/field-defaults.ts
  (避免应用特定信息入 dacli/src, jxcj 36 套模板/vmodel→enCode 仍留在 docs/jxcj/scripts/)

字段形状来源: web componentMap.ts:324-376 (select) + 1363-1406 (relationForm)

Co-Authored-By: Claude <[email protected]>
2026-06-24 14:36:23 +08:00

64 lines
2.7 KiB
TypeScript

/**
* tests/unit/field-builder-bs54.test.ts — BS-54 闭环 unit 测试 (v0.10+)
*
* 根因: jxcj v1.0 浏览器层 select 下拉框空白 (无 options) +
* relationForm 弹窗不显示 (无 modelId). FieldBuilder.build()
* 没补 dataType/options/modelId 默认值, 需用户手工写.
*
* 修复: FieldBuilder.build() 对 select/relationForm 字段自动补默认字段
* (dataType='static' + inline options; modelId='' 占位)
*
* 测试:
* 1. select 字段 build() 后 __config__.dataType 必须存在 (默认 'static')
* 2. select 字段 build() 后 options 数组必须有 ≥1 项 (默认 2 项占位)
* 3. select 字段 build() 后 props.label/value 映射必须存在 (默认 fullName/id)
* 4. relationForm 字段 build() 后 __config__.modelId key 必须存在 (空字符串占位)
* 5. 用户传 --modelId 时, 该值透传 (不被默认覆盖)
*/
import { describe, it, expect } from 'vitest'
import { FieldBuilder } from '../../src/schema/FieldBuilder.ts'
describe('BS-54 FieldBuilder.build() select/relationForm 默认字段', () => {
const fb = new FieldBuilder()
it('select 字段: __config__.dataType 默认 = "static"', () => {
const f = fb.build('select', { label: '状态下拉框' })
expect(f.__config__.dataType).toBe('static')
})
it('select 字段: 不传 options 时, 自动补 ≥1 项 inline options', () => {
const f = fb.build('select', { label: '类型' })
const opts = f.options as Array<{ id: string; fullName: string }> | undefined
expect(opts).toBeDefined()
expect(Array.isArray(opts)).toBe(true)
expect(opts!.length).toBeGreaterThan(0)
})
it('select 字段: 自动补 props.label="fullName", props.value="id"', () => {
const f = fb.build('select', { label: '类型' })
const props = f.props as { label: string; value: string } | undefined
expect(props).toBeDefined()
expect(props!.label).toBe('fullName')
expect(props!.value).toBe('id')
})
it('select 字段: 用户传的 options 优先, 不被默认覆盖', () => {
const custom = [{ fullName: 'A', id: 'a' }, { fullName: 'B', id: 'b' }, { fullName: 'C', id: 'c' }]
const f = fb.build('select', { label: '类型', options: custom })
expect(f.options).toEqual(custom)
})
it('relationForm 字段: __config__.modelId key 必须存在 (默认空字符串)', () => {
const f = fb.build('relationForm', { label: '关联大赛' })
expect('modelId' in f.__config__).toBe(true)
expect(f.__config__.modelId).toBe('')
})
it('relationForm 字段: 用户传 modelId 时, 透传', () => {
const f = fb.build('relationForm', {
label: '关联大赛',
__config__: { modelId: 'jxcj_competition_info' },
})
expect(f.__config__.modelId).toBe('jxcj_competition_info')
})
})