cmvr-wms/ruoyi-ui-vue3/tests/runtime/batch-file-upload.spec.js
lixiaolong 771574fef7 feat: 完善采购协同、到货照片及验收发票流程
优化采购实时编辑与权限,支持先到货后补报价、按批次补录照片和批量附件上传。更新验收报告模板及明细选择,增加验收发票独立管理和付款前置校验。完善 BOM 数据权限及 App 更新兼容性。
2026-11-23 14:41:42 +08:00

30 lines
4.1 KiB
JavaScript

import {expect,it,vi,afterEach} from 'vitest'
import {mount,flushPromises} from '@vue/test-utils'
import ElementPlus from 'element-plus'
import Batch from '../../src/components/BatchFileUpload/index.vue'
import Documents from '../../src/components/PurchaseDocuments/index.vue'
const api=vi.hoisted(()=>({list:vi.fn(),upload:vi.fn()}))
vi.mock('@/store/modules/user',()=>({default:()=>({permissions:['*:*:*']})}))
vi.mock('@/api/warehouse/purchase-documents',()=>({listPurchaseDocuments:api.list,uploadPurchaseDocument:api.upload,savePurchaseDocumentNumber:vi.fn(),removePurchaseDocument:vi.fn(),readPurchaseDocument:vi.fn(),getPurchaseAcceptanceConfirmation:vi.fn(),confirmPurchaseAcceptance:vi.fn()}))
const mounted=[];afterEach(()=>{mounted.splice(0).forEach(w=>w.unmount());vi.clearAllMocks()})
const file=name=>new File(['test'],name,{type:'application/pdf'})
async function select(w,files){const input=w.find('input[type=file]');Object.defineProperty(input.element,'files',{value:files,configurable:true});await input.trigger('change');await flushPromises()}
function make(props){const w=mount(Batch,{props,global:{plugins:[ElementPlus]}});mounted.push(w);return w}
it('queues every selected file, continues past one failure and retries only that file with the original id',async()=>{
const upload=vi.fn().mockResolvedValueOnce({}).mockRejectedValueOnce(Error('network failed')).mockResolvedValueOnce({}).mockResolvedValueOnce({});const w=make({upload})
await select(w,[file('a.pdf'),file('b.pdf'),file('c.pdf')]);expect(upload.mock.calls.map(c=>c[0].name)).toEqual(['a.pdf','b.pdf','c.pdf']);expect(w.text()).toContain('network failed');expect(w.text()).toContain('已上传 2 / 3')
const id=upload.mock.calls[1][1];await w.findAll('button').find(b=>b.text().includes('重试')).trigger('click');await flushPromises();expect(upload).toHaveBeenCalledTimes(4);expect(upload.mock.calls[3][1]).toBe(id);expect(w.text()).toContain('已上传 3 / 3')
})
it('honors remaining slots and single-file controls without truncating a selection',async()=>{
const upload=vi.fn();const w=make({upload,remaining:1,limit:1});expect(w.find('input').element.multiple).toBe(false);await select(w,[file('a.pdf'),file('b.pdf')]);expect(upload).not.toHaveBeenCalled()
})
it('validates files independently and prevents double selection during an active upload',async()=>{
let finish;const upload=vi.fn(()=>new Promise(r=>{finish=r}));const w=make({upload,validate:f=>f.name==='bad.pdf'?'格式错误':''});await select(w,[file('bad.pdf'),file('ok.pdf')]);expect(upload).toHaveBeenCalledTimes(1);expect(w.text()).toContain('格式错误');await select(w,[file('extra.pdf')]);expect(upload).toHaveBeenCalledTimes(1);finish({});await flushPromises();expect(w.text()).toContain('已上传 1 / 2')
})
it('pause leaves unstarted files in the queue and resumes with a stable upload id',async()=>{
const upload=vi.fn((_f,_id,signal)=>new Promise((_resolve,reject)=>signal.addEventListener('abort',()=>reject(Error('aborted')))));const w=make({upload});await select(w,[file('a.pdf'),file('b.pdf')]);const id=upload.mock.calls[0][1];await w.findAll('button').find(b=>b.text()==='暂停').trigger('click');await flushPromises();expect(upload).toHaveBeenCalledTimes(1);upload.mockResolvedValue({});await w.findAll('button').find(b=>b.text().includes('重试')).trigger('click');await flushPromises();expect(upload.mock.calls[1][1]).toBe(id);expect(w.text()).toContain('已上传 2 / 2')
})
it('purchase documents upload all chosen PDFs to the current batch and category, then refresh once',async()=>{
api.list.mockResolvedValue({data:[{kind:'REQUEST',label:'采购申请',files:[],number:'',version:1}]});api.upload.mockResolvedValue({});const w=mount(Documents,{global:{plugins:[ElementPlus]},attachTo:document.body});mounted.push(w);await w.vm.open('batch',7,'批次','REQUEST');await flushPromises();const picker=w.findComponent(Batch);await select(picker,[file('申请.pdf'),file('报价.pdf')]);expect(api.upload).toHaveBeenCalledTimes(2);expect(api.upload.mock.calls.every(c=>c[0]==='batch'&&c[1]===7&&c[2]==='REQUEST')).toBe(true);expect(api.list).toHaveBeenCalledTimes(2)
})