- 静态 8 种: input/inputNumber (含 decimal 修复)/textarea/datePicker/editor/switch/rate/slider/colorPicker - 静态 enum 3 种: select/radio/checkbox (含 dictionaryType 网络拉取接口) - 跳过 18 种: billRule/calculate/uploadFile/popupSelect/table 等 - 字典/数据接口/系统选择器接口预留 (Task 1.5+ 真 JNPF 数据测试) - 单测 34/34 PASS (含 v12 decimal 盲点 32 修复回归) Co-Authored-By: Claude <[email protected]>
197 lines
6.4 KiB
TypeScript
197 lines
6.4 KiB
TypeScript
// dacli/tests/unit/_browser_construct.test.ts
|
|
/**
|
|
* _browser_construct 单元测试
|
|
*
|
|
* 覆盖:
|
|
* - sampleValueForJnpfKey: 静态字段 8 种 (含 v12 decimal 修复)
|
|
* - buildSamples: select/radio/checkbox 静态 enum (跳过网络)
|
|
* - buildSamples: 13 种 skip 类型 (workSystem 实测)
|
|
*
|
|
* 网络拉取 (字典/数据接口/系统选择器) 留到 Task 1.5+ 集成测试
|
|
*/
|
|
import { describe, it, expect } from 'vitest'
|
|
import {
|
|
sampleValueForJnpfKey,
|
|
buildSamples,
|
|
MissingSampleError,
|
|
type FieldConfig,
|
|
} from '../../src/commands/form/_browser_construct'
|
|
|
|
describe('sampleValueForJnpfKey - 静态字段 (v0.7 + v12 decimal 修复)', () => {
|
|
it('input 文本字段 → string', () => {
|
|
expect(sampleValueForJnpfKey('f_name', 'input')).toMatch(/^test-/)
|
|
expect(sampleValueForJnpfKey('f_fullName', 'input')).toMatch(/^test-/)
|
|
})
|
|
|
|
it('inputNumber 整数 → number', () => {
|
|
expect(sampleValueForJnpfKey('f_age', 'inputNumber')).toBe(18)
|
|
expect(sampleValueForJnpfKey('f_count', 'inputNumber')).toBe(18)
|
|
})
|
|
|
|
it('inputNumber 金额字段 (decimal) → number 含小数 (v12 盲点 32 修复)', () => {
|
|
expect(sampleValueForJnpfKey('f_amount', 'inputNumber')).toBe(99.99)
|
|
expect(sampleValueForJnpfKey('f_money', 'inputNumber')).toBe(99.99)
|
|
expect(sampleValueForJnpfKey('f_price', 'inputNumber')).toBe(99.99)
|
|
expect(sampleValueForJnpfKey('f_total', 'inputNumber')).toBe(99.99)
|
|
expect(sampleValueForJnpfKey('f_sum', 'inputNumber')).toBe(99.99)
|
|
expect(sampleValueForJnpfKey('f_cost', 'inputNumber')).toBe(99.99)
|
|
expect(sampleValueForJnpfKey('f_fee', 'inputNumber')).toBe(99.99)
|
|
})
|
|
|
|
it('inputNumber 含 amount/money/price/total/sum/cost/fee 关键词才走 decimal, 其它走整数', () => {
|
|
expect(sampleValueForJnpfKey('f_qty', 'inputNumber')).toBe(18)
|
|
expect(sampleValueForJnpfKey('f_score', 'inputNumber')).toBe(18)
|
|
})
|
|
|
|
it('textarea 多行文本 → string', () => {
|
|
expect(sampleValueForJnpfKey('f_remark', 'textarea')).toMatch(/^test-/)
|
|
})
|
|
|
|
it('datePicker → YYYY-MM-DD', () => {
|
|
expect(sampleValueForJnpfKey('f_date', 'datePicker')).toMatch(/^\d{4}-\d{2}-\d{2}$/)
|
|
})
|
|
|
|
it('editor 富文本 → HTML', () => {
|
|
expect(sampleValueForJnpfKey('f_content', 'editor')).toMatch(/^<p>test-/)
|
|
})
|
|
|
|
it('switch/rate/slider/colorPicker 静态', () => {
|
|
expect(sampleValueForJnpfKey('f_on', 'switch')).toBe(true)
|
|
expect(sampleValueForJnpfKey('f_star', 'rate')).toBe(5)
|
|
expect(sampleValueForJnpfKey('f_pct', 'slider')).toBe(50)
|
|
expect(sampleValueForJnpfKey('f_color', 'colorPicker')).toBe('#1890ff')
|
|
})
|
|
})
|
|
|
|
describe('select/radio/checkbox 静态 enum (无网络)', () => {
|
|
it('select 有 options → 选第 1 项 value', async () => {
|
|
const cfg: FieldConfig = {
|
|
jnpfKey: 'select',
|
|
label: '车头型号',
|
|
required: false,
|
|
fieldKey: 'f_type',
|
|
options: [
|
|
{ value: '1', label: '奔驰' },
|
|
{ value: '2', label: '宝马' },
|
|
],
|
|
}
|
|
const samples = await buildSamples({ fields: [cfg] }, { skipNetwork: true })
|
|
expect(samples[0].value).toBe('1')
|
|
expect(samples[0].skip).toBe(false)
|
|
})
|
|
|
|
it('radio 有 options → 选第 1 项', async () => {
|
|
const cfg: FieldConfig = {
|
|
jnpfKey: 'radio',
|
|
label: '性别',
|
|
required: true,
|
|
fieldKey: 'f_gender',
|
|
options: [
|
|
{ value: '1', label: '男' },
|
|
{ value: '2', label: '女' },
|
|
],
|
|
}
|
|
const samples = await buildSamples({ fields: [cfg] }, { skipNetwork: true })
|
|
expect(samples[0].value).toBe('1')
|
|
})
|
|
|
|
it('checkbox 多选 → 选第 1 项数组', async () => {
|
|
const cfg: FieldConfig = {
|
|
jnpfKey: 'checkbox',
|
|
label: '增补原因',
|
|
required: false,
|
|
fieldKey: 'f_reason',
|
|
options: [
|
|
{ value: 'a', label: 'A' },
|
|
{ value: 'b', label: 'B' },
|
|
],
|
|
}
|
|
const samples = await buildSamples({ fields: [cfg] }, { skipNetwork: true })
|
|
expect(samples[0].value).toEqual(['a'])
|
|
})
|
|
|
|
it('select 无 options + 必填 → 必填错误', async () => {
|
|
const cfg: FieldConfig = {
|
|
jnpfKey: 'select',
|
|
label: '车头',
|
|
required: true,
|
|
fieldKey: 'f_type',
|
|
}
|
|
await expect(
|
|
buildSamples({ fields: [cfg] }, { skipNetwork: true })
|
|
).rejects.toThrow(MissingSampleError)
|
|
})
|
|
|
|
it('select 无 options + 非必填 → skip=true (不抛错)', async () => {
|
|
const cfg: FieldConfig = {
|
|
jnpfKey: 'select',
|
|
label: '可选车头',
|
|
required: false,
|
|
fieldKey: 'f_type',
|
|
}
|
|
const samples = await buildSamples({ fields: [cfg] }, { skipNetwork: true })
|
|
expect(samples[0].skip).toBe(true)
|
|
expect(samples[0].reason).toBe('select 无 options')
|
|
})
|
|
|
|
it('select 有 dictionaryType 但 skipNetwork=true → 非必填 skip, 必填抛错', async () => {
|
|
const cfg: FieldConfig = {
|
|
jnpfKey: 'select',
|
|
label: '状态',
|
|
required: true,
|
|
fieldKey: 'f_status',
|
|
dictionaryType: 'user_status',
|
|
}
|
|
await expect(
|
|
buildSamples({ fields: [cfg] }, { skipNetwork: true })
|
|
).rejects.toThrow(MissingSampleError)
|
|
})
|
|
})
|
|
|
|
describe('skip 类型 (workSystem 13 种)', () => {
|
|
it.each([
|
|
'billRule',
|
|
'createUser', 'createTime', 'modifyUser', 'modifyTime',
|
|
'calculate',
|
|
'uploadFile',
|
|
'qrcode',
|
|
'groupTitle', 'divider', 'row', 'carousel',
|
|
'popupSelect', 'popupTableSelect', 'popupAttr', 'autoComplete',
|
|
'tableGrid',
|
|
'table',
|
|
])('%s → skip=true', async (jnpfKey) => {
|
|
const samples = await buildSamples(
|
|
{ fields: [{ jnpfKey, label: 'X', required: false, fieldKey: 'f_x' }] },
|
|
{ skipNetwork: true }
|
|
)
|
|
expect(samples[0].skip).toBe(true)
|
|
expect(samples[0].reason).toBe(jnpfKey)
|
|
})
|
|
})
|
|
|
|
describe('skip 字段即使 required 也不抛错', () => {
|
|
it('table 必填 → skip=true 不抛 MissingSampleError', async () => {
|
|
const samples = await buildSamples(
|
|
{
|
|
fields: [
|
|
{ jnpfKey: 'table', label: '子表', required: true, fieldKey: 'f_sub' },
|
|
],
|
|
},
|
|
{ skipNetwork: true }
|
|
)
|
|
expect(samples[0].skip).toBe(true)
|
|
expect(samples[0].reason).toBe('table')
|
|
})
|
|
})
|
|
|
|
describe('MissingSampleError', () => {
|
|
it('含 fieldKey + reason 属性', () => {
|
|
const err = new MissingSampleError('f_name', 'no options')
|
|
expect(err.fieldKey).toBe('f_name')
|
|
expect(err.reason).toBe('no options')
|
|
expect(err.message).toContain('f_name')
|
|
expect(err.message).toContain('no options')
|
|
expect(err.name).toBe('MissingSampleError')
|
|
})
|
|
})
|