50 lines
2.1 KiB
TypeScript
50 lines
2.1 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
import { applyNormalizedPatch, validateStrictSchema } from '../../src/commands/form/_schema_normalize.ts'
|
|
|
|
describe('form schema normalize (Task 1)', () => {
|
|
const existing = [{
|
|
__vModel__: 'f_x',
|
|
__config__: {
|
|
jnpfKey: 'relationForm', modelId: 'old-id', propsValue: 'f_x_name', relationField: 'f_x_ref',
|
|
label: 'X', required: false, dataType: 'static',
|
|
},
|
|
}]
|
|
|
|
it('顶层 dataType → __config__.dataType', () => {
|
|
const patch = [{ __vModel__: 'f_x', dataType: 'dictionary', dictionaryType: 'cat-1' }]
|
|
const merged = applyNormalizedPatch(existing, patch)
|
|
expect(merged[0].__config__.dataType).toBe('dictionary')
|
|
expect(merged[0].__config__.dictionaryType).toBe('cat-1')
|
|
expect(merged[0].__config__.jnpfKey).toBe('relationForm') // 保留
|
|
})
|
|
|
|
it('relationForm → select 时清除 relationForm 字段', () => {
|
|
const patch = [{ __vModel__: 'f_x', __config__: { jnpfKey: 'select', optionsList: [] } }]
|
|
const merged = applyNormalizedPatch(existing, patch)
|
|
expect(merged[0].__config__.jnpfKey).toBe('select')
|
|
expect(merged[0].__config__.modelId).toBeUndefined()
|
|
expect(merged[0].__config__.propsValue).toBeUndefined()
|
|
expect(merged[0].__config__.relationField).toBeUndefined()
|
|
})
|
|
|
|
it('required 顶层 → __config__.required', () => {
|
|
const patch = [{ __vModel__: 'f_x', required: true }]
|
|
const merged = applyNormalizedPatch(existing, patch)
|
|
expect(merged[0].__config__.required).toBe(true)
|
|
})
|
|
|
|
it('validateStrictSchema: __config__ 缺 jnpfKey 报错', () => {
|
|
const r = validateStrictSchema([{ __vModel__: 'f_x', __config__: { label: 'X' } }])
|
|
expect(r.ok).toBe(false)
|
|
expect(r.errors[0]).toContain('f_x')
|
|
expect(r.errors[0]).toContain('jnpfKey')
|
|
})
|
|
|
|
it('validateStrictSchema: required 与控件 capability 矛盾时报错 (如 divider 不能 required)', () => {
|
|
const r = validateStrictSchema([{ __vModel__: 'f_x', __config__: { jnpfKey: 'divider', required: true } }])
|
|
expect(r.ok).toBe(false)
|
|
expect(r.errors[0]).toContain('f_x')
|
|
expect(r.errors[0]).toContain('divider')
|
|
})
|
|
})
|