unis-oa/frontend/src/components/oa/design/LinkagePanel.autoFill.spec.ts

76 lines
3.6 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
/**
* 「字典带出」那一栏挑得到哪几列。
*
* 这一栏以前是一只手填的输入框,placeholder 写着「附加值键名」,于是**只能带出附加值**:
* 想把字典的键值(编码)也写到别的格子上,配表的人压根没地方说这件事。
* 这里钉两件事:列清单里有「键值」,以及老表单里那句裸的 ``price`` 还认得出是附加值那一列。
*/
import { describe, expect, it, vi } from 'vitest'
import { flushPromises, mount } from '@vue/test-utils'
import { createI18n } from 'vue-i18n'
import zh from '@/i18n/locales/zh'
import LinkagePanel from './LinkagePanel.vue'
import Select from '@/components/common/Select.vue'
import type { FormFieldDef } from '@/types'
vi.mock('@/api/dicts', () => ({
listDictsCached: vi.fn(async () => [{ code: 'material', name: '物料' }]),
dictValuesCached: vi.fn(async () => ({
material: [
{ code: 'material', value: 'M-1', label: '六角螺栓', extra: { price: 12.5 } },
{ code: 'material', value: 'M-2', label: '弹垫', extra: { price: 3, spec: 'M8' } }
]
}))
}))
async function panel(field: FormFieldDef) {
const wrapper = mount(LinkagePanel, {
props: { field, siblings: [field, { key: 'code', label: '编码', type: 'text' } as FormFieldDef] },
global: { plugins: [createI18n({ legacy: false, locale: 'zh', messages: { zh } })] }
})
await flushPromises()
return wrapper
}
/** 带出那一行的「取哪一列」下拉。 */
function pickSelect(wrapper: ReturnType<typeof mount>) {
return wrapper.findComponent(Select, '[data-test="auto-fill-pick"]')
}
function lastPatch(wrapper: ReturnType<typeof mount>) {
return (wrapper.emitted('patch') as Record<string, unknown>[][] | undefined)?.at(-1)?.[0]
}
describe('带出那一栏挑列', () => {
it('挑得到名称、键值,以及这本字典附加值里出现过的那几个键', async () => {
const wrapper = await panel({ key: 'material', label: '物料', type: 'select', dictCode: 'material', autoFill: [{ to: 'code' }] })
expect(pickSelect(wrapper).props('options')).toEqual([
{ value: '', label: '名称(默认)' },
{ value: 'value', label: '键值(编码)' },
{ value: 'extra.price', label: '附加值 · price' },
{ value: 'extra.spec', label: '附加值 · spec' }
])
})
it('点了「键值」写进配置的是 value,不是让人猜的一句中文', async () => {
const wrapper = await panel({ key: 'material', label: '物料', type: 'select', dictCode: 'material', autoFill: [{ to: 'code' }] })
await pickSelect(wrapper).vm.$emit('update:model-value', 'value')
expect(lastPatch(wrapper)).toEqual({ autoFill: [{ to: 'code', pick: 'value' }] })
})
it('老表单那句裸的 price 还念得出来:它是附加值那一列的旧写法', async () => {
const wrapper = await panel({ key: 'material', label: '物料', type: 'select', dictCode: 'material', autoFill: [{ pick: 'price', to: 'price' }] })
const select = pickSelect(wrapper)
const options = select.props('options') as { value: string; label: string }[]
expect(options).toContainEqual({ value: 'price', label: '附加值 · price(旧写法)' })
// 那一栏认的还是原来那个值:没被偷偷改写成 extra.price,配置照旧带得出单价
expect(select.props('modelValue')).toBe('price')
})
it('没绑字典的格子不出这一栏', async () => {
const wrapper = await panel({ key: 'reason', label: '事由', type: 'text' })
expect(wrapper.find('[data-test="auto-fill-pick"]').exists()).toBe(false)
})
})