unis-oa/frontend/src/utils/formLinkage.pair.spec.ts

197 lines
10 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/**
* 条件右边挑的是表单里的另一格(``rules[].valueField``)。
*
* 「结束日期晚于开始日期」「报销金额超过带回来的预算余额」这一类比法,两边比的都是
* 填单的人才知道的值,写死一个数要么没人维护,要么第一天就过期。这一套要钉三件事:
*
* 1. **空值不当真值**:右边那一格还没填,这一条就不成立。两个都空着时
* ``compare('', 'eq', '')`` 是成立的,条件会在单据刚打开时就满足,格子莫名其妙露出来;
* 2. **前后端一个结果**:判法在 ``utils/formLinkage`` 与 ``service/dicts._rule_ok``
* 各写了一份,这份测试就是那一刀的镜像(对面用同样的数据跑一遍);
* 3. **比不了的比法压根不给**:拿附件比日期、拿明细比大小,拼得出条件但意思不成立。
*/
import { describe, expect, it } from 'vitest'
import {
conditionDependencies,
conditionOk,
dependsOn,
describeLinkageCondition,
isLinkagePairComparable,
isLinkageRuleReady,
linkagePairOps,
linkageProblems,
stateOf
} from '@/utils/formLinkage'
import type { FormFieldDef, LinkageCondition } from '@/types'
const FIELDS: FormFieldDef[] = [
{ key: 'start', label: '开始日期', type: 'date' },
{ key: 'end', label: '结束日期', type: 'date' },
{ key: 'amount', label: '报销金额', type: 'money' },
{ key: 'budget', label: '预算余额', type: 'number' },
{ key: 'note', label: '备注', type: 'text' },
{ key: 'kind', label: '报销类型', type: 'select' },
{ key: 'tags', label: '标签', type: 'checkbox' },
{ key: 'file', label: '附件', type: 'attachment' }
]
function condition(rules: LinkageCondition['rules'], match?: 'all' | 'any'): LinkageCondition {
return { match, rules }
}
describe('条件右边比另一格', () => {
it('数字比大小:读的是那一格此刻的值', () => {
const c = condition([{ field: 'amount', op: 'gt', valueField: 'budget' }])
expect(conditionOk(c, { amount: 1200, budget: 1000 })).toBe(true)
expect(conditionOk(c, { amount: 1200, budget: 1200 })).toBe(false)
// 改的是右边那一格,结果就得跟着翻面
expect(conditionOk(c, { amount: 1200, budget: 2000 })).toBe(false)
})
it('日期比先后,跟固定值那一档同一套比法', () => {
const c = condition([{ field: 'end', op: 'gt', valueField: 'start' }])
expect(conditionOk(c, { end: '2026-04-02', start: '2026-04-01' })).toBe(true)
expect(conditionOk(c, { end: '2026-04-02', start: '2026-04-02' })).toBe(false)
})
/**
* 比大小那一档以前只走 ``Number()``,日期串一律变 NaN,于是配了「晚于 / 早于」的条件
* **永远不成立**,界面上还看不出来。ISO 串按字面比就是按时间比,下面这几种写法
* 是日期 / 日期时间 / 时间 / 月份四类字段真存下来的样子(对面 ``dicts._order_compare``)。
*/
it('时间类的比大小按字面比,四种存法都算得出结果', () => {
const ok = (left: string, op: 'gt' | 'ge' | 'lt' | 'le', right: string) =>
conditionOk(condition([{ field: 'a', op, valueField: 'b' }]), { a: left, b: right })
expect(ok('2026-04-02T14:30', 'gt', '2026-04-02T09:00')).toBe(true)
expect(ok('2026-04-02T14:30', 'le', '2026-04-02T14:30')).toBe(true)
expect(ok('14:30:00', 'ge', '12:00:00')).toBe(true)
expect(ok('2026-03', 'lt', '2026-04')).toBe(true)
expect(ok('2026-13', 'lt', '2027-01')).toBe(true)
// 数还是按数比,别拿字面比把 9 判成大于 10
expect(ok('9', 'gt', '10')).toBe(false)
// 比不了的那种(一半是日期、一半是别的)不给结果,别蒙一个
expect(ok('2026-04-02', 'gt', '待定')).toBe(false)
// 固定值那一档同样修好了:这种条件配下去就曾经一直不成立
expect(conditionOk(condition([{ field: 'leaveDate', op: 'gt', value: '2026-01-01' }]), { leaveDate: '2026-06-30' })).toBe(true)
})
it('右边那一格没填:这一条不算成立,两边都空着也不算相等', () => {
const eq = condition([{ field: 'note', op: 'eq', valueField: 'kind' }])
expect(conditionOk(eq, {})).toBe(false)
expect(conditionOk(eq, { note: '', kind: '' })).toBe(false)
expect(conditionOk(eq, { kind: 'travel' })).toBe(false)
const gt = condition([{ field: 'amount', op: 'gt', valueField: 'budget' }])
expect(conditionOk(gt, { amount: 100 })).toBe(false)
// 填了 0 是填了,不是没填
expect(conditionOk(gt, { amount: 100, budget: 0 })).toBe(true)
})
it('判空类问的是左边那一格自己,右边空不空跟它没关系', () => {
const c = condition([{ field: 'note', op: 'notEmpty', valueField: 'kind' }])
expect(conditionOk(c, { note: '写明原因' })).toBe(true)
expect(conditionOk(c, {})).toBe(false)
})
it('属于 / 不属于:右边那一格是集合时问的是归属', () => {
const c = condition([{ field: 'kind', op: 'in', valueField: 'tags' }])
expect(conditionOk(c, { kind: 'travel', tags: ['travel', 'other'] })).toBe(true)
expect(conditionOk(c, { kind: 'office', tags: ['travel'] })).toBe(false)
})
it('没配 valueField 时照旧读写死的取值,老条件一个字节都不用改', () => {
expect(conditionOk(condition([{ field: 'amount', op: 'gt', value: 1000 }]), { amount: 1200 })).toBe(true)
// 空串那一种也是「没配」,不是「比那一格空值」
expect(conditionOk(condition([{ field: 'amount', op: 'gt', value: 1000, valueField: '' }]), { amount: 900 })).toBe(false)
})
it('且 / 或 混着配时,两边各算各的', () => {
const c = condition(
[
{ field: 'amount', op: 'gt', valueField: 'budget' },
{ field: 'note', op: 'notEmpty' }
],
'all'
)
expect(conditionOk(c, { amount: 2000, budget: 1000, note: '超预算说明' })).toBe(true)
expect(conditionOk(c, { amount: 2000, budget: 1000 })).toBe(false)
expect(conditionOk({ ...c, match: 'any' }, { amount: 500, budget: 1000, note: 'x' })).toBe(true)
})
it('引用了自己或已经不在表单上的那一格,都算配错', () => {
expect(conditionDependencies({ key: 'x', label: 'x', type: 'text', visibleWhen: condition([{ field: 'end', op: 'gt', valueField: 'start' }]) } as FormFieldDef)).toEqual(['end', 'start'])
const problems = linkageProblems([
{ key: 'x', label: '说明', type: 'text', visibleWhen: condition([{ field: 'amount', op: 'gt', valueField: 'amount' }]) },
...FIELDS
] as FormFieldDef[])
expect(problems.join('\n')).toContain('拿「报销金额」和自己比')
const gone = linkageProblems([
{ key: 'x', label: '说明', type: 'text', visibleWhen: condition([{ field: 'amount', op: 'gt', valueField: 'budget_old' }]) },
...FIELDS
] as FormFieldDef[])
expect(gone.join('\n')).toContain('引用了不存在的字段「budget_old」')
const mismatch = linkageProblems([
{ key: 'x', label: '说明', type: 'text', visibleWhen: condition([{ field: 'amount', op: 'gt', valueField: 'file' }]) },
...FIELDS
] as FormFieldDef[])
expect(mismatch.join('\n')).toContain('比不了')
})
})
describe('两个字段能怎么比', () => {
it('同一类:数字与日期比大小,选项只比相等', () => {
expect(linkagePairOps('money', 'number')).toEqual(['gt', 'ge', 'lt', 'le', 'eq', 'ne'])
expect(linkagePairOps('date', 'date')).toEqual(['gt', 'ge', 'lt', 'le', 'eq', 'ne'])
expect(linkagePairOps('select', 'picker')).toEqual(['eq', 'ne'])
expect(linkagePairOps('text', 'textarea')).toEqual(['eq', 'ne', 'contains'])
})
it('跨类只认相等 / 不等,比大小讲不通就不给', () => {
expect(linkagePairOps('money', 'text')).toEqual(['eq', 'ne'])
expect(linkagePairOps('money', 'date')).toEqual([])
expect(linkagePairOps('switch', 'text')).toEqual(['eq', 'ne'])
})
it('右边是集合时问归属;左边本来比不了大小就不给', () => {
expect(linkagePairOps('select', 'checkbox')).toEqual(['in', 'nin'])
expect(linkagePairOps('money', 'checkbox')).toEqual(['in', 'nin'])
// 明细 / 附件那种值不是「可比的一项」,谁都比不了
expect(linkagePairOps('money', 'attachment')).toEqual([])
expect(linkagePairOps('attachment', 'money')).toEqual([])
expect(linkagePairOps('table', 'table')).toEqual([])
expect(isLinkagePairComparable('money', 'attachment')).toBe(false)
})
it('显隐与必填跟着两边此刻的值一起翻面,右边那一格也算上游(后端 state_of 跑的是同一批数据)', () => {
const slot = {
key: 'overReason',
label: '超预算说明',
type: 'textarea',
visibleWhen: condition([{ field: 'amount', op: 'gt', valueField: 'budget' }]),
requiredWhen: condition([{ field: 'end', op: 'gt', valueField: 'start' }])
} as unknown as FormFieldDef
expect(stateOf(slot, {})).toEqual({ visible: false, required: false, readonly: false })
expect(stateOf(slot, { amount: 1200, budget: 1000 })).toMatchObject({ visible: true, required: false })
expect(stateOf(slot, { amount: 800, budget: 1000, end: '2026-04-05', start: '2026-04-01' })).toMatchObject({
visible: false,
required: true
})
// 四个名字都得是上游:改 budget 要重算显隐,改 start 要重算必填
expect(dependsOn(slot)).toEqual(['amount', 'budget', 'end', 'start'])
})
it('比不了的那一对不该被当成填全了,人话摘要念的是那一格的名字', () => {
expect(isLinkageRuleReady({ field: 'amount', op: 'gt', valueField: 'budget' })).toBe(true)
expect(isLinkageRuleReady({ field: 'amount', op: 'gt', valueField: '' })).toBe(false)
const text = describeLinkageCondition(
condition([{ field: 'end', op: 'gt', valueField: 'start' }]),
{
label: (key) => FIELDS.find((item) => item.key === key)?.label || key,
type: (key) => FIELDS.find((item) => item.key === key)?.type ?? 'text',
optionLabel: () => ''
},
(op) => ({ gt: '晚于', eq: '等于' }[op] ?? op)
)
expect(text).toBe('结束日期 晚于 开始日期')
})
})