2026-10-27 09:04:41 +08:00
|
|
|
import {afterEach,expect,it,vi} from 'vitest'
|
2026-10-23 17:00:47 +08:00
|
|
|
import {mount,flushPromises} from '@vue/test-utils'
|
2026-10-27 09:04:41 +08:00
|
|
|
import ElementPlus,{ElMessageBox} from 'element-plus'
|
2026-10-23 17:00:47 +08:00
|
|
|
import Preview from '@/components/PurchaseSheet/PastePreview.vue'
|
|
|
|
|
import {inspectPurchasePaste,purchasePasteMapping,buildPurchasePastePayload} from '@/utils/purchase-paste-preview'
|
|
|
|
|
import {parseClipboardTable,pasteRectangle} from '@/utils/purchase-sheet'
|
|
|
|
|
|
|
|
|
|
const columns=[{field:'materialName',label:'物品名称'},{field:'materialParams',label:'规格型号'},{field:'quantity',label:'数量'},{field:'unit',label:'单位'},{field:'actualUnitPrice',label:'实际单价'},{field:'finalUnitPrice',label:'报价单价'},{field:'projectId',label:'项目'},{field:'materialBrand',label:'品牌'},{field:'productionTaskId',label:'生产计划'}]
|
|
|
|
|
const mounted=[]
|
|
|
|
|
afterEach(()=>{for(const wrapper of mounted.splice(0))wrapper.unmount()})
|
|
|
|
|
const prepared=(text,startField='materialName')=>inspectPurchasePaste(text,columns,startField)
|
|
|
|
|
const payload=model=>buildPurchasePastePayload(model.matrix,model.mapping,columns,model.hasHeader)
|
|
|
|
|
function render(text,extra={}){const wrapper=mount(Preview,{props:{visible:true,text,columns,startField:'materialName',...extra},attachTo:document.body,global:{plugins:[ElementPlus]}});mounted.push(wrapper);return wrapper}
|
|
|
|
|
|
|
|
|
|
it('recognizes headers and reorders mapped columns without treating computed amounts as prices',()=>{
|
|
|
|
|
const model=prepared('报价单价\t实际金额\t物品名称\t采购数量\n9.8\t29.4\t螺丝\t3')
|
|
|
|
|
expect(model.hasHeader).toBe(true);expect(model.mapping).toEqual(['finalUnitPrice','','materialName','quantity'])
|
|
|
|
|
const result=payload(model);expect(result.fields).toEqual(['materialName','quantity','finalUnitPrice']);expect(parseClipboardTable(result.text)).toEqual([['螺丝','3','9.8']]);expect(result.skippedColumns).toBe(1)
|
|
|
|
|
})
|
|
|
|
|
it('starts unheaded rectangles at the selected field and lets users manually change header treatment',()=>{
|
|
|
|
|
const model=prepared('2\t个\n3\t套','quantity')
|
|
|
|
|
expect(model.hasHeader).toBe(false);expect(model.mapping).toEqual(['quantity','unit']);expect(payload(model).rowCount).toBe(2)
|
|
|
|
|
expect(purchasePasteMapping([['名称','数量'],['电机','4']],columns,false,'materialParams')).toEqual(['materialParams','quantity'])
|
|
|
|
|
})
|
|
|
|
|
it('keeps skipped mapping gaps untouched when applied through the existing atomic paste engine',()=>{
|
|
|
|
|
const model=prepared('报价单价\t实际金额\t物品名称\n11\t22\t新物品')
|
|
|
|
|
const result=payload(model),original=[{materialName:'旧物品',materialParams:'旧规格',materialBrand:'旧品牌',quantity:2,unit:'个',actualUnitPrice:7,finalUnitPrice:8}]
|
|
|
|
|
const rows=pasteRectangle(original,result.text,0,0,result.fields,{emptyRow:()=>({}),readonly:()=>false,canEditField:()=>true,fieldLocked:()=>false,items:[],units:['个'],projects:[],categories:[],plans:[]})
|
|
|
|
|
expect(rows[0]).toMatchObject({materialName:'新物品',quantity:2,unit:'个',actualUnitPrice:7,finalUnitPrice:11,materialParams:'旧规格',materialBrand:'旧品牌'})
|
|
|
|
|
expect(original[0].materialName).toBe('旧物品')
|
|
|
|
|
expect(()=>pasteRectangle(original,result.text,0,0,result.fields,{emptyRow:()=>({}),readonly:()=>false,canEditField:(_,field)=>field!=='finalUnitPrice',items:[],units:['个']})).toThrow('不可通过粘贴修改')
|
|
|
|
|
expect(original[0].finalUnitPrice).toBe(8)
|
|
|
|
|
})
|
|
|
|
|
it('serializes quoted multiline cells and keeps blank data rows instead of silently dropping them',()=>{
|
|
|
|
|
const model=prepared('物品名称\t数量\n"电机\n附件"\t2\n\t\n风扇\t3')
|
|
|
|
|
expect(payload(model).rowCount).toBe(3);expect(parseClipboardTable(payload(model).text)).toEqual([['电机\n附件','2'],['',''],['风扇','3']])
|
|
|
|
|
})
|
|
|
|
|
it('blocks duplicate targets, unknown targets, missing mappings, and nonrectangular data',()=>{
|
|
|
|
|
const model=prepared('物品名称\t名称\n电机\t风扇')
|
|
|
|
|
expect(()=>payload(model)).toThrow('对应了多个源列')
|
|
|
|
|
expect(()=>buildPurchasePastePayload(model.matrix,['missing',''],columns,true)).toThrow('目标列已变化')
|
|
|
|
|
expect(()=>buildPurchasePastePayload(model.matrix,['',''],columns,true)).toThrow('至少选择')
|
|
|
|
|
expect(()=>prepared('甲\t乙\n丙')).toThrow('矩形')
|
|
|
|
|
expect(()=>prepared('物品名称\n"不完整')).toThrow('引号未闭合')
|
|
|
|
|
expect(()=>payload(prepared('物品名称'))).toThrow('没有明细')
|
|
|
|
|
})
|
|
|
|
|
it('accepts all 2000 data rows with a header but never truncates oversized pastes',()=>{
|
|
|
|
|
const text='物品名称\t数量\n'+Array.from({length:2000},(_,i)=>`物品${i}\t1`).join('\n')
|
|
|
|
|
expect(payload(prepared(text)).rowCount).toBe(2000)
|
|
|
|
|
const oversized=prepared(Array.from({length:2001},()=>`电机\t1`).join('\n'))
|
|
|
|
|
expect(()=>payload(oversized)).toThrow('最多粘贴2000行')
|
|
|
|
|
expect(()=>prepared(text+'\n多余\t1')).toThrow('最多粘贴2000行')
|
|
|
|
|
})
|
|
|
|
|
it('renders only twenty preview records but emits every mapped row and waits for the parent to close',async()=>{
|
|
|
|
|
const w=render('数量\t物品名称\n'+Array.from({length:25},(_,i)=>`${i+1}\t物品${i}`).join('\n'));await flushPromises()
|
|
|
|
|
expect(w.findComponent({name:'ElTable'}).props('data')).toHaveLength(20)
|
|
|
|
|
expect(document.body.textContent).toContain('共 25 行')
|
|
|
|
|
const button=w.findAllComponents({name:'ElButton'}).find(button=>button.text()==='应用到表格');await button.trigger('click')
|
|
|
|
|
const result=w.emitted('apply')[0][0];expect(result.fields).toEqual(['materialName','quantity']);expect(parseClipboardTable(result.text)).toHaveLength(25)
|
|
|
|
|
expect(w.emitted('update:visible')).toBeUndefined()
|
|
|
|
|
})
|
|
|
|
|
it('duplicates are visible and disabled until the user explicitly skips one source column',async()=>{
|
|
|
|
|
const w=render('物品名称\t名称\n甲\t乙');await flushPromises()
|
|
|
|
|
const apply=w.findAllComponents({name:'ElButton'}).find(button=>button.text()==='应用到表格');expect(apply.props('disabled')).toBe(true)
|
|
|
|
|
const selects=w.findAllComponents({name:'ElSelect'});expect(selects).toHaveLength(2);selects[1].vm.$emit('update:modelValue','');await flushPromises()
|
2026-10-27 09:04:41 +08:00
|
|
|
expect(apply.props('disabled')).toBe(false);await apply.trigger('click');expect(w.emitted('apply')[0][0]).toEqual({text:'甲',fields:['materialName'],sourceStartLine:2})
|
2026-10-23 17:00:47 +08:00
|
|
|
})
|
|
|
|
|
it('busy state prevents duplicate application and keeps user mappings on an external apply failure',async()=>{
|
|
|
|
|
const w=render('数量\n2',{busy:true});await flushPromises()
|
|
|
|
|
const apply=w.findAllComponents({name:'ElButton'}).find(button=>button.text()==='应用到表格');await apply.trigger('click');expect(w.emitted('apply')).toBeUndefined()
|
|
|
|
|
await w.setProps({busy:false});await apply.trigger('click');expect(w.emitted('apply')).toHaveLength(1)
|
|
|
|
|
expect(w.findComponent({name:'ElDialog'}).props('modelValue')).toBe(true)
|
|
|
|
|
expect(w.findAllComponents({name:'ElSelect'})[0].props('modelValue')).toBe('quantity')
|
|
|
|
|
})
|
2026-10-27 09:04:41 +08:00
|
|
|
it('shows all paste issues and jumps beyond the first twenty copied lines for direct correction',async()=>{
|
|
|
|
|
const text='物品名称\t单位\t数量\n'+Array.from({length:60},(_,i)=>`设备${i+1}\t件\t1`).join('\n'),w=render(text,{units:['件','台']});await flushPromises();const issues=[{sourceLine:44,line:87,offset:42,field:'unit',materialName:'设备43',materialParams:'S43',currentValue:'件',expectedValue:'台',message:'单位与物品档案不一致:当前“件”,档案“台”'}];await w.setProps({issues});await flushPromises()
|
|
|
|
|
expect(w.findComponent({name:'ElTable'}).props('data')[0]).toMatchObject({sourceLine:44,line:87,materialName:'设备43'});expect(document.body.textContent).toContain('本次未粘贴任何数据');expect(w.findAllComponents({name:'ElButton'}).some(button=>button.text()==='重新校验并应用')).toBe(true)
|
|
|
|
|
await w.findAllComponents({name:'ElButton'}).find(button=>button.text()==='定位修改').trigger('click');await flushPromises();expect(w.findComponent({name:'ElTable'}).props('data').some(row=>row.line===44)).toBe(true)
|
|
|
|
|
const select=w.findAllComponents({name:'ElSelect'}).find(component=>component.props('ariaLabel')==='源内容第44行单位');expect(select).toBeTruthy();select.vm.$emit('update:modelValue','台');await flushPromises()
|
|
|
|
|
await w.findAllComponents({name:'ElButton'}).find(button=>button.text()==='重新校验并应用').trigger('click');const applied=w.emitted('apply')[0][0];expect(parseClipboardTable(applied.text)).toHaveLength(60);expect(parseClipboardTable(applied.text)[42]).toEqual(['设备43','1','台']);expect(applied.sourceStartLine).toBe(2)
|
|
|
|
|
expect(w.props('text')).toBe(text);expect(w.emitted('update:visible')).toBeUndefined()
|
|
|
|
|
})
|
|
|
|
|
it('edits preview values before applying without mutating the source clipboard and protects cancelling corrections',async()=>{
|
|
|
|
|
const text='物品名称\t数量\n旧名称\t2',w=render(text);await flushPromises();const name=w.findAllComponents({name:'ElInput'}).find(component=>component.props('ariaLabel')==='源内容第2行物品名称');name.vm.$emit('update:modelValue','新名称');await flushPromises();const confirm=vi.spyOn(ElMessageBox,'confirm').mockRejectedValueOnce('cancel')
|
|
|
|
|
try{await w.findAllComponents({name:'ElButton'}).find(button=>button.text()==='取消').trigger('click');await flushPromises();expect(confirm).toHaveBeenCalled();expect(w.emitted('update:visible')).toBeUndefined();await w.findAllComponents({name:'ElButton'}).find(button=>button.text()==='应用到表格').trigger('click');expect(w.emitted('apply')[0][0].text).toBe('新名称\t2');expect(w.props('text')).toBe(text)}finally{confirm.mockRestore()}
|
|
|
|
|
})
|