cmvr-wms/ruoyi-ui-vue3/tests/runtime/resumable-upload.spec.js

17 lines
2.6 KiB
JavaScript
Raw Normal View History

import {expect,it,vi} from 'vitest'
import {mount} from '@vue/test-utils'
import ElementPlus from 'element-plus'
import Files from '../../src/components/ProductAttachments/index.vue'
const api=vi.hoisted(()=>({list:vi.fn(),upload:vi.fn(),cancel:vi.fn(),remove:vi.fn(),read:vi.fn()}))
vi.mock('@/api/production/product-editions',()=>({productAttachments:api.list,uploadProductAttachment:api.upload,cancelProductAttachmentTransfer:api.cancel,removeProductAttachment:api.remove,readProductAttachment:api.read}))
it('pause keeps the selected file, resume displays progress and success, and reopening never leaks another product transfer',async()=>{
api.list.mockResolvedValue({data:[]});let options
api.upload.mockImplementationOnce((_id,_file,_uploadId,signal,progress)=>new Promise((_resolve,reject)=>{options={signal,progress};progress({phase:'uploading',percent:50,bytes:10,resumed:false});signal.addEventListener('abort',()=>reject({code:'ERR_CANCELED'}),{once:true})})).mockImplementationOnce(async(_id,_file,_uploadId,_signal,progress)=>{progress({phase:'complete',percent:100,resumed:true});return {data:{id:'native'}}})
const wrapper=mount(Files,{global:{plugins:[ElementPlus],mocks:{$can:()=>true,$dateTime:()=>''}}})
await wrapper.vm.open({id:7,itemName:'测试产品'});const file=new File(['续传测试'],'说明.txt'),upload=wrapper.vm.upload({file});await Promise.resolve();expect(wrapper.vm.uploading).toBe(true);wrapper.vm.pauseUpload();await upload;expect(options.signal.aborted).toBe(true);expect(wrapper.vm.busy).toBe(false);expect(wrapper.vm.pendingFile).toBe(file);expect(wrapper.vm.transfer.phase).toBe('paused')
await wrapper.vm.resumeUpload();await new Promise(resolve=>setTimeout(resolve,0));expect(wrapper.vm.pendingFile).toBe(null);expect(wrapper.vm.transfer.phase).toBe('complete');await wrapper.vm.open({id:8,itemName:'另一个产品'});expect(wrapper.vm.transfer).toBe(null);wrapper.unmount()
})
it('cancelling a paused transfer never removes a business attachment',async()=>{
api.list.mockResolvedValue({data:[]});api.cancel.mockResolvedValue({code:200});const wrapper=mount(Files,{global:{plugins:[ElementPlus],mocks:{$can:()=>true,$dateTime:()=>''}}});await wrapper.vm.open({id:7,itemName:'测试产品'});wrapper.vm.pendingFile=new File(['test'],'说明.txt');wrapper.vm.transfer={phase:'paused',percent:50,sessionId:'11111111-1111-1111-1111-111111111111'};await wrapper.vm.cancelUpload();expect(api.cancel).toHaveBeenCalledWith('11111111-1111-1111-1111-111111111111');expect(api.remove).not.toHaveBeenCalled();expect(wrapper.vm.pendingFile).toBe(null);wrapper.unmount()
})