Files
docs/悦/官网头程下单/tests/inbound-order-info.test.js
T
2026-09-08 18:17:00 +08:00

52 lines
2.9 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}`);
}
assert.match(html, /inbound-order-info-columns/);
const renderFunctionSource = html.slice(html.indexOf('function renderInboundOrderInfo('), html.indexOf('function handleInboundCustomsUpload('));
for (const label of ['入仓码', '开船时间', '到港时间', '提货方式', '提货时间', '提货联系人电话', '提货地址', '提货邮编', '提货联系人邮箱', '预计到货时间', '货物到仓时间', '订单类型', '渠道', '集装箱号', '目的仓类型', '目的仓', '邮编', '联系人', '联系人电话', '是否整柜', '商检验', '买单/退税', '总箱数', '总毛重(T.K.GW)', '总净重(T.K.NW)', '总体积(m³)', '计费毛重', '计费体积(m³)', '预估重量', '创建时间', '备注']) {
const escapedLabel = label.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
assert.match(renderFunctionSource, new RegExp(`['\"]${escapedLabel}['\"]`), `missing order info label: ${label}`);
}
const order = {
orderNo: 'RK-1', inboundNo: 'RK-1', channel: '美线快线', destinationWarehouse: 'ONT8', boxes: 4,
grossWeight: '79 kg', volume: '0.384 m³', billableWeight: '79 kg', billableVolume: '0.384 m³',
createdAt: '2026-09-02 10:47:09', remark: '-'
};
const target = { innerHTML: '' };
const context = {
document: { getElementById: id => id === 'inbound-detail-order-info' ? target : null },
escapeInboundHtml: value => String(value == null ? '' : value),
inboundDetailField: (label, value) => `<div class="inbound-detail-field"><dt>${label}</dt><dd>${value || '-'}</dd></div>`,
};
const render = vm.runInNewContext(`(${extractFunction('renderInboundOrderInfo')})`, context);
render(order);
assert.equal((target.innerHTML.match(/class="inbound-order-info-column/g) || []).length, 3);
assert.match(target.innerHTML, /<dt>入仓码<\/dt><dd>RK-1<\/dd>/);
assert.match(target.innerHTML, /<dt>订单类型<\/dt><dd>头程订单<\/dd>/);
assert.match(target.innerHTML, /<dt>渠道<\/dt><dd>美线快线<\/dd>/);
assert.match(target.innerHTML, /<dt>总箱数<\/dt><dd>4<\/dd>/);
assert.match(target.innerHTML, /<dt>创建时间<\/dt><dd>2026-09-02 10:47:09<\/dd>/);
assert.doesNotMatch(target.innerHTML, /<dt>客户<\/dt>/);
assert.doesNotMatch(renderFunctionSource, /['"]客户['"]/);
assert.doesNotMatch(renderFunctionSource, /['"]公司['"]/);
console.log('inbound order info requirements: ok');