Files
workspace/code/fms/fms-vue/tests/unit/schema-render.spec.js
T
2026-09-11 17:31:34 +08:00

158 lines
5.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 界面配置 JSON → 运行时渲染配置 适配层测试(fms-module-common/schemaRender)。
* 覆盖 view→列表列、edit→分组/编辑行、query→查询条件,以及栏位设置回写 JSON。
*/
import { describe, expect, it } from 'vitest'
import {
buildListConfig,
buildEditRender,
buildQueryRender,
orderQueryConditions,
applyColumnSettings,
} from '@/components/fms-module-common/schemaRender'
import { parseOptions } from '@/components/fms-module-common/fieldUtils'
describe('buildListConfig(view JSON → 列表列)', () => {
it('分组/嵌套分组按文档顺序展开为扁平列,保留宽度与可见性', () => {
const view = {
schemaVersion: 1,
columns: [
{ field: 'name', width: 120 },
{
type: 'group',
title: '基本信息',
children: [
{ field: 'age', width: 100 },
{ type: 'group', title: '深层', children: [{ field: 'remark', width: 200 }] },
],
},
{ field: 'hidden', visible: false },
],
}
const rows = buildListConfig(view)
expect(rows.map((r) => r.b_field)).toEqual(['name', 'age', 'remark', 'hidden'])
expect(rows.map((r) => r.b_xh)).toEqual([10, 20, 30, 40])
expect(rows[0]).toMatchObject({ b_field: 'name', b_width: 120, b_visible: 1 })
expect(rows[3].b_visible).toBe(0)
})
it('无配置时返回空数组', () => {
expect(buildListConfig({ schemaVersion: 1, columns: [] })).toEqual([])
expect(buildListConfig(null)).toEqual([])
})
})
describe('buildEditRender(edit JSON → 分组 + 编辑行)', () => {
it('还原分组与行属性,嵌套分组生成独立分组', () => {
const edit = {
schemaVersion: 1,
children: [
{ field: 'code', span: 24, required: true },
{
type: 'group',
title: '基础',
children: [
{ field: 'name', span: 12, readonly: true, defaultValue: 'X' },
{ type: 'group', title: '扩展', children: [{ field: 'remark', span: 24 }] },
],
},
],
}
const { groups, rows } = buildEditRender(edit)
expect(rows.map((r) => r.b_field)).toEqual(['code', 'name', 'remark'])
const outer = groups.find((g) => g.b_title === '基础')
const inner = groups.find((g) => g.b_title === '扩展')
expect(outer).toBeTruthy()
expect(inner.b_parent_id).toBe(outer.b_id)
expect(rows[0]).toMatchObject({ b_group_id: null, b_required: 1, b_colspan: 24 })
expect(rows[1]).toMatchObject({
b_group_id: outer.b_id,
b_colspan: 12,
b_readonly: 1,
b_default_value: 'X',
})
expect(rows[2].b_group_id).toBe(inner.b_id)
})
})
describe('buildQueryRender / orderQueryConditions(query JSON → 查询条件)', () => {
const query = {
schemaVersion: 1,
quick: ['b_status'],
groups: [
{
logic: 'and',
conditions: [
{ field: 'b_no', operator: 'like' },
{ field: 'b_status', operator: 'eq', component: 'select', defaultValue: '1' },
],
},
],
}
it('展开条件并透出 quick', () => {
const { quick, conditions } = buildQueryRender(query)
expect(quick).toEqual(['b_status'])
expect(conditions.map((c) => c.field)).toEqual(['b_no', 'b_status'])
expect(conditions[1]).toMatchObject({ operator: 'eq', component: 'select', defaultValue: '1' })
})
it('quick 字段优先排序', () => {
const { quick, conditions } = buildQueryRender(query)
const ordered = orderQueryConditions(conditions, quick)
expect(ordered.map((c) => c.field)).toEqual(['b_status', 'b_no'])
})
})
describe('applyColumnSettings(栏位设置回写 JSON)', () => {
it('同层按设置顺序重排并更新宽度,保留分组结构', () => {
const view = {
schemaVersion: 1,
columns: [
{ field: 'a', width: 100 },
{ type: 'group', title: 'G', children: [{ field: 'b', width: 110 }, { field: 'c', width: 120 }] },
{ field: 'd', width: 130 },
],
}
const settings = [
{ b_field: 'd', b_width: 300 },
{ b_field: 'b', b_width: 200 },
{ b_field: 'a', b_width: 150 },
{ b_field: 'c', b_width: 120 },
]
const next = applyColumnSettings('view', view, settings)
expect(next.columns[0].field).toBe('d')
expect(next.columns[0].width).toBe(300)
expect(next.columns[1].type).toBe('group')
expect(next.columns[1].children.map((n) => n.field)).toEqual(['b', 'c'])
expect(next.columns[1].children[0].width).toBe(200)
expect(next.columns[2].field).toBe('a')
expect(next.columns[2].width).toBe(150)
})
})
describe('parseOptions(选项来源)', () => {
it('读取界面配置节点的 options 数组', () => {
const { options } = parseOptions({ options: [{ value: '1', label: '是' }, { value: '0', label: '否' }] })
expect(options.map((o) => o.value)).toEqual(['1', '0'])
})
it('读取 { options: [] } 形态并解析 optionConfig', () => {
const { options, optionConfig } = parseOptions({
options: { precision: 3, options: [{ value: 1, label: '一' }] },
})
expect(optionConfig.precision).toBe(3)
expect(options[0]).toMatchObject({ value: 1, label: '一' })
})
it('兼容旧字段行 b_options JSON 字符串', () => {
const { options } = parseOptions({ b_options: JSON.stringify(['A', 'B']) })
expect(options.map((o) => o.value)).toEqual(['A', 'B'])
})
it('无选项来源时返回空', () => {
expect(parseOptions({ b_type: 'input' })).toEqual({ optionConfig: {}, options: [] })
expect(parseOptions(null)).toEqual({ optionConfig: {}, options: [] })
})
})