92 lines
3.6 KiB
JavaScript
92 lines
3.6 KiB
JavaScript
/**
|
||
* 界面配置 JSON 公共工具测试(schemaJson)。
|
||
* 覆盖分组节点 id 的收集、取号与归一化:归一化必须是确定性纯函数(同一份 JSON 每次结果一致、
|
||
* 可重复执行),否则加载时的「归一化后与基线比较」会把补号误判成用户改动。
|
||
*/
|
||
import { describe, expect, it } from 'vitest'
|
||
import {
|
||
collectGroupIds,
|
||
nextGroupId,
|
||
normalizeGroupIds,
|
||
parseSchema,
|
||
schemaToJsonText,
|
||
} from '@/utils/schemaJson'
|
||
|
||
const group = (over = {}) => ({ type: 'group', title: '分组', children: [], ...over })
|
||
|
||
describe('collectGroupIds / nextGroupId', () => {
|
||
it('收集分组 id 并去重(view 取 columns,edit 取 children)', () => {
|
||
const schema = {
|
||
schemaVersion: 1,
|
||
columns: [group({ id: 'group1' }), group({ id: 'group1' }), group({ id: 'basic' })],
|
||
}
|
||
expect([...collectGroupIds('view', schema)].toSorted()).toEqual(['basic', 'group1'])
|
||
expect(collectGroupIds('edit', schema).size).toBe(0)
|
||
})
|
||
|
||
it('nextGroupId 取已用编号里最小的未用正整数', () => {
|
||
expect(nextGroupId([])).toBe('group1')
|
||
expect(nextGroupId(['group1', 'group3'])).toBe('group2')
|
||
expect(nextGroupId(['group1', 'group2'])).toBe('group3')
|
||
// 非 group{n} 的自定义 id 不占编号
|
||
expect(nextGroupId(['basic'])).toBe('group1')
|
||
})
|
||
})
|
||
|
||
describe('normalizeGroupIds', () => {
|
||
it('缺 id 的分组按先序补号,嵌套分组一并补齐', () => {
|
||
const schema = {
|
||
schemaVersion: 1,
|
||
children: [
|
||
{ field: 'a' },
|
||
group({ title: '外层', children: [group({ title: '内层' }), { field: 'b' }] }),
|
||
group({ title: '另一个' }),
|
||
],
|
||
}
|
||
const next = normalizeGroupIds('edit', schema)
|
||
expect(next.children[0]).toEqual({ field: 'a' })
|
||
expect(next.children[1].id).toBe('group1')
|
||
expect(next.children[1].children[0].id).toBe('group2')
|
||
expect(next.children[2].id).toBe('group3')
|
||
// 原对象不被修改
|
||
expect(schema.children[1].id).toBeUndefined()
|
||
})
|
||
|
||
it('已有 id 保留、不重排:删除产生的空洞不回收', () => {
|
||
const schema = {
|
||
schemaVersion: 1,
|
||
columns: [group({ id: 'group2' }), group({})],
|
||
}
|
||
const next = normalizeGroupIds('view', schema)
|
||
expect(next.columns.map((n) => n.id)).toEqual(['group2', 'group1'])
|
||
})
|
||
|
||
it('重复 id 的后来者重新发号,自定义 id 原样保留', () => {
|
||
const schema = {
|
||
schemaVersion: 1,
|
||
columns: [group({ id: 'group1' }), group({ id: 'group1' }), group({ id: 'basic' })],
|
||
}
|
||
const next = normalizeGroupIds('view', schema)
|
||
expect(next.columns.map((n) => n.id)).toEqual(['group1', 'group2', 'basic'])
|
||
})
|
||
|
||
it('确定性且幂等:重复归一化结果一致,无改动时返回原对象', () => {
|
||
const schema = { schemaVersion: 1, columns: [group({}), group({})] }
|
||
const once = normalizeGroupIds('view', schema)
|
||
const twice = normalizeGroupIds('view', once)
|
||
expect(schemaToJsonText('view', once)).toBe(schemaToJsonText('view', twice))
|
||
expect(twice).toBe(once)
|
||
})
|
||
|
||
it('无分组节点时返回原对象,query 配置不受影响', () => {
|
||
const view = { schemaVersion: 1, columns: [{ field: 'a' }] }
|
||
expect(normalizeGroupIds('view', view)).toBe(view)
|
||
const query = { schemaVersion: 1, quick: [], groups: [{ logic: 'and', conditions: [] }] }
|
||
expect(normalizeGroupIds('query', query)).toBe(query)
|
||
})
|
||
|
||
it('解析库中 JSON 后可归一化(缺 columns 的畸形数据不报错)', () => {
|
||
expect(() => normalizeGroupIds('view', parseSchema('view', '{bad json'))).not.toThrow()
|
||
})
|
||
})
|