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

36 lines
1.5 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 target = { innerHTML: '' };
const context = {
document: { getElementById: id => id === 'inbound-detail-fees' ? target : null },
escapeInboundHtml: value => String(value == null ? '' : value),
};
const render = vm.runInNewContext(`(${extractFunction('renderInboundFees')})`, context);
render({ unitPrice:'$0.85/kg', billableVolume:'1.26 m³', billableWeight:'186.4 kg', totalFee:'$158.44', paymentStatus:'已付款' });
const labels = Array.from(target.innerHTML.matchAll(/<span>([^<]+)<\/span>/g), match => match[1]);
assert.deepEqual(labels, ['计费单价', '计费体积', '计费重量', '费用总计', '费用状态']);
assert.match(target.innerHTML, /<span>费用总计<\/span><strong>\$158\.44<\/strong>/);
render({});
assert.match(target.innerHTML, /<span>费用总计<\/span><strong>—<\/strong>/);
console.log('inbound fee info requirements: ok');