cmvr-wms/ruoyi-ui-vue3/tests/runtime/purchase-grid.spec.js
lixiaolong cd86623061 feat(app): 添加服务器连接管理和到货操作功能
- 集成 ServerConnection 组件用于开发环境服务器切换
- 实现多服务器令牌隔离存储机制
- 添加 ArrivalOperation 屏幕处理到货和验收流程
- 支持待登记到货和待验收状态切换
- 优化库存操作界面的字段显示逻辑
- 增强 Excel 文件分片上传支持产品文件识别
- 添加 AI 助手会话滚动跟踪功能
- 实现开发服务器地址持久化存储
- 优化登录界面集成服务器连接组件
- 添加领用出库功能支持项目物资使用
- 改进盘点确认生效的操作流程
- 增强采购到货登记和验收的权限控制
- 优化移动端文件上传的断点续传机制
- 添加服务器切换时的数据清理保护机制
- 实现紧凑金额显示模式减少界面冗余
- 增强库存操作的数量验证和错误提示
- 优化 AI 助手的会话状态同步机制
2026-10-20 10:04:17 +08:00

68 lines
5.9 KiB
JavaScript

import {expect,it,vi} from 'vitest'
import {reactive,nextTick,computed,watch} from 'vue'
import {createRouter,createMemoryHistory} from 'vue-router'
import Sheet from '../../src/components/PurchaseSheet/index.vue'
import {createSheetEditor} from '../../src/utils/purchase-sheet-editor'
import {writeClipboardText} from '../../src/utils/purchase-grid-clipboard'
vi.mock('@/utils/request',()=>({default:vi.fn()}))
vi.mock('@/store/modules/user',()=>({default:()=>({permissions:['*:*:*']})}))
vi.mock('@/store/modules/app',()=>({default:()=>({size:'default'})}))
it('compact display filters rows without removing them from complete purchase saves',()=>{
const rows=[{_key:'zero',id:1,finalUnitPrice:0,purchaseStatus:0},{_key:'priced',id:2,finalUnitPrice:3,purchaseStatus:0}]
const vm={sheet:{rows},rowFilter:'',rowQueue:'all',rowSort:'original',onlyInvalid:false,compactAmounts:true,isEditing:false,viewReference:r=>r,projectName:()=>''}
vm.amountRowVisible=Sheet.methods.amountRowVisible.bind(vm)
expect(Sheet.computed.visibleRows.call(vm).map(r=>r.id)).toEqual([2])
expect(Sheet.methods.activeRows.call(vm).map(r=>r.id)).toEqual([1,2])
vm.isEditing=true;expect(vm.amountRowVisible({_key:'new',finalUnitPrice:0})).toBe(true)
})
it('viewing a moved target navigates to the full procurement route, not an embedded sheet replacement',async()=>{
const push=vi.fn(),openBatch=vi.fn(),vm={moveOutcome:72,saving:false,embedded:true,$route:{path:'/project/detail/1501',query:{}},$router:{push},openBatch}
await Sheet.methods.viewMoveTarget.call(vm)
expect(push).toHaveBeenCalledWith({path:'/procurement/details',query:{purchaseBatchId:'72'}})
expect(openBatch).not.toHaveBeenCalled();expect(vm.moveOutcome).toBeNull()
})
it('returning from a deep-linked batch removes its route parameter so reload and reopen work',async()=>{
const replace=vi.fn(),loadBatches=vi.fn(),vm={saving:false,dirty:false,sheet:{batch:{id:72}},embedded:false,$route:{path:'/procurement/details',query:{purchaseBatchId:'72',projectId:'9'}},$router:{replace},loadBatches}
await Sheet.methods.back.call(vm)
expect(replace).toHaveBeenCalledWith({path:'/procurement/details',query:{projectId:'9'}})
expect(vm.sheet).toBeNull();expect(loadBatches).not.toHaveBeenCalled()
})
it('target route navigation and back cooperate with the actual route watcher and query restoration',async()=>{
const router=createRouter({history:createMemoryHistory(),routes:[{path:'/procurement/details',component:{template:'<div/>'}}]})
await router.push('/procurement/details?purchaseBatchId=11');await router.isReady()
const vm=reactive({moveOutcome:72,saving:false,dirty:false,embedded:false,sheet:{batch:{id:11}},query:{},projectId:null,$router:router,_routeStamp:undefined,loadBatches:vi.fn(),openBatch:vi.fn(async batch=>{vm.sheet={batch}})})
Object.defineProperty(vm,'$route',{get:()=>router.currentRoute.value})
const stop=watch(()=>router.currentRoute.value.fullPath,()=>Sheet.methods.applyRoute.call(vm))
try{await Sheet.methods.viewMoveTarget.call(vm);await nextTick();expect(router.currentRoute.value.query.purchaseBatchId).toBe('72');expect(vm.sheet.batch.id).toBe(72)
await Sheet.methods.back.call(vm);await nextTick();expect(router.currentRoute.value.query.purchaseBatchId).toBeUndefined();expect(vm.sheet).toBeNull();expect(vm.loadBatches).toHaveBeenCalled()
}finally{stop()}
})
it('the actual undo/redo handlers commit buffered inputs and retain editor focus for repeated shortcuts',async()=>{
const root=document.createElement('div'),input=document.createElement('input');root.tabIndex=-1;root.append(input);document.body.append(root)
const vm=reactive({sheet:null,baseline:'',editing:true,isEditing:true,saving:false,history:[],redoHistory:[],selected:[],changedKeys:new Set(),persistDraft(){},$el:root,$nextTick:nextTick,focusSelectedCell:vi.fn()})
const editor=createSheetEditor(vm);vm._editor=editor;vm.finishCellEdit=Sheet.methods.finishCellEdit.bind(vm)
vm.sheet={batch:{},rows:[{_key:'r',quantity:1}],deletedIds:[]};vm.baseline=JSON.stringify(vm.sheet)
const canUndo=computed(()=>Sheet.computed.canUndo.call(vm))
try{editor.beginCell(vm.sheet.rows[0],'quantity');input.focus();input.addEventListener('blur',()=>{vm.sheet.rows[0].quantity=4},{once:true})
await Sheet.methods.undo.call(vm);expect(vm.sheet.rows[0].quantity).toBe(1);expect(document.activeElement).toBe(root)
await Sheet.methods.redo.call(vm);expect(vm.sheet.rows[0].quantity).toBe(4);expect(canUndo.value).toBe(true)
await Sheet.methods.undo.call(vm);expect(canUndo.value).toBe(false)
}finally{editor.stop();root.remove()}
})
it('spreadsheet copy uses the modern clipboard in secure contexts and falls back on internal HTTP',async()=>{
const copy=vi.fn(),exec=vi.fn(()=>true)
vi.stubGlobal('isSecureContext',true);Object.defineProperty(navigator,'clipboard',{configurable:true,value:{writeText:copy}})
try{await writeClipboardText('A\tB');expect(copy).toHaveBeenCalledWith('A\tB')
vi.stubGlobal('isSecureContext',false);Object.defineProperty(document,'execCommand',{configurable:true,value:exec});await writeClipboardText('C\tD');expect(exec).toHaveBeenCalledWith('copy');expect(document.querySelector('textarea')).toBeNull()
}finally{vi.unstubAllGlobals();delete navigator.clipboard;delete document.execCommand}
})
it('search input retains native shortcuts while grid shortcuts are intercepted exactly once',()=>{
const root=document.createElement('div'),field=document.createElement('input');field.className='search';root.append(field)
const undo=vi.fn(),redo=vi.fn(),vm={isEditing:true,saving:false,$el:root,undo,redo}
const event={target:field,key:'z',ctrlKey:true,preventDefault:vi.fn(),stopPropagation:vi.fn()}
Sheet.methods.editorShortcut.call(vm,event);expect(undo).not.toHaveBeenCalled();expect(event.preventDefault).not.toHaveBeenCalled()
event.target=root;Sheet.methods.editorShortcut.call(vm,event);expect(undo).toHaveBeenCalledOnce()
event.shiftKey=true;Sheet.methods.editorShortcut.call(vm,event);expect(redo).toHaveBeenCalledOnce()
})