190 lines
7.6 KiB
JavaScript
190 lines
7.6 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)
|
||
// 分组 id 即 JSON 节点 id(原数据缺 id 时按先序补齐)
|
||
expect(outer.b_id).toBe('group1')
|
||
expect(inner.b_id).toBe('group2')
|
||
|
||
const back = editorToSchema('view', rows, groups, schema)
|
||
const outerNode = back.columns.find((n) => n.type === 'group')
|
||
expect(outerNode.title).toBe('基本信息')
|
||
expect(outerNode.id).toBe('group1')
|
||
expect(outerNode.children[1].id).toBe('group2')
|
||
expect(outerNode.children.map((n) => n.field ?? n.title)).toEqual(['age', '扩展'])
|
||
// 列宽等已知属性保留
|
||
expect(back.columns.find((n) => n.field === 'name').width).toBe(120)
|
||
})
|
||
|
||
it('edit:行节点 ↔ b_line 草稿互转,span/required 保留、默认值不额外写出', () => {
|
||
const schema = {
|
||
schemaVersion: 2,
|
||
children: [
|
||
{
|
||
type: 'row',
|
||
children: [
|
||
{ field: 'name', span: 12, required: true },
|
||
{ field: 'age', span: 12 },
|
||
],
|
||
},
|
||
{ type: 'row', children: [{ field: 'remark' }] },
|
||
],
|
||
}
|
||
const { rows } = schemaToEditor('edit', schema, 'm1')
|
||
expect(rows.map((r) => r.b_field)).toEqual(['name', 'age', 'remark'])
|
||
expect(rows[0].b_colspan).toBe(12)
|
||
expect(rows[0].b_required).toBe(1)
|
||
expect(rows[2].b_colspan).toBe(24)
|
||
// 同一行共用一个行 id,不同行必须不同(行是硬边界)
|
||
expect(rows[0].b_line).toBe(rows[1].b_line)
|
||
expect(rows[2].b_line).not.toBe(rows[0].b_line)
|
||
|
||
const back = editorToSchema('edit', rows, [], schema)
|
||
expect(back.schemaVersion).toBe(2)
|
||
expect(back.children[0].type).toBe('row')
|
||
expect(back.children[0].children[0]).toMatchObject({ field: 'name', span: 12, required: true })
|
||
// span=24 且原节点未显式给出 → 不写出 span
|
||
expect(back.children[1].children[0].span).toBeUndefined()
|
||
})
|
||
|
||
it('edit:老结构(平铺 + newline)加载时折成临时行,不写回 newline', () => {
|
||
const schema = {
|
||
schemaVersion: 1,
|
||
children: [
|
||
{ field: 'a', span: 8 },
|
||
{ field: 'b', span: 8 },
|
||
{ field: 'c', span: 8, newline: true },
|
||
{ field: 'd', span: 24 },
|
||
],
|
||
}
|
||
const { rows } = schemaToEditor('edit', schema, 'm1')
|
||
// a b 同行(8+8=16);c 显式断行;d 放不下再折行
|
||
expect(rows[0].b_line).toBe(rows[1].b_line)
|
||
expect(rows[2].b_line).not.toBe(rows[1].b_line)
|
||
expect(rows[3].b_line).not.toBe(rows[2].b_line)
|
||
|
||
const back = editorToSchema('edit', rows, [], schema)
|
||
expect(back.children.map((n) => n.type)).toEqual(['row', 'row', 'row'])
|
||
expect(back.children[0].children.map((n) => n.field)).toEqual(['a', 'b'])
|
||
expect(back.children[1].children.map((n) => n.field)).toEqual(['c'])
|
||
expect(JSON.stringify(back)).not.toContain('newline')
|
||
})
|
||
|
||
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' })
|
||
})
|
||
|
||
it('view:合计(summary)与行属性互转,无合计不写出该属性', () => {
|
||
const schema = {
|
||
schemaVersion: 1,
|
||
columns: [{ field: 'amount', width: 110, summary: 'sum' }, { field: 'name' }],
|
||
}
|
||
const { rows, groups } = schemaToEditor('view', schema, 'm1')
|
||
expect(rows[0].b_summary).toBe('sum')
|
||
expect(rows[1].b_summary).toBe('')
|
||
|
||
const back = editorToSchema('view', rows, groups, schema)
|
||
expect(back.columns[0].summary).toBe('sum')
|
||
expect(back.columns[1].summary).toBeUndefined()
|
||
|
||
// 改成计数 → 写回 count;清空 → 删除属性
|
||
rows[0].b_summary = 'count'
|
||
const counted = editorToSchema('view', rows, groups, schema)
|
||
expect(counted.columns[0].summary).toBe('count')
|
||
rows[0].b_summary = ''
|
||
const cleared = editorToSchema('view', rows, groups, schema)
|
||
expect(cleared.columns[0].summary).toBeUndefined()
|
||
})
|
||
})
|
||
|
||
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: 2,
|
||
children: [{ type: 'row', children: [{ field: 'name', span: 24 }] }],
|
||
}
|
||
expect(hasSchemaField('edit', schema, 'name', { requireVisible: true })).toBe(true)
|
||
schema = setSchemaFieldVisible('edit', schema, 'name', false)
|
||
expect(schema.children[0].type).toBe('row')
|
||
expect(schema.children[0].children[0].visible).toBe(false)
|
||
expect(hasSchemaField('edit', schema, 'name')).toBe(true)
|
||
expect(hasSchemaField('edit', schema, 'name', { requireVisible: true })).toBe(false)
|
||
// 删除字段后空行节点一并消失
|
||
expect(removeSchemaField('edit', schema, 'name').children).toEqual([])
|
||
})
|
||
|
||
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)
|
||
})
|
||
})
|