214 lines
10 KiB
TypeScript
214 lines
10 KiB
TypeScript
// @vitest-environment jsdom
|
||
/**
|
||
* 挑完关联之后,填单页上那一趟即时预览到底问了什么、写回了什么。
|
||
*
|
||
* 对面是 `backend/app/service/linkfill.py`(提交时服务端按同一份配置重算并覆盖),
|
||
* `utils/linkFill.spec.ts` 钉的是那一份算法,这一份钉的是**接上填单页之后**那三件:
|
||
*
|
||
* 1. **一次问一趟**:这一屏上所有 `pick` 并成一个请求发出去(路径解析只在后端有一份),
|
||
* 回体里的值写进主表那一格、并按「一单一行」补出明细的行;
|
||
* 2. **只盯关联那串值**:别的格子改一下不重问,多挂一张才再问一趟——否则每敲一个字
|
||
* 都要去问一遍那几张单;
|
||
* 3. **设计态与只读态不发**:那两屏没有人正在挑关联,问了也没人看。
|
||
*/
|
||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||
import { flushPromises, mount } from '@vue/test-utils'
|
||
import { createPinia, setActivePinia } from 'pinia'
|
||
import { createI18n } from 'vue-i18n'
|
||
import zh from '@/i18n/locales/zh'
|
||
import FormRenderer from './FormRenderer.vue'
|
||
import { workAPI } from '@/api'
|
||
import { useAuthStore } from '@/stores/auth'
|
||
import type { FormFieldDef, FormSchema, User } from '@/types'
|
||
|
||
vi.mock('@/api', () => ({
|
||
orgAPI: { simpleUnits: vi.fn(async () => []) },
|
||
dictAPI: { dictValues: vi.fn(async () => ({})) },
|
||
supportAPI: { deleteAttachment: vi.fn(async () => ({})) },
|
||
workAPI: {
|
||
relationValues: vi.fn(async () => ({ items: [] })),
|
||
listRelatedWorks: vi.fn(async () => ({ items: [], total: 0 })),
|
||
workSummaries: vi.fn(async () => [])
|
||
}
|
||
}))
|
||
|
||
const i18n = createI18n({ legacy: false, locale: 'zh', messages: { zh }, missingWarn: false, fallbackWarn: false })
|
||
|
||
const RELATION: FormFieldDef = {
|
||
key: 'orders',
|
||
label: '关联报销单',
|
||
type: 'relation',
|
||
span: 6,
|
||
optionSource: 'process',
|
||
linkFill: {
|
||
rules: [
|
||
{ pick: 'work.serial', to: 'src_serial', agg: 'join' },
|
||
{ pick: 'data.amount', to: 'items.amount' }
|
||
]
|
||
}
|
||
}
|
||
|
||
function schema(fields: FormFieldDef[]): FormSchema {
|
||
return { fields, layout: { columns: 12, grid: 12 } } as unknown as FormSchema
|
||
}
|
||
|
||
const FIELDS: FormFieldDef[] = [
|
||
RELATION,
|
||
{ key: 'src_serial', label: '来源单号', type: 'text', span: 6 },
|
||
{ key: 'remark', label: '备注', type: 'text', span: 6 },
|
||
{
|
||
key: 'items',
|
||
label: '明细',
|
||
type: 'table',
|
||
span: 12,
|
||
columns: [{ key: 'amount', label: '金额', type: 'money' }]
|
||
}
|
||
]
|
||
|
||
const ITEMS = [
|
||
{ job: 'J1', values: { 'work.serial': 'BX-001', 'data.amount': 100 } },
|
||
{ job: 'J2', values: { 'work.serial': 'BX-002', 'data.amount': 2300.5 } }
|
||
]
|
||
|
||
async function mountForm(props: Record<string, unknown> = {}, fields: FormFieldDef[] = FIELDS) {
|
||
const pinia = createPinia()
|
||
setActivePinia(pinia)
|
||
useAuthStore().$patch({
|
||
user: { id: 'u1', account: 'zhangsan', display_name: '张三', organization_name: '研发中心' } as unknown as User,
|
||
token: 'token'
|
||
} as never)
|
||
const wrapper = mount(FormRenderer, {
|
||
props: { schema: schema(fields), modelValue: {}, ...props },
|
||
global: { plugins: [pinia, i18n] }
|
||
})
|
||
await flushPromises()
|
||
// 那一趟预览防抖 300ms 才发
|
||
await new Promise((resolve) => setTimeout(resolve, 450))
|
||
await flushPromises()
|
||
return wrapper
|
||
}
|
||
|
||
function relationValues() {
|
||
return vi.mocked(workAPI.relationValues)
|
||
}
|
||
|
||
function lastEmit(wrapper: Awaited<ReturnType<typeof mountForm>>) {
|
||
return wrapper.emitted('update:modelValue')?.at(-1)?.[0] as Record<string, unknown>
|
||
}
|
||
|
||
beforeEach(() => {
|
||
relationValues().mockReset()
|
||
// 照着真后端的脾气来:只回这一串值里查得到的那几张,顺序也按关联顺序
|
||
relationValues().mockImplementation(async (values: string[]) =>
|
||
({ items: ITEMS.filter((item) => values.includes(item.job)) }))
|
||
})
|
||
|
||
/** 照着填单页上那只父组件的样子把上一次写回的值接住,再改一两格传回去。 */
|
||
function threaded(wrapper: Awaited<ReturnType<typeof mountForm>>, change: Record<string, unknown>) {
|
||
return { ...(lastEmit(wrapper) ?? {}), ...change }
|
||
}
|
||
|
||
describe('挑完关联那一趟预览', () => {
|
||
it('这一屏上所有 pick 并成一趟发出去,回的值写进主表那一格并补出明细的行', async () => {
|
||
const wrapper = await mountForm({ modelValue: { orders: ['J1', 'J2'] } })
|
||
expect(relationValues()).toHaveBeenCalledTimes(1)
|
||
// 第三个参数是正在填的那一张单:后端拿它多认一档"确实挂在这一单上"的可见性
|
||
expect(relationValues()).toHaveBeenCalledWith(['J1', 'J2'], ['work.serial', 'data.amount'], '')
|
||
const written = lastEmit(wrapper)
|
||
expect(written.src_serial).toBe('BX-001、BX-002')
|
||
expect((written.items as Record<string, unknown>[]).map((row) => row._link)).toEqual(['J1', 'J2'])
|
||
expect((written.items as Record<string, unknown>[]).map((row) => row.amount)).toEqual([100, 2300.5])
|
||
// 留痕跟着写:提交时后端拿它对「关联变了没有」,界面与引擎认的是同一本账
|
||
expect((written._linkfill as Record<string, unknown>).orders).toEqual(['J1', 'J2'])
|
||
})
|
||
|
||
it('那一格存的是单号:回来的账按问出去的那一句认领,明细表照样补行', async () => {
|
||
// 费用报销单上「请选择出差备案流程」就是这一档:optionMap.valueKey = serial
|
||
const fields = FIELDS.map((field) =>
|
||
field.key === 'orders' ? { ...field, optionMap: { labelKey: 'title', valueKey: 'serial' } } : field
|
||
)
|
||
relationValues().mockImplementation(async (values: string[]) => ({
|
||
// 真后端的脾气:按单号也认得那张单,回来的 job 是 job、value 才是问出去的单号
|
||
items: ITEMS.map((item) => ({ ...item, value: `BX-00${item.job.slice(1)}` })).filter((item) =>
|
||
values.includes(item.value)
|
||
)
|
||
}))
|
||
const wrapper = await mountForm({ modelValue: { orders: ['BX-001', 'BX-002'] } }, fields)
|
||
expect(relationValues()).toHaveBeenCalledTimes(1)
|
||
expect(relationValues().mock.calls.at(-1)?.[0]).toEqual(['BX-001', 'BX-002'])
|
||
const written = lastEmit(wrapper)
|
||
expect(written.src_serial).toBe('BX-001、BX-002')
|
||
// 行上的来源标记仍是那张单的 job:提交时后端按同一分寸对账
|
||
expect((written.items as Record<string, unknown>[]).map((row) => row._link)).toEqual(['J1', 'J2'])
|
||
expect((written.items as Record<string, unknown>[]).map((row) => row.amount)).toEqual([100, 2300.5])
|
||
})
|
||
|
||
it('只盯关联那一串值:改别的格子不重问,多挂一张才再问一趟', async () => {
|
||
const wrapper = await mountForm({ modelValue: { orders: ['J1'] } })
|
||
expect(relationValues()).toHaveBeenCalledTimes(1)
|
||
await wrapper.setProps({ modelValue: threaded(wrapper, { remark: '自己写的备注' }) })
|
||
await new Promise((resolve) => setTimeout(resolve, 450))
|
||
await flushPromises()
|
||
expect(relationValues()).toHaveBeenCalledTimes(1)
|
||
await wrapper.setProps({ modelValue: threaded(wrapper, { orders: ['J1', 'J2'] }) })
|
||
await new Promise((resolve) => setTimeout(resolve, 450))
|
||
await flushPromises()
|
||
expect(relationValues()).toHaveBeenCalledTimes(2)
|
||
expect(relationValues().mock.calls.at(-1)?.[0]).toEqual(['J1', 'J2'])
|
||
})
|
||
|
||
it('取消全部关联时不再去问,界面上那几行与那几个数跟着撤', async () => {
|
||
const wrapper = await mountForm({ modelValue: { orders: ['J1'] } })
|
||
expect(relationValues()).toHaveBeenCalledTimes(1)
|
||
// 人自己加的那一行不是带出生来的,撤带出不能顺手把它一起删掉
|
||
await wrapper.setProps({
|
||
modelValue: threaded(wrapper, { orders: [], items: [{ amount: 999 }, ...(lastEmit(wrapper).items as object[])] })
|
||
})
|
||
await new Promise((resolve) => setTimeout(resolve, 450))
|
||
await flushPromises()
|
||
expect(relationValues()).toHaveBeenCalledTimes(1)
|
||
const written = lastEmit(wrapper)
|
||
expect(written.src_serial).toBe('')
|
||
expect((written.items as Record<string, unknown>[]).map((row) => row._link)).toEqual([undefined])
|
||
// 留痕也跟着改成空串那一串,提交时后端认的是同一本账
|
||
expect((written._linkfill as Record<string, unknown>).orders).toEqual([])
|
||
})
|
||
|
||
it('没挂任何单时一趟都不问', async () => {
|
||
await mountForm({ modelValue: { remark: '还没挑关联' } })
|
||
expect(relationValues()).not.toHaveBeenCalled()
|
||
})
|
||
|
||
it('两格关联字段并成一趟问,回来的账各归各的', async () => {
|
||
relationValues().mockImplementation(async (values: string[]) => ({
|
||
items: [{ job: 'J1', values: { 'work.serial': 'BX-001', 'data.amount': 100 } },
|
||
{ job: 'J9', values: { 'work.serial': 'CG-009', 'data.amount': 500 } }].filter((item) =>
|
||
values.includes(item.job))
|
||
}))
|
||
const fields: FormFieldDef[] = [
|
||
RELATION,
|
||
{ key: 'buys', label: '关联采购单', type: 'relation', span: 6, optionSource: 'process',
|
||
linkFill: { rules: [{ pick: 'work.serial', to: 'buy_serial' }] } },
|
||
{ key: 'src_serial', label: '来源单号', type: 'text', span: 6 },
|
||
{ key: 'buy_serial', label: '采购单号', type: 'text', span: 6 },
|
||
{ key: 'items', label: '明细', type: 'table', span: 12,
|
||
columns: [{ key: 'amount', label: '金额', type: 'money' }] }
|
||
]
|
||
const wrapper = await mountForm({ schema: schema(fields), modelValue: { orders: ['J1'], buys: ['J9'] } })
|
||
expect(relationValues()).toHaveBeenCalledTimes(1)
|
||
expect(relationValues().mock.calls.at(-1)?.[0]).toEqual(['J1', 'J9'])
|
||
// 两条 pick 并成一份清单,重复的去掉
|
||
expect(relationValues().mock.calls.at(-1)?.[1]).toEqual(['work.serial', 'data.amount'])
|
||
const written = lastEmit(wrapper)
|
||
expect(written.src_serial).toBe('BX-001')
|
||
expect(written.buy_serial).toBe('CG-009')
|
||
expect((written.items as Record<string, unknown>[]).map((row) => row._link)).toEqual(['J1'])
|
||
})
|
||
|
||
it('设计态与只读态不发这一趟:那两屏没人正在挑关联', async () => {
|
||
await mountForm({ modelValue: { orders: ['J1'] }, design: true })
|
||
await mountForm({ modelValue: { orders: ['J1'] }, readonly: true })
|
||
expect(relationValues()).not.toHaveBeenCalled()
|
||
})
|
||
})
|