cmvr-wms/ruoyi-ui-vue3/tests/unit/purchase-cancel-edit.test.mjs

36 lines
2.4 KiB
JavaScript
Raw Normal View History

import test from 'node:test'
import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
const source=readFileSync(new URL('../../src/components/PurchaseSheet/index.vue',import.meta.url),'utf8')
const method=source.slice(source.indexOf('async cancelEditing()'),source.indexOf('async back()')).trim().replace(/,$/,'')
const cancelEditing=Function(`return ({${method}}).cancelEditing`)()
function context({existing=false,confirm=true,saving=false}={}) {
const calls=[]
return {calls,saving,dirty:true,sheet:{batch:{id:existing?12:null},version:existing?'v1':null,unbatched:false},baseline:JSON.stringify({batch:{id:12},version:'v1',rows:[{materialName:'原始物品'}]}),
validationEnabled:true,onlyInvalid:true,editing:true,rowFilter:'filter',history:[1],selected:[1],commonProject:1,commonCategory:1,_activeCell:{},
$confirm:async(...args)=>{calls.push(['confirm',...args]);if(!confirm)throw 'cancel'},
draftId:()=>existing?'12':'new',clearDraft:id=>calls.push(['clear',id]),
loadBatches:async()=>calls.push(['load']),layout:()=>calls.push(['layout']),
back:()=>{throw Error('Cancel must not preserve and return through back()')}
}
}
test('cancelling a new purchase explicitly discards only its draft and returns without saving',async()=>{
const state=context();await cancelEditing.call(state)
assert.equal(state.sheet,null);assert.equal(state.editing,false);assert.deepEqual(state.selected,[])
assert.deepEqual(state.calls.filter(c=>c[0]==='clear'),[['clear','new']])
assert.equal(state.calls.filter(c=>c[0]==='load').length,1)
})
test('declining discard preserves the new purchase and never clears its draft',async()=>{
const state=context({confirm:false}),original=state.sheet;await cancelEditing.call(state)
assert.equal(state.sheet,original);assert.equal(state.editing,true)
assert.equal(state.calls.some(c=>c[0]==='clear'||c[0]==='load'),false)
})
test('existing purchase cancellation restores baseline; saving and absent sheets are no-ops',async()=>{
const state=context({existing:true});await cancelEditing.call(state)
assert.deepEqual(state.sheet,JSON.parse(state.baseline));assert.equal(state.editing,false)
assert.equal(state.calls.some(c=>c[0]==='load'),false)
const busy=context({saving:true});await cancelEditing.call(busy);assert.equal(busy.calls.length,0)
const empty=context();empty.sheet=null;await cancelEditing.call(empty);assert.equal(empty.calls.length,0)
})