Files
workspace/code/fms/fms-vue/tests/unit/module-auto-code-panel.spec.js
T
2026-09-17 21:29:08 +08:00

69 lines
2.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, expect, it } from 'vitest'
import { mount } from '@vue/test-utils'
import ModuleAutoCodePanel from '@/views/module/module-management/ModuleAutoCodePanel.vue'
import Switch from '@/components/ui/switch/switch.vue'
const fields = [
{ b_field: 'b_id', b_name: '编码', b_type: 'input', b_canuse: 1 },
{ b_field: 'b_no', b_name: '业务单号', b_type: 'input', b_canuse: 1 },
{ b_field: 'b_bz', b_name: '备注', b_type: 'textarea', b_canuse: 1 },
]
/** 模拟父级 v-model:emit 回来要显式写回 prop,否则复现不了「立刻回读旧值」的问题 */
function mountPanel() {
let wrapper
wrapper = mount(ModuleAutoCodePanel, {
props: {
moduleId: 'm1',
fields,
modelValue: [],
'onUpdate:modelValue': (value) => wrapper.setProps({ modelValue: value }),
},
})
return wrapper
}
describe('自动编码面板', () => {
it('首次拨「启用」时惰性建行并写回 b_canuse(defineModel 不能写完立刻回读)', async () => {
const wrapper = mountPanel()
// 模块从未配置自动编码:草稿为空,此时拨开关会走 ensureRow 建行
wrapper.findComponent(Switch).vm.$emit('update:modelValue', true)
await wrapper.vm.$nextTick()
const emitted = wrapper.emitted('update:modelValue')
expect(emitted).toBeTruthy()
const row = emitted[0][0][0]
expect(row.b_canuse).toBe(1)
expect(row.b_module_id).toBe('m1')
// 业务编号字段自动取第一个可用文本字段(排除 b_id)
expect(row.b_field).toBe('b_no')
})
it('已有一行时就地改,不再新建也不重新 emit 整个数组', async () => {
let wrapper
wrapper = mount(ModuleAutoCodePanel, {
props: {
moduleId: 'm1',
fields,
modelValue: [{ b_module_id: 'm1', b_field: 'b_no', b_canuse: 0, b_prefix: 'A' }],
'onUpdate:modelValue': (value) => wrapper.setProps({ modelValue: value }),
},
})
wrapper.findComponent(Switch).vm.$emit('update:modelValue', true)
await wrapper.vm.$nextTick()
// 已落草稿:直接改这一行的属性(父级 ref 是深度响应式,改得见)
expect(wrapper.emitted('update:modelValue')).toBeUndefined()
const row = wrapper.props('modelValue')[0]
expect(row.b_canuse).toBe(1)
expect(row.b_prefix).toBe('A') // 沿用原行,未被占位行覆盖
})
it('紧凑网格固定竖排,不跟随全局横排配置', () => {
const wrapper = mountPanel()
expect(wrapper.find('form').classes()).toContain('fms-form-vertical')
})
})