import { flushPromises, mount } from '@vue/test-utils' import ElementPlus, { ElMessage } from 'element-plus' import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest' import Outbound from './Outbound.vue' const outboundStore = vi.hoisted(() => ({ outboundList: [], loading: false, page: { currentPage: 1, pageSize: 20, total: 0 }, fetchOutboundList: vi.fn(), fetchOutboundDetail: vi.fn(), createOutbound: vi.fn(), updateOutbound: vi.fn(), deleteOutbound: vi.fn() })) const materialsStore = vi.hoisted(() => ({ materialOptions: [], fetchMaterialOptions: vi.fn() })) const stockStore = vi.hoisted(() => ({ stockList: [], fetchAllStock: vi.fn(), fetchStockByMaterialId: vi.fn() })) vi.mock('../stores/outbound', () => ({ useOutboundStore: () => outboundStore })) vi.mock('../stores/materials', () => ({ useMaterialsStore: () => materialsStore })) vi.mock('../stores/stock', () => ({ useStockStore: () => stockStore })) vi.mock('../stores/auth', () => ({ useAuthStore: () => ({ isAdmin: true }) })) function mountOutbound() { return mount(Outbound, { global: { plugins: [ElementPlus], stubs: { teleport: true, ElSelect: { props: ['modelValue'], template: '
' }, ElOption: { props: ['label', 'value', 'disabled'], template: '{{ label }}' } } } }) } describe('出库单表单必填校验', () => { beforeEach(() => { vi.clearAllMocks() }) afterEach(() => vi.restoreAllMocks()) test('未填写接收方时提示友好信息且不发送请求', async () => { const warning = vi.spyOn(ElMessage, 'warning').mockImplementation(() => {}) const wrapper = mountOutbound() await flushPromises() await wrapper.findAll('button').find(button => button.text().includes('新增出库单')).trigger('click') await flushPromises() await wrapper.findAll('button').find(button => button.text().includes('保存')).trigger('click') await flushPromises() expect(warning).toHaveBeenCalledWith('请输入接收方') expect(outboundStore.createOutbound).not.toHaveBeenCalled() wrapper.unmount() }) })