68 lines
3.1 KiB
JavaScript
68 lines
3.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}`);
|
||
}
|
||
|
||
assert.match(html, /<th>地址名称<\/th>/);
|
||
assert.match(html, /<label for="profile-address-name"><span class="required">\*<\/span>地址名称<\/label>/);
|
||
assert.match(html, /id="pickup-address-select"/);
|
||
assert.match(html, /提货时间[\s\S]*?选择地址/);
|
||
assert.match(html, /class="pickup-time-address-row"[\s\S]*?提货时间[\s\S]*?选择地址/);
|
||
|
||
const normalizeProfileAddress = vm.runInNewContext(`(${extractFunction('normalizeProfileAddress')})`);
|
||
const legacy = normalizeProfileAddress({id:'legacy-1', privateWarehouse:'旧地址', warehouseCode:'WH-1', country:'美国'});
|
||
assert.equal(legacy.addressName, '旧地址');
|
||
|
||
const select = { innerHTML: '' };
|
||
const renderPickupAddressOptions = vm.runInNewContext(`(${extractFunction('renderPickupAddressOptions')})`, {
|
||
document: { getElementById: id => id === 'pickup-address-select' ? select : null },
|
||
loadProfileAddresses: () => [
|
||
{id:'enabled-1', addressName:'洛杉矶仓', enabled:true},
|
||
{id:'disabled-1', addressName:'停用地址', enabled:false}
|
||
],
|
||
escapeProfileAddressHtml: value => String(value == null ? '' : value)
|
||
});
|
||
renderPickupAddressOptions();
|
||
assert.match(select.innerHTML, /value="enabled-1"[^>]*>洛杉矶仓<\/option>/);
|
||
assert.doesNotMatch(select.innerHTML, /停用地址/);
|
||
|
||
const controls = {
|
||
'pickup-address-select': { value: '' },
|
||
'pickup-contact': { value: '' },
|
||
'pickup-phone': { value: '' },
|
||
'pickup-address-detail': { value: '' }
|
||
};
|
||
const applyPickupAddress = vm.runInNewContext(`(${extractFunction('applyPickupAddress')})`, {
|
||
document: { getElementById: id => controls[id] || null },
|
||
formatPickupAddress: address => address.detailAddress || [address.stateProvince, address.city, address.country, address.postalCode, address.warehouseCode].filter(Boolean).join(','),
|
||
pickupAutoFilledValues: null,
|
||
loadProfileAddresses: () => [{
|
||
id:'address-1', addressName:'纽约仓', contact:'李雷', phone:'13800000000',
|
||
warehouseCode:'WH-NY', detailAddress:'123 Main St', city:'纽约', stateProvince:'纽约州', country:'美国', postalCode:'10001', enabled:true
|
||
}]
|
||
});
|
||
applyPickupAddress('address-1');
|
||
assert.equal(controls['pickup-contact'].value, '李雷');
|
||
assert.equal(controls['pickup-phone'].value, '13800000000');
|
||
assert.equal(controls['pickup-address-detail'].value, '123 Main St');
|
||
applyPickupAddress('');
|
||
assert.equal(controls['pickup-contact'].value, '');
|
||
assert.equal(controls['pickup-phone'].value, '');
|
||
assert.equal(controls['pickup-address-detail'].value, '');
|
||
|
||
console.log('address selection: ok');
|