Files
docs/悦/官网头程下单/tests/storage-transfer-associated-fields.test.js
T
2026-09-08 18:17:00 +08:00

111 lines
4.1 KiB
JavaScript

const assert = require('node:assert/strict');
const fs = require('node:fs');
const vm = require('node:vm');
const html = fs.readFileSync('workbench.html', 'utf8');
function extractFunction(name) {
const start = html.indexOf(`function ${name}(`);
assert.notEqual(start, -1, `${name} should exist`);
const bodyStart = html.indexOf('{', start);
let depth = 0;
for (let index = bodyStart; index < html.length; index += 1) {
if (html[index] === '{') depth += 1;
if (html[index] === '}') depth -= 1;
if (depth === 0) return html.slice(start, index + 1);
}
throw new Error(`Unable to extract ${name}`);
}
const createPage = html.match(/<div class="page-content" id="page-storage-transfer-create">([\s\S]*?)<!-- ========== PAGE: 仓储转运单详情/)?.[1] || '';
const editModal = html.match(/<div class="modal-overlay" id="storage-transfer-edit-modal"([\s\S]*?)<!-- ========== MODAL:/)?.[1] || '';
assert.match(
createPage,
/换标要求[\s\S]*关联文件模板[\s\S]*上传箱唛[\s\S]*上传 SKU 标签[\s\S]*上传关联文件[\s\S]*备注/,
'create order should place the relation template and associated fields inside relabel settings'
);
assert.match(
editModal,
/换标要求[\s\S]*关联文件模板[\s\S]*上传箱唛[\s\S]*上传 SKU 标签[\s\S]*上传关联文件[\s\S]*备注/,
'edit order should expose the same associated fields as create order'
);
assert.match(
createPage,
/class="storage-transfer-upload-card" id="storage-transfer-relation-upload-card"/,
'create relation upload should use the same background card as carton upload'
);
assert.match(
editModal,
/class="storage-transfer-upload-card" id="storage-transfer-edit-relation-upload-card"/,
'edit relation upload should use the same background card as carton upload'
);
const getStorageTransferRelationTemplateRows = vm.runInNewContext(
`(${extractFunction('getStorageTransferRelationTemplateRows')})`
);
assert.deepEqual(
JSON.parse(JSON.stringify(getStorageTransferRelationTemplateRows())),
[['箱唛', '需贴箱唛', '需贴SKU标', '备注']]
);
let writtenWorkbook = null;
let writtenFileName = '';
const downloadStorageTransferRelationTemplate = vm.runInNewContext(
`(${extractFunction('downloadStorageTransferRelationTemplate')})`,
{
getStorageTransferRelationTemplateRows,
showToast: () => {},
XLSX: {
utils: {
aoa_to_sheet: rows => ({ rows }),
book_new: () => ({ sheets: [] }),
book_append_sheet: (workbook, worksheet, name) => workbook.sheets.push({ worksheet, name })
},
writeFile: (workbook, fileName) => {
writtenWorkbook = workbook;
writtenFileName = fileName;
}
}
}
);
downloadStorageTransferRelationTemplate();
assert.equal(writtenFileName, '仓储转运关联模板.xlsx');
assert.deepEqual(
JSON.parse(JSON.stringify(writtenWorkbook.sheets)),
[{ worksheet: { rows: [['箱唛', '需贴箱唛', '需贴SKU标', '备注']], '!cols': [{ wch: 24 }, { wch: 14 }, { wch: 14 }, { wch: 32 }] }, name: '关联明细' }]
);
const fileControl = name => ({ files: name ? [{ name }] : [] });
const form = {
elements: {
relabel: { checked: true },
relabelRequirement: { value: 'Case3' },
relabelCartonLabel: fileControl('carton.pdf'),
relabelSkuLabel: fileControl('sku.pdf'),
relabelRelationFile: fileControl('关联明细.xlsx'),
relabelRemark: { value: ' 优先处理 A 箱 ' }
}
};
const getStorageTransferRelabelConfig = vm.runInNewContext(`(${extractFunction('getStorageTransferRelabelConfig')})`);
assert.deepEqual(JSON.parse(JSON.stringify(getStorageTransferRelabelConfig(form))), {
relabel: 'yes',
relabelRequirement: 'Case3',
relabelCartonLabel: 'carton.pdf',
relabelSkuLabel: 'sku.pdf',
relabelRelationFile: '关联明细.xlsx',
relabelRemark: '优先处理 A 箱'
});
form.elements.relabel.checked = false;
assert.deepEqual(JSON.parse(JSON.stringify(getStorageTransferRelabelConfig(form))), {
relabel: 'no',
relabelRequirement: '',
relabelCartonLabel: '',
relabelSkuLabel: '',
relabelRelationFile: '',
relabelRemark: ''
});
console.log('storage transfer associated fields: ok');