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

94 lines
4.3 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 textContent(markup) {
return markup.replace(/<[^>]+>/g, '').trim();
}
function firstHeaders(pageMarkup) {
return Array.from(pageMarkup.matchAll(/<th(?:\s[^>]*)?>([\s\S]*?)<\/th>/g), match => textContent(match[1]));
}
function page(id, endMarker) {
return html.match(new RegExp(`<div class="page-content" id="${id}">([\\s\\S]*?)${endMarker}`))?.[1] || '';
}
assert.equal(firstHeaders(page('page-inbound', '<!-- ========== PAGE: 头程订单详情'))[0], '序号');
assert.equal(firstHeaders(page('page-delivery', '<!-- ========== PAGE: 异常订单列表'))[0], '序号');
assert.equal(firstHeaders(page('page-exception-orders', '<!-- ========== PAGE: 异常订单详情'))[0], '序号');
assert.equal(firstHeaders(page('page-labeling-list', '<!-- ========== PAGE: 贴标申请'))[0], '序号');
assert.equal(firstHeaders(page('page-storage-transfer-list', '<!-- ========== PAGE: 仓储转运'))[0], '序号');
assert.equal(firstHeaders(page('page-cargo-disposal-list', '<!-- ========== PAGE: 新增货物废弃'))[0], '序号');
assert.equal(firstHeaders(page('page-return-relabel-list', '<div class="page-content" id="page-return-relabel-create">'))[0], '序号');
for (const id of ['sea-procurement-orders-body', 'sea-truck-orders-body', 'sea-pallet-orders-body']) {
const table = html.match(new RegExp(`<tbody id="${id}">`))?.[0] || '';
assert.ok(table, `${id} should exist`);
}
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}`);
}
function firstCell(markup) {
return textContent(markup.match(/<td(?:\s[^>]*)?>([\s\S]*?)<\/td>/)?.[1] || '');
}
const seaBody = { innerHTML: '' };
const seaContext = {
document: {
getElementById: id => ({
'sea-procurement-orders-body': seaBody,
'sea-procurement-total': { textContent: '' },
'sea-procurement-page-size': { value: '' },
'sea-procurement-page-number': { textContent: '' },
'sea-procurement-prev': { disabled: false },
'sea-procurement-next': { disabled: false }
})[id] || null
},
seaBusinessSlug: () => 'procurement',
seaOrders: { Procurement: [{ id: 'one', orderNo: 'PO-1', fields: { '采购品类': '箱子' }, status: '处理中' }] },
seaPageState: { Procurement: { orderNo: '', customer: '', status: '' } },
seaPaginationState: { Procurement: { currentPage: 1, pageSize: 10 } },
paginateSeaOrders: (orders, currentPage, pageSize) => ({ total: orders.length, pages: 1, currentPage, pageSize, rows: orders }),
seaOrderCells: () => ['PO-1', '箱子', '-', '-', '-', '处理中'],
escapeSeaOrderHtml: value => String(value == null ? '' : value),
seaBusinessName: () => '采购'
};
const renderSeaOrders = vm.runInNewContext(`(${extractFunction('renderSeaOrders')})`, seaContext);
renderSeaOrders('Procurement');
assert.equal(firstCell(seaBody.innerHTML), '1');
const storageBody = { innerHTML: '' };
const storageControls = {
'storage-transfer-list-body': storageBody,
'storage-transfer-total': { textContent: '' },
'storage-transfer-page-number': { textContent: '' },
'storage-transfer-prev': { disabled: false },
'storage-transfer-next': { disabled: false }
};
const renderStorageTransferList = vm.runInNewContext(`(${extractFunction('renderStorageTransferList')})`, {
document: { getElementById: id => storageControls[id] || null },
storageTransferFilteredOrders: null,
loadStorageTransferOrders: () => [{ id: 'one', orderNo: 'ZY-1', inboundNo: 'RK-1', boxCount: 1, warehouse: '仓', fbaCode: 'ONT8', shipmentId: 'FBA-1', deliveryMethod: '卡车', relabel: 'no', tailOrderNo: 'LC-1', status: '待处理', createdAt: '2026-09-01' }],
STORAGE_TRANSFER_PAGE_SIZE: 10,
storageTransferCurrentPage: 1,
escapeStorageTransferHtml: value => String(value == null ? '' : value)
});
renderStorageTransferList();
assert.equal(firstCell(storageBody.innerHTML), '1');
console.log('order list index requirements: ok');