122 lines
4.8 KiB
JavaScript
122 lines
4.8 KiB
JavaScript
/**
|
||
* 界面配置 JSON 节点模型适配层测试(schemaModel)。
|
||
* 覆盖 view / edit 配置对象与编辑器行/分组草稿的互转(含分组嵌套与未知属性保留),
|
||
* 以及字段参与(增删/可见性)的节点操作。
|
||
*/
|
||
import { describe, expect, it } from 'vitest'
|
||
import {
|
||
schemaToEditor,
|
||
editorToSchema,
|
||
addSchemaField,
|
||
removeSchemaField,
|
||
hasSchemaField,
|
||
setSchemaFieldVisible,
|
||
hasQueryCondition,
|
||
addQueryCondition,
|
||
removeQueryCondition,
|
||
defaultQueryCondition,
|
||
} from '@/views/module/module-management/components/design-editor/common/schemaModel'
|
||
|
||
describe('schemaModel:view/edit 配置 ↔ 编辑器草稿', () => {
|
||
it('view:分组嵌套解析为 b_group_id / b_parent_id,回写保留分组结构', () => {
|
||
const schema = {
|
||
schemaVersion: 1,
|
||
columns: [
|
||
{ field: 'name', width: 120 },
|
||
{
|
||
type: 'group',
|
||
title: '基本信息',
|
||
children: [
|
||
{ field: 'age', width: 100 },
|
||
{
|
||
type: 'group',
|
||
title: '扩展',
|
||
children: [{ field: 'remark', width: 200 }],
|
||
},
|
||
],
|
||
},
|
||
],
|
||
}
|
||
const { rows, groups } = schemaToEditor('view', schema, 'm1')
|
||
expect(rows.map((r) => r.b_field)).toEqual(['name', 'age', 'remark'])
|
||
// name 未分组;age 属顶级分组;remark 属子分组
|
||
expect(rows[0].b_group_id).toBeNull()
|
||
const outer = groups.find((g) => g.b_title === '基本信息')
|
||
const inner = groups.find((g) => g.b_title === '扩展')
|
||
expect(rows[1].b_group_id).toBe(outer.b_id)
|
||
expect(rows[2].b_group_id).toBe(inner.b_id)
|
||
expect(inner.b_parent_id).toBe(outer.b_id)
|
||
|
||
const back = editorToSchema('view', rows, groups, schema)
|
||
const outerNode = back.columns.find((n) => n.type === 'group')
|
||
expect(outerNode.title).toBe('基本信息')
|
||
expect(outerNode.children.map((n) => n.field ?? n.title)).toEqual(['age', '扩展'])
|
||
// 列宽等已知属性保留
|
||
expect(back.columns.find((n) => n.field === 'name').width).toBe(120)
|
||
})
|
||
|
||
it('edit:span/required/readonly 与行属性互转,默认值不额外写出', () => {
|
||
const schema = {
|
||
schemaVersion: 1,
|
||
children: [
|
||
{ field: 'name', span: 12, required: true },
|
||
{ field: 'age' },
|
||
],
|
||
}
|
||
const { rows } = schemaToEditor('edit', schema, 'm1')
|
||
expect(rows[0].b_colspan).toBe(12)
|
||
expect(rows[0].b_required).toBe(1)
|
||
expect(rows[1].b_colspan).toBe(24)
|
||
|
||
const back = editorToSchema('edit', rows, [], schema)
|
||
expect(back.children[0]).toMatchObject({ field: 'name', span: 12, required: true })
|
||
// span=24 且原节点未显式给出 → 不写出 span
|
||
expect(back.children[1].span).toBeUndefined()
|
||
})
|
||
|
||
it('未知节点扩展属性在往返中保留', () => {
|
||
const schema = {
|
||
schemaVersion: 1,
|
||
columns: [{ field: 'name', width: 120, fixed: 'left', format: 'money' }],
|
||
}
|
||
const { rows, groups } = schemaToEditor('view', schema, 'm1')
|
||
const back = editorToSchema('view', rows, groups, schema)
|
||
expect(back.columns[0]).toMatchObject({ field: 'name', fixed: 'left', format: 'money' })
|
||
})
|
||
})
|
||
|
||
describe('schemaModel:字段参与节点操作', () => {
|
||
it('view:追加 / 移除字段节点', () => {
|
||
let schema = { schemaVersion: 1, columns: [] }
|
||
expect(hasSchemaField('view', schema, 'name')).toBe(false)
|
||
schema = addSchemaField('view', schema, { b_field: 'name', b_type: 'input' })
|
||
expect(hasSchemaField('view', schema, 'name')).toBe(true)
|
||
expect(schema.columns[0].width).toBe(120)
|
||
schema = removeSchemaField('view', schema, 'name')
|
||
expect(hasSchemaField('view', schema, 'name')).toBe(false)
|
||
})
|
||
|
||
it('edit:切换 visible 保留节点', () => {
|
||
let schema = { schemaVersion: 1, children: [{ field: 'name', span: 24 }] }
|
||
expect(hasSchemaField('edit', schema, 'name', { requireVisible: true })).toBe(true)
|
||
schema = setSchemaFieldVisible('edit', schema, 'name', false)
|
||
expect(schema.children[0].visible).toBe(false)
|
||
expect(hasSchemaField('edit', schema, 'name')).toBe(true)
|
||
expect(hasSchemaField('edit', schema, 'name', { requireVisible: true })).toBe(false)
|
||
})
|
||
|
||
it('query:条件增删与默认组件', () => {
|
||
let schema = { schemaVersion: 1, quick: [], groups: [] }
|
||
expect(hasQueryCondition(schema, 'amount')).toBe(false)
|
||
expect(defaultQueryCondition({ b_field: 'amount', b_type: 'money' })).toMatchObject({
|
||
component: 'number',
|
||
operator: 'eq',
|
||
})
|
||
schema = addQueryCondition(schema, { b_field: 'amount', b_type: 'money' })
|
||
expect(hasQueryCondition(schema, 'amount')).toBe(true)
|
||
expect(schema.groups[0].conditions[0].field).toBe('amount')
|
||
schema = removeQueryCondition(schema, 'amount')
|
||
expect(hasQueryCondition(schema, 'amount')).toBe(false)
|
||
})
|
||
})
|