Files
workspace/code/fms/fms-vue/tests/unit/module-rule-edit-modal.spec.js
T
2026-09-19 21:17:02 +08:00

124 lines
4.2 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.
/**
* 删除 SQL 规则编辑弹窗(ModuleRuleEditModal):
* - 只编辑 b_sql + b_message:SQL 必填、必须只读、必须含 :ids;提示文案必填;
* - 校验不通过时不提交(不 emit ok);
* - 打开时按当前行回显 SQL 与文案;标题按新增/编辑切换。
*/
import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
import { mount, flushPromises } from '@vue/test-utils'
import { createPinia, setActivePinia } from 'pinia'
import { nextTick } from 'vue'
import ModuleRuleEditModal from '@/views/module/module-management/ModuleRuleEditModal.vue'
import { IDS_PLACEHOLDER } from '@/views/module/module-management/ruleUtils'
vi.mock('@/services/api', () => ({ loadDataApi: vi.fn() }))
vi.mock('@/components/ui/message/message-manager.js', () => {
const m = { success: vi.fn(), error: vi.fn(), warning: vi.fn(), info: vi.fn() }
return { Message: m, default: m }
})
beforeAll(() => {
Object.defineProperty(HTMLElement.prototype, 'clientWidth', {
configurable: true,
get() {
return 5000
},
})
})
const ruleRow = () => ({
_rowKey: '-1',
b_id: '1900000000000000001',
b_module_id: 'order',
b_sql: `select b_id from bill where b_id in (${IDS_PLACEHOLDER})`,
b_message: '单据已销账,不能删除',
b_canuse: 1,
b_xh: 10,
})
async function mountModal(row = ruleRow(), isNew = false) {
const pinia = createPinia()
setActivePinia(pinia)
const wrapper = mount(ModuleRuleEditModal, {
props: { open: true, row, isNew },
global: { plugins: [pinia] },
attachTo: document.body,
})
await flushPromises()
await nextTick()
return wrapper
}
beforeEach(() => {
document.body.innerHTML = ''
})
describe('删除 SQL 规则编辑弹窗', () => {
it('SQL 为空时不提交', async () => {
const wrapper = await mountModal()
wrapper.vm.sqlText = ' '
await nextTick()
await wrapper.vm.onOk()
expect(wrapper.vm.sqlError).toBe('请填写规则 SQL')
expect(wrapper.emitted('ok')).toBeFalsy()
})
it('SQL 非只读时不提交', async () => {
const wrapper = await mountModal()
wrapper.vm.sqlText = `delete from bill where b_id in (${IDS_PLACEHOLDER})`
await nextTick()
await wrapper.vm.onOk()
expect(wrapper.vm.sqlError).toBe('规则 SQL 必须是只读查询(以 select 或 with 开头)')
expect(wrapper.emitted('ok')).toBeFalsy()
})
it('SQL 缺少 :ids 标记时不提交', async () => {
const wrapper = await mountModal()
wrapper.vm.sqlText = 'select b_id from bill'
await nextTick()
await wrapper.vm.onOk()
expect(wrapper.vm.sqlError).toBe('规则 SQL 必须包含 :ids 标记,用于限定本次待删记录')
expect(wrapper.emitted('ok')).toBeFalsy()
})
it('缺提示文案时不提交,填好后原样存 SQL 与文案', async () => {
const row = { ...ruleRow(), b_message: '' }
const wrapper = await mountModal(row)
wrapper.vm.sqlText = `select b_id from bill where b_id in (${IDS_PLACEHOLDER}) and b_settled = 1`
await nextTick()
await wrapper.vm.onOk()
expect(wrapper.vm.messageError).toBe('请填写提示文案')
expect(wrapper.emitted('ok')).toBeFalsy()
wrapper.vm.messageText = '单据已销账,不能删除'
await nextTick()
await wrapper.vm.onOk()
const saved = wrapper.emitted('ok')?.[0]?.[0]
expect(saved.b_sql).toBe(
`select b_id from bill where b_id in (${IDS_PLACEHOLDER}) and b_settled = 1`,
)
expect(saved.b_message).toBe('单据已销账,不能删除')
// 旧字段不再写入
expect(saved.b_predicate).toBeUndefined()
expect(saved.b_hook).toBeUndefined()
expect(wrapper.emitted('update:open')?.at(-1)).toEqual([false])
})
it('回显与标题:按当前行还原 SQL 与文案;新增/编辑标题不同', async () => {
const wrapper = await mountModal({ ...ruleRow() }, true)
expect(wrapper.vm.sqlText).toBe(`select b_id from bill where b_id in (${IDS_PLACEHOLDER})`)
expect(wrapper.vm.messageText).toBe('单据已销账,不能删除')
expect(wrapper.vm.modalTitle).toBe('新增删除规则')
const editWrapper = await mountModal(ruleRow(), false)
expect(editWrapper.vm.modalTitle).toBe('编辑删除规则')
})
})