import {beforeEach,afterEach,expect,it,vi} from 'vitest' import {mount,flushPromises} from '@vue/test-utils' import ElementPlus from 'element-plus' import Photos from '@/components/ArrivalPhotos/index.vue' const request=vi.hoisted(()=>vi.fn()) vi.mock('@/utils/request',()=>({default:request})) let wrapper,created,revoke beforeEach(()=>{request.mockReset();created=URL.createObjectURL;revoke=URL.revokeObjectURL;URL.createObjectURL=vi.fn(()=>`blob:photo-${Math.random()}`);URL.revokeObjectURL=vi.fn()}) afterEach(()=>{wrapper?.unmount();document.body.innerHTML='';URL.createObjectURL=created;URL.revokeObjectURL=revoke}) function page(props={}){wrapper=mount(Photos,{attachTo:document.body,props:{requestId:10,modelValue:[],...props,'onUpdate:modelValue':value=>wrapper.setProps({modelValue:value})},global:{plugins:[ElementPlus]}});return wrapper} async function choose(files){const input=wrapper.find('input[type=file]');Object.defineProperty(input.element,'files',{value:files,configurable:true});await input.trigger('change');await flushPromises()} const picture=()=>new File(['image'],'到货.png',{type:'image/png'}) it('photos are optional and successful uploads are included in the model before submission',async()=>{ request.mockImplementation(async c=>c.method==='post'?{data:{id:c.data.get('uploadId'),name:'到货.png'}}:{});page();expect(wrapper.vm.pending).toBe(false) await choose([picture()]);expect(wrapper.props('modelValue')).toHaveLength(1);expect(wrapper.props('modelValue')[0].id).toMatch(/^[0-9a-f-]{36}$/);expect(wrapper.vm.pending).toBe(false) await wrapper.findAll('button').find(b=>b.text()==='移除').trigger('click');await flushPromises();expect(wrapper.props('modelValue')).toEqual([]);expect(request.mock.calls[1][0].method).toBe('delete') }) it('failed photos can be retried using the same upload identifier or removed without blocking optional registration',async()=>{ request.mockRejectedValueOnce(Error('模拟上传失败')).mockImplementation(async c=>({data:{id:c.data.get('uploadId'),name:'到货.png'}}));page();await choose([picture()]);expect(wrapper.vm.pending).toBe(true);expect(wrapper.text()).toContain('模拟上传失败') await wrapper.findAll('button').find(b=>b.text()==='重试').trigger('click');await flushPromises();expect(wrapper.vm.pending).toBe(false);expect(request.mock.calls[0][0].data.get('uploadId')).toBe(request.mock.calls[1][0].data.get('uploadId')) }) it('read-only saved photos load privately on demand and never expose upload or remove controls',async()=>{ request.mockResolvedValue(new Blob(['private'],{type:'image/png'}));page({compact:true,readonly:true,disabled:true,modelValue:[{id:'11111111-1111-4111-8111-111111111111',name:'到货.png'}]});expect(request).not.toHaveBeenCalled() await wrapper.find('button').trigger('click');await flushPromises();expect(request).toHaveBeenCalledOnce();expect(request.mock.calls[0][0].url).toContain('/content');expect(document.body.textContent).not.toContain('上传照片');expect(document.body.textContent).not.toContain('移除') }) it('non-images and oversized files do not upload; each arrival is limited to nine photos',async()=>{ request.mockImplementation(async c=>({data:{id:c.data.get('uploadId'),name:'到货.png'}}));page();await choose([new File(['not image'],'file.txt',{type:'text/plain'}),new File([new Uint8Array(20*1024*1024+1)],'large.png',{type:'image/png'})]);expect(request).not.toHaveBeenCalled() await choose(Array.from({length:10},picture));expect(wrapper.props('modelValue')).toHaveLength(9);expect(request).toHaveBeenCalledTimes(9) }) it('saved photo loading errors can retry without editing the arrival or its photo metadata',async()=>{ request.mockRejectedValueOnce(Error('模拟读取失败')).mockResolvedValue(new Blob(['private'],{type:'image/png'}));page({readonly:true,modelValue:[{id:'11111111-1111-4111-8111-111111111111',name:'到货.png'}]});await flushPromises();expect(wrapper.text()).toContain('加载失败,点击重试');expect(wrapper.props('modelValue')[0].error).toBeUndefined() await wrapper.find('.arrival-photo-placeholder').trigger('click');await flushPromises();expect(request).toHaveBeenCalledTimes(2);expect(wrapper.text()).not.toContain('模拟读取失败') })