124 lines
3.6 KiB
TypeScript
124 lines
3.6 KiB
TypeScript
import { describe, it } from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import {
|
|
buildFlowPreviewRequestKey,
|
|
collectConditionFields,
|
|
shouldShowFlowPreviewSpinner,
|
|
shouldSkipFlowPreviewRequest
|
|
} from './oa-flow-preview-refresh'
|
|
|
|
describe('oa-flow-preview-refresh', () => {
|
|
it('ignores unrelated form fields when no condition fields', () => {
|
|
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)
|
|
})
|
|
|
|
it('only condition fields affect request 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)
|
|
})
|
|
|
|
it('initiator select changes request key', () => {
|
|
const keyA = buildFlowPreviewRequestKey({
|
|
formData: {},
|
|
initiatorSelectByNode: { n1: ['u1'] },
|
|
conditionFields: []
|
|
})
|
|
const keyB = buildFlowPreviewRequestKey({
|
|
formData: {},
|
|
initiatorSelectByNode: { n1: ['u2'] },
|
|
conditionFields: []
|
|
})
|
|
assert.notEqual(keyA, keyB)
|
|
})
|
|
|
|
it('collects condition fields from flow tree', () => {
|
|
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'])
|
|
})
|
|
|
|
it('collects formula fields and skips __initiator__', () => {
|
|
assert.deepEqual(
|
|
collectConditionFields({
|
|
type: 'condition',
|
|
conditionGroups: [{ when: '{days} > 3', child: { type: 'approver' } }]
|
|
}),
|
|
['days']
|
|
)
|
|
assert.deepEqual(
|
|
collectConditionFields({
|
|
type: 'condition',
|
|
conditionGroups: [
|
|
{
|
|
rules: [{ field: '__initiator__', op: 'eq', value: 'x' }],
|
|
child: { type: 'approver' }
|
|
}
|
|
]
|
|
}),
|
|
[]
|
|
)
|
|
})
|
|
|
|
it('shows spinner only before first completed preview', () => {
|
|
assert.equal(shouldShowFlowPreviewSpinner({ hasCompletedPreview: false }), true)
|
|
assert.equal(shouldShowFlowPreviewSpinner({ hasCompletedPreview: true }), false)
|
|
})
|
|
|
|
it('with in-flight key, allows rollback to previous successful 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)
|
|
})
|
|
})
|