import {expect,it,vi} from 'vitest' import {mount,flushPromises} from '@vue/test-utils' import ElementPlus from 'element-plus' import Cost from '@/views/archive/ArchiveCost.vue' const api=vi.hoisted(()=>({archiveCost:vi.fn(),archiveCostPeople:vi.fn()})) vi.mock('@/api/archive',()=>api) it('the real select keeps its query when loading hides and reopens the dropdown',async()=>{ vi.useFakeTimers();let wrapper try{ const users=[{id:1,name:'管理员',account:'admin',rate:1000},{id:2,name:'李一',account:'liyi',rate:1000},{id:3,name:'王一',account:'wangyi',rate:1000}] api.archiveCostPeople.mockImplementation(async(_id,{keyword})=>{if(keyword)await new Promise(resolve=>setTimeout(resolve,10));return {data:users}}) wrapper=mount(Cost,{props:{projectId:21,version:3,editable:true,cost:{complete:true,data:{formatVersion:3,income:1000,labor:[{userId:2,name:'李一',duty:'研发',days:1,rate:1000}]}}},attachTo:document.body,global:{plugins:[ElementPlus]}}) await flushPromises();const input=wrapper.get('input[aria-label="项目参与人员"]');await input.trigger('focus');await wrapper.get('.el-select__wrapper').trigger('click');await flushPromises();await input.setValue('王');await vi.advanceTimersByTimeAsync(700);await flushPromises() expect(wrapper.vm.$.setupState.personKeyword).toBe('王');expect(wrapper.vm.$.setupState.matchingPeople.map(person=>person.name)).toEqual(['王一']);expect(api.archiveCostPeople.mock.calls.at(-1)[1].keyword).toBe('王') expect([...document.querySelectorAll('.el-select-dropdown__item')].map(element=>element.textContent)).toEqual(['王一 / wangyi']) }finally{wrapper?.unmount();vi.useRealTimers()} })