48 lines
2.2 KiB
JavaScript
48 lines
2.2 KiB
JavaScript
import test from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import { readFileSync } from 'node:fs'
|
|
import ts from 'typescript'
|
|
const source = readFileSync(new URL('./transferCartonPrinting.ts', import.meta.url), 'utf8')
|
|
const js = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS } }).outputText
|
|
const exports = {}
|
|
new Function('exports', js)(exports)
|
|
const prepare = exports.prepareTransferCartonLabels
|
|
|
|
test('prints forecast counts independently of quantities or order fields and escapes cargo', () => {
|
|
const rows = [{ sku: 'A<script>', name: '货物 & 品名', expectedPackageCount: 12 }]
|
|
let next = 0
|
|
const html = prepare(rows, () => `BX-${++next}`)
|
|
assert.equal((html.match(/<section /g) || []).length, 12)
|
|
assert.match(html, /12 \/ 12/)
|
|
assert.match(html, /A<script>/)
|
|
assert.equal(rows[0].generatedCartonMarks.length, 12)
|
|
assert.equal(prepare(rows, () => { throw new Error('must retain labels') }), html)
|
|
})
|
|
|
|
test('adding boxes or rows keeps previously printed identities', () => {
|
|
const rows = [{ sku: 'A', name: '', expectedPackageCount: 2 }]
|
|
let next = 0
|
|
prepare(rows, () => `BX-${++next}`)
|
|
rows[0].expectedPackageCount = 3
|
|
rows.push({ sku: 'B', name: '', expectedPackageCount: 1 })
|
|
const html = prepare(rows, () => `BX-${++next}`)
|
|
assert.deepEqual(rows[0].generatedCartonMarks, ['BX-1', 'BX-2', 'BX-3'])
|
|
assert.match(html, /BX-4/)
|
|
})
|
|
|
|
test('invalid or excessive forecasts do not generate any labels', () => {
|
|
for (const count of [0, -1, 1.5, undefined, 5001]) {
|
|
const row = { sku: '', name: '', expectedPackageCount: count }
|
|
assert.throws(() => prepare([row]), /预计箱数/)
|
|
assert.equal(row.generatedCartonMarks, undefined)
|
|
}
|
|
})
|
|
|
|
test('print button cannot create, update or submit an order', () => {
|
|
const view = readFileSync(new URL('../views/WarehouseServiceView.vue', import.meta.url), 'utf8')
|
|
const handler = view.slice(view.indexOf('function printForecastCartons()'), view.indexOf('async function submit('))
|
|
assert.match(handler, /prepareTransferCartonLabels\(cargoRows.value\)/)
|
|
assert.doesNotMatch(handler, /createTransfer|updateTransfer|submitTransfer|getTransferCartonLabel|form\./)
|
|
assert.match(view, /cartonMarks: item.generatedCartonMarks/)
|
|
})
|