141 lines
3.6 KiB
JavaScript
141 lines
3.6 KiB
JavaScript
import assert from 'node:assert/strict'
|
|
import {
|
|
collectConditionFields,
|
|
buildFlowPreviewRequestKey,
|
|
shouldSkipFlowPreviewRequest,
|
|
shouldShowFlowPreviewSpinner
|
|
} from './oa-flow-preview-refresh.js'
|
|
|
|
// 无条件分支:表单无关字段变化不应改变 request key
|
|
{
|
|
const keyA = buildFlowPreviewRequestKey({
|
|
formData: { title: 'a', reason: 'x' },
|
|
initiatorSelectByNode: {},
|
|
conditionFields: []
|
|
})
|
|
const keyB = buildFlowPreviewRequestKey({
|
|
formData: { title: 'b', reason: 'y' },
|
|
initiatorSelectByNode: {},
|
|
conditionFields: []
|
|
})
|
|
assert.equal(keyA, keyB)
|
|
assert.equal(shouldSkipFlowPreviewRequest(keyA, keyB), true)
|
|
}
|
|
|
|
// 有条件字段:仅条件字段变化才改变 key
|
|
{
|
|
const fields = ['amount']
|
|
const keyA = buildFlowPreviewRequestKey({
|
|
formData: { amount: 100, remark: 'old' },
|
|
initiatorSelectByNode: {},
|
|
conditionFields: fields
|
|
})
|
|
const keySame = buildFlowPreviewRequestKey({
|
|
formData: { amount: 100, remark: 'new' },
|
|
initiatorSelectByNode: {},
|
|
conditionFields: fields
|
|
})
|
|
const keyDiff = buildFlowPreviewRequestKey({
|
|
formData: { amount: 200, remark: 'new' },
|
|
initiatorSelectByNode: {},
|
|
conditionFields: fields
|
|
})
|
|
assert.equal(keyA, keySame)
|
|
assert.notEqual(keyA, keyDiff)
|
|
assert.equal(shouldSkipFlowPreviewRequest(keyA, keySame), true)
|
|
assert.equal(shouldSkipFlowPreviewRequest(keyA, keyDiff), false)
|
|
}
|
|
|
|
// 自选审批人变化应触发预览
|
|
{
|
|
const keyA = buildFlowPreviewRequestKey({
|
|
formData: {},
|
|
initiatorSelectByNode: { n1: ['u1'] },
|
|
conditionFields: []
|
|
})
|
|
const keyB = buildFlowPreviewRequestKey({
|
|
formData: {},
|
|
initiatorSelectByNode: { n1: ['u2'] },
|
|
conditionFields: []
|
|
})
|
|
assert.notEqual(keyA, keyB)
|
|
}
|
|
|
|
// 从 flowTree 收集条件字段(对齐后端语义)
|
|
{
|
|
const tree = {
|
|
type: 'start',
|
|
child: {
|
|
type: 'condition',
|
|
conditionGroups: [
|
|
{
|
|
rules: [{ field: 'leaveType', op: 'eq', value: '事假' }],
|
|
child: { type: 'approver', id: 'a1' }
|
|
},
|
|
{
|
|
isDefault: true,
|
|
child: { type: 'approver', id: 'a2' }
|
|
}
|
|
],
|
|
child: { type: 'end' }
|
|
}
|
|
}
|
|
assert.deepEqual(collectConditionFields(tree), ['leaveType'])
|
|
}
|
|
|
|
// 公式字段 {fieldName}
|
|
{
|
|
const tree = {
|
|
type: 'condition',
|
|
conditionGroups: [
|
|
{
|
|
when: '{days} > 3',
|
|
child: { type: 'approver' }
|
|
}
|
|
]
|
|
}
|
|
assert.deepEqual(collectConditionFields(tree), ['days'])
|
|
}
|
|
|
|
// 系统伪字段 __initiator__ 不纳入
|
|
{
|
|
const tree = {
|
|
type: 'condition',
|
|
conditionGroups: [
|
|
{
|
|
rules: [{ field: '__initiator__', op: 'eq', value: 'x' }],
|
|
child: { type: 'approver' }
|
|
}
|
|
]
|
|
}
|
|
assert.deepEqual(collectConditionFields(tree), [])
|
|
}
|
|
|
|
// 仅首次预览展示整区 loading
|
|
assert.equal(shouldShowFlowPreviewSpinner({ hasCompletedPreview: false }), true)
|
|
assert.equal(shouldShowFlowPreviewSpinner({ hasCompletedPreview: true }), false)
|
|
|
|
// 有在途请求时:相同 key 跳过,不同 key(含回滚到上次成功值)必须再请求
|
|
{
|
|
const key100 = buildFlowPreviewRequestKey({
|
|
formData: { amount: 100 },
|
|
initiatorSelectByNode: {},
|
|
conditionFields: ['amount']
|
|
})
|
|
const key200 = buildFlowPreviewRequestKey({
|
|
formData: { amount: 200 },
|
|
initiatorSelectByNode: {},
|
|
conditionFields: ['amount']
|
|
})
|
|
assert.equal(
|
|
shouldSkipFlowPreviewRequest(key100, key200, { inFlightKey: key200 }),
|
|
true
|
|
)
|
|
assert.equal(
|
|
shouldSkipFlowPreviewRequest(key100, key100, { inFlightKey: key200 }),
|
|
false
|
|
)
|
|
}
|
|
|
|
console.log('oa-flow-preview-refresh.test.js ok')
|