71 lines
3.6 KiB
JavaScript
71 lines
3.6 KiB
JavaScript
import test from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import { readFileSync } from 'node:fs'
|
|
import vm from 'node:vm'
|
|
import { parse, compileScript, compileTemplate } from 'vue/compiler-sfc'
|
|
|
|
const filename = 'src/components/ReturnBoxMergeWork.vue'
|
|
const source = readFileSync(new URL('../' + filename, import.meta.url), 'utf8')
|
|
const { descriptor } = parse(source)
|
|
test('box merge component compiles both its script and template', () => {
|
|
compileScript(descriptor, { id: 'box-merge' })
|
|
assert.deepEqual(compileTemplate({ source: descriptor.template.content, filename, id: 'box-merge' }).errors, [])
|
|
})
|
|
function instance() {
|
|
const component = vm.runInNewContext(descriptor.script.content.replace(/^import .*$/gm, '').replace('export default', 'component ='), { ScanTargetInput: {}, component: null })
|
|
const work = { ...component.data(), plan: {customerId:'C1',sources:[{cartonMark:'A',items:[{skuCode:'SKU1',quantity:2},{skuCode:'SKU2',quantity:3}]},{cartonMark:'B',items:[{skuCode:'SKU1',quantity:2}]}]}, $emit(){} }
|
|
for (const [name, method] of Object.entries(component.methods)) work[name] = method.bind(work)
|
|
work.save = () => {}
|
|
return {work,component}
|
|
}
|
|
test('new plan box displays all sources without scanning and keeps independent SKU deductions', async () => {
|
|
const {work,component}=instance(); work.choose('NEW')
|
|
assert.equal(work.sources.length,2)
|
|
assert.equal(work.active,'')
|
|
await work.acceptScan('A')
|
|
work.setDeduction('A','SKU1',1); work.setDeduction('A','SKU2',2); work.setDeduction('B','SKU1',2)
|
|
work.dimensions={length:10,width:20,height:30,weight:1}
|
|
assert.equal(work.sources.length,2)
|
|
assert.equal(work.plannedItems().reduce((sum,item)=>sum+item.quantity,0),5)
|
|
assert.equal(component.computed.canComplete.call(work),true)
|
|
})
|
|
test('existing box uses another box as target and never removes target inventory', async () => {
|
|
const {work,component}=instance(); work.choose('EXISTING'); await work.scanSource('A'); await work.scanTarget('B')
|
|
assert.equal(work.target.cartonMark,'B'); assert.equal(work.sources.length,1)
|
|
work.setDeduction('A','SKU1',1)
|
|
assert.equal(work.plannedItems().length,1)
|
|
assert.equal(work.target.items[0].quantity,2)
|
|
assert.equal(component.computed.canComplete.call(work),true)
|
|
work.choose('NEW'); assert.equal(work.target,null); assert.equal(work.sources.length,2)
|
|
assert.equal(work.plannedItems().length,0)
|
|
})
|
|
|
|
test('existing source can be replaced before merging, echoing the mark and clearing old deductions and target', async () => {
|
|
const {work,component}=instance()
|
|
work.choose('EXISTING'); await work.scanSource('A'); await work.scanTarget('B')
|
|
work.setDeduction('A','SKU1',1)
|
|
assert.equal(work.sourceCode,'A')
|
|
work.sourceCode='B'
|
|
assert.equal(component.computed.canComplete.call(work),false)
|
|
await work.scanSource('B')
|
|
assert.equal(work.sources.length,1)
|
|
assert.equal(work.sources[0].cartonMark,'B')
|
|
assert.equal(work.sourceCode,'B')
|
|
assert.equal(work.target,null)
|
|
assert.equal(work.targetCode,'')
|
|
assert.equal(work.plannedItems().length,0)
|
|
await work.scanTarget('A'); work.setDeduction('B','SKU1',1)
|
|
assert.equal(component.computed.canComplete.call(work),true)
|
|
})
|
|
|
|
test('a directly scanned box survives switching between existing and new targets', async () => {
|
|
const {work}=instance()
|
|
const box=work.plan.sources[0];work.plan=null;work.initialSource=box
|
|
work.choose('EXISTING')
|
|
assert.equal(work.sources[0].cartonMark,'A');assert.equal(work.sourceCode,'A')
|
|
work.choose('NEW')
|
|
assert.equal(work.sources[0].items.length,2)
|
|
work.choose('EXISTING')
|
|
assert.equal(work.sourceCode,'A');assert.equal(work.active,'target')
|
|
})
|