unis-oa/frontend/src/utils/formWriteback.spec.ts

69 lines
3.1 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.

/**
* 写回挂载的「要发的字段」勾选:读、写、校验三份口径的前端镜像测试。
*
* 后端对应在 ``mapping.mount_entry_fields`` / ``render_template(only_fields=...)`` /
* ``mapping.mount_problems``;这边钉住形状,那边才敢继续按老配置兼容读。
*/
import { describe, expect, it } from 'vitest'
import { mountEntryList, mountEntries, mountProblems, mountShape, templateFieldTargets } from '@/utils/formWriteback'
import type { IntegrationTemplate, WritebackMount } from '@/types'
function template(code: string, toList: string[]): IntegrationTemplate {
return {
code,
name: `模板${code}`,
source: 'demo_api',
triggerEvent: 'activity',
payload: { method: 'POST', fields: toList.map((to) => ({ to, from: '{form.x}' })) }
} as unknown as IntegrationTemplate
}
describe('写回挂载 · 要发的字段', () => {
it('对象写法带着字段进出;纯编码老写法一个字都不变', () => {
const block = {
trigger: 'leave',
integrations: ['erp_x', { code: 'fin_y', fields: [' a ', 'a', 'b', ''] }]
}
expect(mountEntries(block)).toEqual([
{ code: 'erp_x' },
{ code: 'fin_y', fields: ['a', 'b'] }
])
expect(mountEntryList(mountEntries(block))).toEqual(['erp_x', { code: 'fin_y', fields: ['a', 'b'] }])
})
it('和条件同格共存时两个键都留着;落库形状(单条平铺 / 多条 blocks)不吞字段', () => {
const flat = mountShape([{
trigger: 'arrive',
integrations: [{ code: 'fin_y', fields: ['a'], when: { match: 'all', rules: [{ field: 'payWay', op: 'eq', value: '对公' }] } }]
}])
expect(JSON.parse(JSON.stringify(flat))).toEqual({
trigger: 'arrive',
integrations: [{ code: 'fin_y', when: { match: 'all', rules: [{ field: 'payWay', op: 'eq', value: '对公' }] }, fields: ['a'] }]
})
const multi = mountShape([
{ trigger: 'arrive', integrations: [{ code: 'fin_y', fields: ['a'] }] },
{ trigger: 'leave', integrations: ['erp_x'] }
])
expect(JSON.parse(JSON.stringify(multi))).toEqual({
blocks: [
{ trigger: 'arrive', integrations: [{ code: 'fin_y', fields: ['a'] }] },
{ trigger: 'leave', integrations: ['erp_x'] }
]
})
})
it('模板可勾清单 = 映射行 to 按序去重;模板没映射行就是空清单', () => {
expect(templateFieldTargets(template('fin_y', ['b', 'a', 'b']))).toEqual(['b', 'a'])
expect(templateFieldTargets(undefined)).toEqual([])
expect(templateFieldTargets({ code: 'x' } as IntegrationTemplate)).toEqual([])
})
it('勾了模板里没有的发送字段就点名(并列出能勾的);勾的是真字段则一声不吭', () => {
const bad: WritebackMount = { trigger: 'arrive', integrations: [{ code: 'fin_y', fields: ['a', 'ghost'] }] }
const problems = mountProblems(bad, [template('fin_y', ['a', 'b'])])
expect(problems.some((item) => item.includes('ghost') && item.includes('a / b'))).toBe(true)
const ok: WritebackMount = { trigger: 'arrive', integrations: [{ code: 'fin_y', fields: ['b'] }] }
expect(mountProblems(ok, [template('fin_y', ['a', 'b'])])).toEqual([])
})
})