import {afterEach,expect,it,vi} from 'vitest' import {shallowMount} from '@vue/test-utils' import Operations from '../../src/components/Production/StockOperations.vue' const api=vi.hoisted(()=>({outStock:vi.fn().mockResolvedValue({}),moveStock:vi.fn(),taskAction:vi.fn()})) vi.mock('@/api/warehouse/stock',()=>({outStock:api.outStock,moveStock:api.moveStock})) vi.mock('@/api/production/tasks',()=>({taskAction:api.taskAction})) vi.mock('@/api/warehouse/area',()=>({optionselectArea:vi.fn().mockResolvedValue({data:[]})})) vi.mock('@/utils/use-compact-layout',()=>({useCompactLayout:()=>({compact:false})})) const mounted=[] afterEach(()=>{for(const wrapper of mounted.splice(0))wrapper.unmount();vi.clearAllMocks()}) function render(data={}){ const wrapper=shallowMount(Operations,{data:()=>({visible:true,date:'2026-10-23',rows:[8,6,14,16,24].map((stockNum,i)=>({id:i+1,projectId:71,productId:i+10,areaId:1,stockNum,quantity:1,done:false,failed:false,key:`test-${i}`})),...data}),global:{stubs:{ElDialog:{template:'
'},ElButton:{props:['disabled'],template:''},ElTable:{template:'
'},ElTableColumn:true,DialogWorkspace:{template:'
'}}}}) mounted.push(wrapper);return wrapper } const fillButton=w=>w.findAll('button').find(button=>button.text()==='全部填入可用数量') it('fills all selected quantities without submitting and still permits individual edits',async()=>{ const w=render();expect(w.vm.rows.map(r=>r.quantity)).toEqual([1,1,1,1,1]) await fillButton(w).trigger('click');expect(w.vm.rows.map(r=>r.quantity)).toEqual([8,6,14,16,24]) expect(api.outStock).not.toHaveBeenCalled();expect(api.moveStock).not.toHaveBeenCalled();expect(api.taskAction).not.toHaveBeenCalled() w.vm.rows[0].quantity=2;expect(w.vm.rows[0].quantity).toBe(2) expect(w.findAll('button').some(button=>button.text()==='确认出库')).toBe(true) }) it('only the explicit confirmation submits the filled quantities',async()=>{ const w=render();await fillButton(w).trigger('click');await w.vm.submit() expect(api.outStock.mock.calls.map(([data])=>data.outNum)).toEqual([8,6,14,16,24]);expect(w.vm.rows.every(r=>r.done)).toBe(true) expect(api.outStock.mock.calls[0][0]).toMatchObject({id:1,usageProjectId:71,outType:4,outTime:'2026-10-23 00:00:00'}) }) it('supports decimal and string quantities without changing successful or invalid rows',async()=>{ const w=render({rows:[{stockNum:'0.1250',quantity:0},{stockNum:2.3456,quantity:1},{stockNum:9,quantity:1,done:true},...['invalid',Infinity,0,-1,null].map(stockNum=>({stockNum,quantity:0}))]}) await fillButton(w).trigger('click');expect(w.vm.rows.map(r=>r.quantity)).toEqual([0.125,2.3456,1,0,0,0,0,0]) }) it('never fills during submission or after an attempt using the old stock snapshot',async()=>{ const w=render({saving:true});expect(fillButton(w).element.disabled).toBe(true);w.vm.fillAvailableQuantities();expect(w.vm.rows[0].quantity).toBe(1) await w.setData({saving:false,runRows:[{failed:true}]});expect(fillButton(w)).toBeUndefined();w.vm.fillAvailableQuantities();expect(w.vm.rows[0].quantity).toBe(1) }) it('does not offer or execute whole-stock filling for production allocation or other operation kinds',async()=>{ const w=render() for(const kind of ['ISSUE','MOVE','DELIVERY','SCRAP_STOCK','SCRAP_FINISHED']){await w.setData({kind});expect(fillButton(w)).toBeUndefined();w.vm.fillAvailableQuantities();expect(w.vm.rows[0].quantity).toBe(1)} await w.setData({kind:'USE',taskContext:{id:9}});expect(fillButton(w)).toBeUndefined();w.vm.fillAvailableQuantities();expect(w.vm.rows[0].quantity).toBe(1) }) it('disables the action if no pending row has a positive available quantity',()=>{ const w=render({rows:[{stockNum:9,quantity:1,done:true},{stockNum:0,quantity:0}]});expect(fillButton(w).element.disabled).toBe(true) }) it('exposes the same non-submitting shortcut on the compact layout',async()=>{ const w=render();await w.setData({compact:true});await fillButton(w).trigger('click');expect(w.vm.rows.map(r=>r.quantity)).toEqual([8,6,14,16,24]);expect(api.outStock).not.toHaveBeenCalled() }) it('keeps the form aligned above the quantity toolbar and reduces blank space for small batches',()=>{ const w=render();expect(w.find('.stock-operation-form').attributes('label-position')).toBe('top') expect(w.find('.stock-quantity-toolbar .stock-selection-summary').text()).toBe('待处理 5 项库存') expect(w.vm.workspaceStyle.height).toContain('504px');expect(w.find('.stock-batch-submit').text()).toBe('确认出库') expect(api.outStock).not.toHaveBeenCalled() })