cmvr-wms/ruoyi-ui-vue3/tests/runtime/notice-attachments.spec.js

15 lines
1.6 KiB
JavaScript
Raw Normal View History

import {afterEach,expect,it,vi} from 'vitest'
import {mount,flushPromises} from '@vue/test-utils'
import ElementPlus from 'element-plus'
import * as icons from '@element-plus/icons-vue'
import Detail from '@/layout/components/HeaderNotice/DetailView.vue'
const api=vi.hoisted(()=>({get:vi.fn()}))
vi.mock('@/api/system/notice',()=>({getNotice:api.get}))
vi.mock('@/api/system/files',()=>({downloadFile:vi.fn(),fileSize:n=>String(n)}))
let w
afterEach(()=>{w?.unmount();document.body.innerHTML='';vi.resetAllMocks()})
function page(){w=mount(Detail,{attachTo:document.body,global:{plugins:[ElementPlus],components:icons,stubs:{ManagedAttachments:{props:['modelValue'],template:'<div class="attachment-fixture">{{ modelValue.map(f=>f.name).join() }}</div>'}}}})}
it('opening a preset notice still fetches its saved attachments and ignores a slower previous notice response',async()=>{
let old;api.get.mockImplementationOnce(()=>new Promise(resolve=>{old=resolve})).mockResolvedValueOnce({data:{noticeId:2,noticeTitle:'第二份公告',noticeContent:'内容',status:'0',attachments:[{id:'two',name:'操作手册.pdf'}]}});page();w.vm.open({noticeId:1,noticeTitle:'旧公告',noticeContent:'已有列表内容'});await w.vm.open({noticeId:2,noticeContent:'列表内容'});await flushPromises();expect(api.get).toHaveBeenCalledTimes(2);expect(document.body.textContent).toContain('操作手册.pdf');old({data:{noticeId:1,noticeTitle:'迟到的内容',attachments:[]}});await flushPromises();expect(document.body.textContent).toContain('操作手册.pdf');expect(document.body.textContent).not.toContain('迟到的内容')
})