- 添加生产环境服务器地址配置支持内外网切换 - 实现采购列表只看我创建的功能开关 - 更新构建脚本中的默认生产环境API地址 - 优化采购单表格单元格编辑体验改用自动补全组件 - 调整应用更新检查逻辑增强生产环境兼容性 - 修复开发服务器切换时的一些验证问题 - 增强应用启动时的服务地址配置灵活性
43 lines
6.6 KiB
JavaScript
43 lines
6.6 KiB
JavaScript
import {afterEach,beforeEach,expect,it,vi} from 'vitest'
|
|
import {mount,flushPromises} from '@vue/test-utils'
|
|
import ElementPlus from 'element-plus'
|
|
import Uploader from '@/components/ManagedFiles/Upload.vue'
|
|
import Attachments from '@/components/ManagedFiles/Attachments.vue'
|
|
import Library from '@/views/system/file/index.vue'
|
|
const api=vi.hoisted(()=>({upload:vi.fn(),download:vi.fn(),list:vi.fn(),remove:vi.fn(),cancel:vi.fn()}))
|
|
vi.mock('@/store/modules/user',()=>({default:()=>({id:2})}))
|
|
vi.mock('@/api/common/file-transfers',()=>({uploadFileResumable:api.upload,cancelFileTransfer:api.cancel}))
|
|
vi.mock('@/api/system/files',()=>({downloadFile:api.download,listFiles:api.list,removeFile:api.remove,fileSize:n=>n+' B'}))
|
|
let w
|
|
beforeEach(()=>{vi.resetAllMocks();api.download.mockResolvedValue();api.cancel.mockResolvedValue();api.list.mockResolvedValue({data:{rows:[],total:0,canUpload:true}})})
|
|
afterEach(()=>{w?.unmount();vi.unstubAllGlobals();document.body.innerHTML=''})
|
|
function editor(props={}){w=mount(Uploader,{props:{purpose:'noticeAttachment',modelValue:[],...props,'onUpdate:modelValue':value=>w.setProps({modelValue:value})},global:{plugins:[ElementPlus]}});return w}
|
|
async function choose(files){const input=w.find('input[type=file]');Object.defineProperty(input.element,'files',{value:files,configurable:true});await input.trigger('change');await flushPromises()}
|
|
it('uploads multiple notice attachments and emits their IDs while progress never moves backwards',async()=>{
|
|
api.upload.mockImplementation(async(file,target,{onProgress})=>{expect(target.purpose).toBe('noticeAttachment');onProgress({phase:'uploading',percent:80});await flushPromises();expect(w.text()).toContain('78%');onProgress({phase:'hashing',percent:10});await flushPromises();expect(w.text()).toContain('78%');return {data:{id:file.name,name:file.name,size:file.size}}});editor();await choose([new File(['a'],'a.txt'),new File(['b'],'b.txt')]);await vi.waitFor(()=>expect(w.props('modelValue').map(f=>f.id),w.text()).toEqual(['a.txt','b.txt']));expect(w.vm.pending).toBe(false);expect(w.emitted('uploaded')).toHaveLength(2)
|
|
})
|
|
it.each(['noticeAttachment','fileLibrary'])('uploads multiple files over internal HTTP without Web Crypto (%s)',async purpose=>{
|
|
vi.stubGlobal('crypto',undefined);api.upload.mockImplementation(async file=>({data:{id:file.name,name:file.name,size:file.size}}));editor({purpose});const apk=new File(['APK'],'app.apk');Object.defineProperty(apk,'size',{value:25*1024*1024});await choose([apk,new File(['guide'],'guide.txt')]);await vi.waitFor(()=>expect(w.props('modelValue').map(f=>f.id)).toEqual(['app.apk','guide.txt']));expect(api.upload).toHaveBeenCalledTimes(2);expect(w.findAll('.upload-job')).toHaveLength(2);expect(w.vm.pending).toBe(false)
|
|
})
|
|
it('failed uploads remain visible and block publishing until retried or explicitly removed',async()=>{
|
|
api.upload.mockRejectedValueOnce(new Error('upload failed')).mockResolvedValueOnce({data:{id:'one',name:'a.txt',size:1}});editor();await choose([new File(['a'],'a.txt')]);expect(w.vm.pending).toBe(true);expect(w.text()).toContain('upload failed');await w.findAll('button').find(b=>b.text()==='重试').trigger('click');await flushPromises();expect(w.vm.pending).toBe(false);expect(w.props('modelValue')[0].id).toBe('one')
|
|
})
|
|
it('notice editor hides completed transfer history once the attachment list has its file',async()=>{
|
|
api.upload.mockResolvedValue({data:{id:'one',name:'a.txt',size:1}});editor({showCompleted:false});await choose([new File(['a'],'a.txt')]);await vi.waitFor(()=>expect(w.props('modelValue')).toHaveLength(1));expect(w.find('.upload-jobs').exists()).toBe(false);expect(w.vm.pending).toBe(false)
|
|
})
|
|
it('existing attachment count is respected and empty files are rejected',async()=>{
|
|
editor({modelValue:[{id:'old'}],limit:1});await choose([new File(['a'],'a.txt')]);expect(api.upload).not.toHaveBeenCalled();w.unmount();editor();await choose([new File([],'empty.txt')]);expect(api.upload).not.toHaveBeenCalled()
|
|
})
|
|
it('dedicated app uploads accept APKs up to 250MB and reject ordinary files before transfer',async()=>{
|
|
api.upload.mockResolvedValue({data:{id:'app',name:'test.apk',size:250*1024*1024}});editor({purpose:'appRelease'});const apk=new File(['a'],'test.apk');Object.defineProperty(apk,'size',{value:250*1024*1024});await choose([new File(['a'],'notes.txt'),apk]);expect(api.upload).toHaveBeenCalledTimes(1);expect(api.upload.mock.calls[0][1]).toEqual({purpose:'appRelease'});const tooLarge=new File(['a'],'large.apk');Object.defineProperty(tooLarge,'size',{value:250*1024*1024+1});await choose([tooLarge]);expect(api.upload).toHaveBeenCalledTimes(1)
|
|
})
|
|
it('notice readers download attachments without editable controls and editors remove links in their draft',async()=>{
|
|
const file={id:'one',name:'说明.pdf',size:2};w=mount(Attachments,{props:{modelValue:[file]},global:{plugins:[ElementPlus]}});await w.find('.attachment-name').trigger('click');await flushPromises();expect(api.download).toHaveBeenCalledWith(file,expect.anything());expect(w.text()).not.toContain('移除');w.unmount();w=mount(Attachments,{props:{modelValue:[file],editable:true},global:{plugins:[ElementPlus]}});await w.findAll('button').find(b=>b.text()==='移除').trigger('click');expect(w.emitted('update:modelValue')).toEqual([[[]]])
|
|
})
|
|
it('file search preserves filters, resets page and cancels superseded list requests',async()=>{
|
|
w=mount(Library,{global:{plugins:[ElementPlus],stubs:{BusinessListPage:{template:'<div><slot name="header"/></div>'},Pagination:true,BusinessTable:true}}});await flushPromises();expect(api.list).toHaveBeenCalledWith(expect.objectContaining({page:1,size:20}),expect.anything());const state=w.vm.$.setupState;state.query.keyword='说明';state.query.page=4;state.search();await flushPromises();expect(api.list).toHaveBeenLastCalledWith(expect.objectContaining({keyword:'说明',page:1}),expect.anything())
|
|
})
|
|
it('removing a paused file also releases its resumable session, and a 1GB file is accepted without allocating it',async()=>{
|
|
const sessionId='11111111-1111-4111-8111-111111111111';api.upload.mockImplementationOnce(async(file,target,{onProgress})=>{onProgress({phase:'uploading',percent:1,sessionId});throw new Error('paused')});editor();const large=new File(['a'],'large.bin');Object.defineProperty(large,'size',{value:1024**3});await choose([large]);expect(api.upload).toHaveBeenCalledTimes(1);await w.findAll('button').find(b=>b.text()==='移除').trigger('click');await flushPromises();expect(api.cancel).toHaveBeenCalledWith(sessionId);expect(w.vm.pending).toBe(false);const tooLarge=new File(['a'],'too-large.bin');Object.defineProperty(tooLarge,'size',{value:1024**3+1});await choose([tooLarge]);expect(api.upload).toHaveBeenCalledTimes(1)
|
|
})
|