unis-oa/frontend/src/components/oa/FormRenderer.readback.spec.ts

99 lines
5.2 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.

// @vitest-environment jsdom
/**
* 办结之后回看那一屏(只读渲染)摆哪几格。
*
* 生产上 `出差备案申请` 那一单:八格项目 / 客户 / 阶段全勾了「默认隐藏」、又全按「项目分类」
* 配了「显示条件」。办理那一屏跟着条件露得好好的,办结之后再打开就是一句"字段没了"——
* 回看渲染那句 `props.readonly ? null : model.value` 承认自己算不了条件,可那份值就躺在
* `modelValue` 里。这一份钉的是修完之后那一屏的四种下场,与
* `utils/formRuntime.hidden.spec.ts`(判据本体)、`mobileLayout.spec.ts`(手机上那一屏)
* 对上同一句话:回看屏只往宽放,凭存过的值把「默认隐藏 + 显示条件」那一组摆回来,
* 不拿条件去收已经存了值的格子。
*/
import { 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 { 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 () => ({})) }
}))
vi.mock('@/api/dataSources', () => ({ fetchValue: vi.fn(async () => { throw new Error('不该取数') }) }))
vi.mock('@/api/quota', () => ({ quotaOptions: vi.fn(async () => []) }))
const i18n = createI18n({ legacy: false, locale: 'zh', messages: { zh }, missingWarn: false, fallbackWarn: false })
/** 按「项目分类」放行:这就是那八格的配法 */
const whenType = (value: string) => ({ rules: [{ field: 'project_type', op: 'eq', value }] })
const FIELDS: FormFieldDef[] = [
{ key: 'reason', label: '出差事由', type: 'textarea', row: 1, col: 1, span: 12 },
{ key: 'project_type', label: '项目分类', type: 'select', row: 2, col: 1, span: 6 },
{ key: 'project_pms', label: 'PMS项目名称', type: 'select', hidden: true, visibleWhen: whenType('PMS'), row: 3, col: 1, span: 6 },
{ key: 'project_scc', label: 'VDI项目名称', type: 'select', hidden: true, visibleWhen: whenType('VDI'), row: 3, col: 7, span: 6 },
{ key: 'area_cf', label: '出发地', type: 'text', row: 4, col: 1, span: 6 },
{ key: 'project_xz', label: '行政项目名称', type: 'text', hidden: true, row: 4, col: 7, span: 6 }
]
function schema(): FormSchema {
return { fields: FIELDS, layout: { columns: 12, grid: 12 } }
}
async function open(data: Record<string, unknown>, props: Record<string, unknown> = {}) {
const pinia = createPinia()
setActivePinia(pinia)
useAuthStore().$patch({
user: { id: 'u1', account: 'zhangsan', display_name: '张三' } as unknown as User,
token: 'token'
} as never)
const wrapper = mount(FormRenderer, {
props: { schema: schema(), modelValue: data, perms: {}, ...props },
global: { plugins: [pinia, i18n] }
})
await flushPromises()
return wrapper.text()
}
/** 单子办结时存下来的那一份:项目分类是 PMS,四个项目的值都带过 */
const PMS = { reason: '客户巡访', project_type: 'PMS', project_pms: '智慧园区', project_scc: 'SCC-01', area_cf: '北京', project_xz: '行政A' }
describe('办结回看那一屏:条件把「默认隐藏 + 显示条件」那一组放回来', () => {
it('条件成立的那一格摆出来,隔壁条件不成立的那一格仍旧不露', async () => {
const text = await open(PMS, { readonly: true })
expect(text).toContain('PMS项目名称')
expect(text).toContain('智慧园区')
expect(text).not.toContain('VDI项目名称')
})
it('没配条件的「默认隐藏」一个字都不改:值存着也不上这一屏', async () => {
expect(await open(PMS, { readonly: true })).not.toContain('行政项目名称')
})
it('已经存了值的条件格不凭条件收:回看屏讲的是"存过的都摊开"', async () => {
// 出发地没配条件,照旧摊开;把项目分类改走,PMS 那一格按默认隐藏那一寸退回不露
const flipped = await open({ ...PMS, project_type: 'VDI' }, { readonly: true })
expect(flipped).toContain('出发地')
expect(flipped).not.toContain('PMS项目名称')
expect(flipped).toContain('VDI项目名称')
})
it('环节明着要回去的那一格,条件翻走了也摊开(与从前那句保守口径同寸)', async () => {
const text = await open({ ...PMS, project_type: 'VDI' }, { readonly: true, perms: { project_pms: 'readonly' } })
expect(text).toContain('PMS项目名称')
// 而环节明写隐藏是最高优先级:条件成立也不给看
expect(await open(PMS, { readonly: true, perms: { project_pms: 'hidden' } })).not.toContain('PMS项目名称')
})
it('办理那一屏走的是另一句判据:联动往严收,两屏各说各的话', async () => {
// 能改值的这一屏:条件不成立就不摆,跟回看屏只放行不一样
expect(await open({ ...PMS, project_type: 'VDI' })).not.toContain('PMS项目名称')
expect(await open(PMS)).toContain('PMS项目名称')
})
})