Files
workspace/code/fms/fms-vue/tests/unit/data-diff.spec.js
T
2026-09-03 22:10:55 +08:00

87 lines
3.1 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.
/**
* dataChanges.diffRow(单条记录字段级差异比对)测试:
* 语义对齐旧系统 diffData 的单表分支 —— 无基线视为新建(整行 insert),
* 修改时 updates 只携带「变化字段 + 主键」,内置 null/''/undefined 等价、
* 数字型等价与 _/v_/ignoreFields 过滤;主键默认 'b_id',可自定义。
*/
import { describe, expect, it } from 'vitest'
import { diffRow } from '@/utils/dataChanges'
describe('diffRow 单条记录', () => {
it('无基线时视为新建,返回整行 insert', () => {
const changes = diffRow({
current: { b_id: '1', b_code: 'demo', b_name: '演示模块' },
original: null,
})
expect(changes.hasChanges).toBe(true)
expect(changes.inserts).toEqual([{ b_id: '1', b_code: 'demo', b_name: '演示模块' }])
expect(changes.updates).toHaveLength(0)
})
it('与基线完全一致时 hasChanges 为 false(不产生仅主键的空 update)', () => {
const original = { b_id: '1', b_code: 'demo', b_name: '演示模块' }
const changes = diffRow({ current: { ...original }, original })
expect(changes.hasChanges).toBe(false)
expect(changes.updates).toHaveLength(0)
})
it('修改只提交变化字段 + 主键', () => {
const original = { b_id: '1', b_code: 'demo', b_name: '演示模块', b_xh: 10 }
const changes = diffRow({
current: { b_id: '1', b_code: 'demo', b_name: '改名后', b_xh: 10 },
original,
})
expect(changes.updates).toEqual([{ b_id: '1', b_name: '改名后' }])
})
it('null/undefined/空串 与数字型(40 vs "40.00")归一后不误报变更', () => {
const original = { b_id: '1', b_name: '演示', b_code: null, b_xh: 40 }
const changes = diffRow({
current: { b_id: '1', b_name: '演示', b_code: '', b_xh: '40.00' },
original,
})
expect(changes.hasChanges).toBe(false)
})
it('跳过 _ 前缀与 v_ 前缀字段,ignoreFields 不参与比较', () => {
const original = {
b_id: '1',
b_name: '演示',
_treeKey: 'x',
v_customer_name: 'A',
b_currentperiod: '2026-01',
}
const changes = diffRow({
current: {
b_id: '1',
b_name: '演示',
_treeKey: 'y', // 内部字段变化不影响
v_customer_name: 'B', // 视图字段变化不影响
b_currentperiod: '2026-02', // ignoreFields 内,不影响
},
original,
ignoreFields: ['b_currentperiod'],
})
expect(changes.hasChanges).toBe(false)
})
it('insert 行剔除 _/v_/ignoreFields 前缀字段', () => {
const changes = diffRow({
current: { b_id: '1', b_name: '新', _ui: true, v_view: 'x', b_xh: 10 },
original: null,
ignoreFields: ['b_xh'],
})
expect(changes.inserts).toEqual([{ b_id: '1', b_name: '新' }])
})
it('支持非默认主键(primaryKey)', () => {
const original = { gid: '7', b_name: '旧' }
const changes = diffRow({
current: { gid: '7', b_name: '新' },
original,
primaryKey: 'gid',
})
expect(changes.updates).toEqual([{ gid: '7', b_name: '新' }])
})
})