212 lines
9.3 KiB
TypeScript
212 lines
9.3 KiB
TypeScript
/**
|
||
* 「一家一格」并成「一格四家」这一步该长成什么样。
|
||
*
|
||
* 用例照生产上那张「出差备案申请」摆:四种项目分类各摆一行同构字段,全靠显示条件互相藏。
|
||
* 这里钉的是并格那一步的三条分寸——认得出同构、写出来的还是 `optionRoutes` /
|
||
* `fetch.sources[].when` 那几份本来就要存的东西、指着旧名字的带出一处都不落在地上。
|
||
* 设计见 `docs/option-source-routing-design.md` §12,后端那张真表单的逐家比对在
|
||
* `backend/scripts/collapse_trip_form.py`。
|
||
*/
|
||
import { describe, expect, it } from 'vitest'
|
||
import { applyMerge, needsOutsideValue, planMerge } from '@/utils/formMerge'
|
||
import type { FormFieldDef, LinkageCondition } from '@/types'
|
||
|
||
const when = (value: string): LinkageCondition => ({
|
||
match: 'all',
|
||
rules: [{ field: 'project_type', op: 'eq', value }],
|
||
})
|
||
|
||
const PROJECT_PMS: FormFieldDef = {
|
||
key: 'project_pms',
|
||
label: 'PMS项目名称',
|
||
type: 'select',
|
||
required: true,
|
||
hidden: true,
|
||
row: 4,
|
||
col: 7,
|
||
optionSource: 'dict',
|
||
dictCode: 'pms_project',
|
||
optionMap: { labelKey: 'label', valueKey: 'label' },
|
||
autoFill: [{ to: 'project_code_pms', pick: 'value' }],
|
||
visibleWhen: when('PMS'),
|
||
}
|
||
|
||
const PROJECT_SCC: FormFieldDef = {
|
||
key: 'project_scc',
|
||
label: 'VDI项目名称',
|
||
type: 'select',
|
||
required: true,
|
||
hidden: true,
|
||
row: 5,
|
||
col: 7,
|
||
optionSource: 'dict',
|
||
dictCode: 'SCC_Project',
|
||
optionMap: { labelKey: 'label', valueKey: 'label' },
|
||
picker: { dropdown: { searchKeys: ['label', 'value', 'extra.customerName'] } },
|
||
autoFill: [
|
||
{ to: 'project_code_SCC', pick: 'value' },
|
||
{ to: 'customer_scc', pick: 'extra.customerName' },
|
||
{ to: 'project_stage_scc', pick: 'extra.stageName' },
|
||
],
|
||
visibleWhen: when('VDI'),
|
||
}
|
||
|
||
const PROJECT_XZ: FormFieldDef = {
|
||
key: 'project_xz',
|
||
label: '行政项目名称',
|
||
type: 'text',
|
||
readonly: true,
|
||
hidden: true,
|
||
row: 6,
|
||
col: 7,
|
||
fetch: { source: 'xz_code', pick: 'project', picks: { code: 'project_code_xz' }, freezeAfterStart: true },
|
||
visibleWhen: when('XZZH'),
|
||
}
|
||
|
||
const PROJECT_DZ: FormFieldDef = {
|
||
key: 'project_dz',
|
||
label: '定制项目名称',
|
||
type: 'text',
|
||
readonly: true,
|
||
hidden: true,
|
||
row: 7,
|
||
col: 7,
|
||
fetch: { source: 'Code_dz', pick: 'project', picks: { code: 'project_code_dz' } },
|
||
visibleWhen: when('DZKFXM'),
|
||
}
|
||
|
||
const FOUR = [PROJECT_PMS, PROJECT_SCC, PROJECT_XZ, PROJECT_DZ]
|
||
|
||
const cell = (field: FormFieldDef) => ({ key: String(field.key), field })
|
||
|
||
describe('认得出「同构的那几格」', () => {
|
||
it('四格各挂一条「项目分类等于某一个值」= 认,并留内容最多的那一格', () => {
|
||
const plan = planMerge(FOUR)
|
||
expect(plan).not.toBeNull()
|
||
expect(plan?.driver).toBe('project_type')
|
||
// 两家有字典候选、两家只是取数写进来的只读文本:留 PMS 那一格的模样
|
||
expect(plan?.keep).toBe('project_pms')
|
||
expect(plan?.dropped).toEqual(['project_scc', 'project_xz', 'project_dz'])
|
||
expect(plan?.members.map((item) => item.value)).toEqual(['PMS', 'VDI', 'XZZH', 'DZKFXM'])
|
||
})
|
||
|
||
it('显示条件不是「单条等值」的不认:并错一家比不并还难查', () => {
|
||
expect(planMerge([PROJECT_PMS, { ...PROJECT_SCC, visibleWhen: { match: 'any', rules: [] } }])).toBeNull()
|
||
expect(
|
||
planMerge([PROJECT_PMS, { ...PROJECT_SCC, visibleWhen: { match: 'all', rules: [{ field: 'bu', op: 'eq', value: 'X' }] } }])
|
||
).toBeNull()
|
||
// 两家指的是同一个分类值 = 并完两条会打架
|
||
expect(planMerge([PROJECT_PMS, { ...PROJECT_SCC, visibleWhen: when('PMS') }])).toBeNull()
|
||
expect(planMerge([PROJECT_PMS])).toBeNull()
|
||
})
|
||
|
||
it('已经并过一次的不并;不是这几种形状的不并', () => {
|
||
expect(planMerge([{ ...PROJECT_PMS, optionRoutes: [{ source: 'dict', dictCode: 'x' }] }, PROJECT_SCC])).toBeNull()
|
||
const table: FormFieldDef = { key: 'items', label: '明细', type: 'table', row: 4, col: 1, visibleWhen: when('PMS') }
|
||
expect(planMerge([PROJECT_PMS, table])).toBeNull()
|
||
})
|
||
})
|
||
|
||
describe('并出来的那一格存的就是本来就要存的那几份', () => {
|
||
it('字典那几家进候选来源清单,取数那两家进外部取数条目,条件都从显示条件搬过来', () => {
|
||
const { rows, report } = applyMerge(FOUR.map(cell), planMerge(FOUR)!)
|
||
const field = rows.find((row) => row.key === 'project_pms')?.field as FormFieldDef
|
||
expect(report.routes).toBe(1)
|
||
expect(report.entries).toBe(2)
|
||
expect(field.optionRoutes).toEqual([
|
||
{
|
||
source: 'dict',
|
||
when: when('VDI'),
|
||
dictCode: 'SCC_Project',
|
||
optionMap: { labelKey: 'label', valueKey: 'label' },
|
||
},
|
||
])
|
||
// 留那一格自己那一份当兜底:PMS 那一刻清单里一条都不成立,用的就是它
|
||
expect(field.optionSource).toBe('dict')
|
||
expect(field.dictCode).toBe('pms_project')
|
||
expect((field.fetch?.sources ?? []).map((entry) => entry.source)).toEqual(['xz_code', 'Code_dz'])
|
||
expect((field.fetch?.sources ?? [])[0]?.when).toEqual(when('XZZH'))
|
||
expect(field.allowOutsideValue).toBe(true)
|
||
// 显示条件是这一格自己的事,并完由分类决定问哪一家,那一栏不再挂条件
|
||
expect(field.visibleWhen).toBeUndefined()
|
||
expect(field.hidden).toBeUndefined()
|
||
expect(rows.map((row) => row.key)).toEqual(['project_pms'])
|
||
})
|
||
|
||
it('带出只留一份,各家的附加值列合在一起;搜哪几列也合', () => {
|
||
const { rows } = applyMerge(FOUR.map(cell), planMerge(FOUR)!)
|
||
const field = rows[0].field
|
||
expect((field.autoFill ?? []).map((rule) => `${rule.pick}->${rule.to}`)).toEqual([
|
||
'value->project_code_pms',
|
||
'value->project_code_SCC',
|
||
'extra.customerName->customer_scc',
|
||
'extra.stageName->project_stage_scc',
|
||
])
|
||
expect(field.picker?.dropdown?.searchKeys).toEqual(['label', 'value', 'extra.customerName'])
|
||
})
|
||
|
||
it('别的格子里指着旧名字的带出跟着搬,一条不落', () => {
|
||
const reader: FormFieldDef = {
|
||
key: 'stage_note',
|
||
label: '阶段说明',
|
||
type: 'text',
|
||
row: 8,
|
||
col: 1,
|
||
fetch: { source: 'xz_code', args: { name: '{form.project_pms}' }, picks: { stage: 'project_scc' } },
|
||
}
|
||
const { rows, report } = applyMerge([...FOUR.map(cell), cell(reader)], planMerge(FOUR)!)
|
||
expect(report.moved).toBe(1)
|
||
const kept = rows.find((row) => row.key === 'stage_note')?.field as FormFieldDef
|
||
expect(kept.fetch?.picks?.stage).toBe('project_pms')
|
||
})
|
||
|
||
it('再并一次「项目编码」那四格:搬完落点不留两条一模一样的带出', () => {
|
||
const first = applyMerge(FOUR.map(cell), planMerge(FOUR)!)
|
||
const codes: FormFieldDef[] = [
|
||
{ key: 'project_code_pms', label: 'PMS项目编码', type: 'text', readonly: true, row: 4, col: 1, visibleWhen: when('PMS') },
|
||
{ key: 'project_code_SCC', label: 'VDI项目编码', type: 'text', readonly: true, row: 5, col: 1, visibleWhen: when('VDI') },
|
||
]
|
||
const plan = planMerge(codes)
|
||
expect(plan?.keep).toBe('project_code_pms')
|
||
const { rows } = applyMerge([...first.rows, ...codes.map(cell)], plan!)
|
||
const project = rows.find((row) => row.key === 'project_pms')?.field as FormFieldDef
|
||
expect((project.autoFill ?? []).map((rule) => `${rule.pick}->${rule.to}`)).toEqual([
|
||
'value->project_code_pms',
|
||
'extra.customerName->customer_scc',
|
||
'extra.stageName->project_stage_scc',
|
||
])
|
||
expect(rows.map((row) => row.key)).not.toContain('project_code_SCC')
|
||
})
|
||
|
||
it('两格都没有候选可搬(阶段那种纯文本):清单不留空数组,取数条目也不留', () => {
|
||
const stages: FormFieldDef[] = [
|
||
{ key: 'stage_pms', label: 'PMS项目阶段', type: 'text', required: true, row: 8, col: 7, visibleWhen: when('PMS') },
|
||
{ key: 'stage_xz', label: '行政项目阶段', type: 'text', row: 10, col: 7, visibleWhen: when('XZZH') },
|
||
]
|
||
const { rows, report } = applyMerge(stages.map(cell), planMerge(stages)!)
|
||
expect(report.routes).toBe(0)
|
||
expect(report.entries).toBe(0)
|
||
const field = rows[0].field
|
||
expect(field.optionRoutes).toBeUndefined()
|
||
expect(field.fetch).toBeUndefined()
|
||
expect(field.allowOutsideValue).toBeUndefined()
|
||
// 必填跟着留下来的那一格走,另一家从「不必填」变成「必填」
|
||
expect(field.required).toBe(true)
|
||
// 留下来的那一格必填,另一家从「不必填」被带成「必填」——这一句得交出去让人看见
|
||
expect(report.changed).toEqual([{ attr: 'required', values: ['XZZH'] }])
|
||
})
|
||
})
|
||
|
||
describe('被别人的带出写值的候选格要收下带回来的值', () => {
|
||
it('这一格是字典下拉、隔壁那一格要把附加值带进来 = 要勾', () => {
|
||
const stage: FormFieldDef = { key: 'project_stage', label: '项目阶段', type: 'select', optionSource: 'dict', dictCode: 'project_stage' }
|
||
const project: FormFieldDef = { key: 'project', label: '项目名称', type: 'select', autoFill: [{ to: 'project_stage', pick: 'extra.stageName' }] }
|
||
expect(needsOutsideValue(stage, [project, stage])).toBe(true)
|
||
expect(needsOutsideValue({ ...stage, allowOutsideValue: true }, [project, stage])).toBe(false)
|
||
// 没人往它身上写,就不必多勾这一条
|
||
expect(needsOutsideValue(stage, [stage])).toBe(false)
|
||
// 手填且没选项的格子不判越界,也不用勾
|
||
expect(needsOutsideValue({ key: 'project_stage', label: '项目阶段', type: 'text' }, [project])).toBe(false)
|
||
})
|
||
})
|