import { afterEach, expect, it, vi } from 'vitest' import { mount } from '@vue/test-utils' import ElementPlus from 'element-plus' import Cell from '@/components/PurchaseSheet/Cell.vue' import Sheet from '@/components/PurchaseSheet/index.vue' import { candidates } from '@/utils/purchase-sheet' vi.mock('@/utils/request',()=>({default:vi.fn()})) vi.mock('@/store/modules/user',()=>({default:()=>({permissions:['*:*:*']})})) vi.mock('@/store/modules/app',()=>({default:()=>({size:'default'})})) const wrappers=[] afterEach(()=>wrappers.splice(0).forEach(w=>w.unmount())) function setup(field, row, items) { const owner={saving:false,nameMenuRow:null,nameItems:items, items, fieldLocked:()=>false,cellError:()=>'',editableCell:()=>true,isEditing:true,displayCell:(r,f)=>r[f], rememberCell:()=>{},paste:()=>{},navigate:()=>{},searchNames:vi.fn(async()=>{}),loadName:vi.fn(async()=>{}), nameChoices:Sheet.methods.nameChoices,specs(r){return candidates(this.items,r,'spec')},brands:()=>[], specChanged:vi.fn(),nameChanged:vi.fn(),brandChanged:vi.fn(),itemLabel:i=>[i.productName,i.spec].filter(Boolean).join(' / ')} const w=mount(Cell,{props:{row,column:{field,label:field},index:0,owner,active:true},global:{plugins:[ElementPlus]}}) wrappers.push(w) return {w,owner,autocomplete:w.findComponent({name:'ElAutocomplete'})} } it('产品远程搜索结果即使没有旧下拉状态也可选择档案',async()=>{ const items=[{id:1,productName:'电机',spec:'型号A'},{id:2,productName:'电机支架',spec:'型号B'}] const {autocomplete,owner}=setup('materialName',{_key:'r',materialName:''},items) let suggestions await autocomplete.props('fetchSuggestions')('电机',value=>suggestions=value) expect(suggestions.map(x=>x.id)).toEqual([1,2]) await autocomplete.vm.$emit('select',suggestions[1]) expect(owner.nameChanged).toHaveBeenCalledWith(expect.anything(),'@item:2') }) it('规格保留原文本、展开可选其他规格并支持搜索和选择档案',async()=>{ const items=[{id:1,productName:'电机',brand:'甲',spec:'型号A',unit:'个'},{id:2,productName:'电机',brand:'甲',spec:'型号B',unit:'个'}] const {autocomplete,owner}=setup('materialParams',{_key:'r',materialName:'电机',materialBrand:'甲',materialParams:'型号A'},items) expect(autocomplete.props('modelValue')).toBe('型号A') let suggestions await autocomplete.props('fetchSuggestions')('型号A',value=>suggestions=value) expect(owner.loadName).toHaveBeenCalledWith('电机') expect(suggestions.map(x=>x.id)).toEqual([1,2]) await autocomplete.props('fetchSuggestions')('型号B',value=>suggestions=value) expect(suggestions.map(x=>x.id)).toEqual([2]) await autocomplete.vm.$emit('select',suggestions[0]) expect(owner.specChanged).toHaveBeenCalledWith(expect.anything(),'@item:2') })