You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
99 lines
2.7 KiB
JavaScript
99 lines
2.7 KiB
JavaScript
|
3 weeks ago
|
import assert from 'node:assert/strict';
|
||
|
|
import { readFile } from 'node:fs/promises';
|
||
|
|
import vm from 'node:vm';
|
||
|
|
|
||
|
|
const template = await readFile(
|
||
|
|
new URL('../resources/views/venix/admin/shop/offer_form_js.tpl', import.meta.url),
|
||
|
|
'utf8',
|
||
|
|
);
|
||
|
|
const script = template.match(
|
||
|
|
/\{literal\}\s*<script>([\s\S]*?)<\/script>\s*\{\/literal\}/,
|
||
|
|
)?.[1];
|
||
|
|
assert.ok(script, 'Offer form script was not found.');
|
||
|
|
|
||
|
|
class Element {
|
||
|
|
constructor(value = '') {
|
||
|
|
this.value = value;
|
||
|
|
this.disabled = false;
|
||
|
|
this.innerHTML = '保存销售方案';
|
||
|
|
this.textContent = '';
|
||
|
|
this.className = '';
|
||
|
|
this.listeners = new Map();
|
||
|
|
}
|
||
|
|
|
||
|
|
addEventListener(name, listener) {
|
||
|
|
this.listeners.set(name, listener);
|
||
|
|
}
|
||
|
|
|
||
|
|
getAttribute(name) {
|
||
|
|
if (name === 'data-url') return '/admin/shop/1/offers/2';
|
||
|
|
if (name === 'data-method') return 'PUT';
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
const form = new Element();
|
||
|
|
const nodes = new Map([
|
||
|
|
['offerForm', form],
|
||
|
|
['offerFormSubmit', new Element()],
|
||
|
|
['offerFormStatus', new Element()],
|
||
|
|
['offerFormToast', new Element()],
|
||
|
|
['resource_pool_id', new Element('1')],
|
||
|
|
['name', new Element('Tokyo standard')],
|
||
|
|
['price', new Element('19.90')],
|
||
|
|
['backup_price', new Element('4.25')],
|
||
|
|
['stock', new Element('-1')],
|
||
|
|
['status', new Element('1')],
|
||
|
|
['sort', new Element('0')],
|
||
|
|
['revision', new Element('3')],
|
||
|
|
]);
|
||
|
|
const requests = [];
|
||
|
|
const context = {
|
||
|
|
URLSearchParams,
|
||
|
|
document: {
|
||
|
|
getElementById: id => nodes.get(id) || null,
|
||
|
|
},
|
||
|
|
location: { href: '' },
|
||
|
|
fetch(url, options) {
|
||
|
|
requests.push({ url, options });
|
||
|
|
return Promise.resolve({
|
||
|
|
ok: true,
|
||
|
|
json: async () => ({ ret: 1, msg: 'saved', redirect: '/done' }),
|
||
|
|
});
|
||
|
|
},
|
||
|
|
setTimeout() {},
|
||
|
|
console,
|
||
|
|
};
|
||
|
|
context.window = context;
|
||
|
|
|
||
|
|
vm.runInNewContext(script, context, { filename: 'offer_form_js.tpl' });
|
||
|
|
form.listeners.get('submit')({ preventDefault() {} });
|
||
|
|
await new Promise(resolve => setImmediate(resolve));
|
||
|
|
|
||
|
|
assert.equal(requests.length, 1, 'Offer form submitted an unexpected number of requests.');
|
||
|
|
const [request] = requests;
|
||
|
|
assert.equal(request.url, '/admin/shop/1/offers/2');
|
||
|
|
assert.equal(request.options.method, 'PUT');
|
||
|
|
assert.equal(request.options.credentials, 'same-origin');
|
||
|
|
assert.equal(
|
||
|
|
request.options.headers['Content-Type'],
|
||
|
|
'application/x-www-form-urlencoded; charset=UTF-8',
|
||
|
|
);
|
||
|
|
const payload = new URLSearchParams(request.options.body);
|
||
|
|
assert.deepEqual(
|
||
|
|
Object.fromEntries(payload),
|
||
|
|
{
|
||
|
|
resource_pool_id: '1',
|
||
|
|
name: 'Tokyo standard',
|
||
|
|
price: '19.90',
|
||
|
|
backup_price: '4.25',
|
||
|
|
stock: '-1',
|
||
|
|
status: '1',
|
||
|
|
sort: '0',
|
||
|
|
revision: '3',
|
||
|
|
},
|
||
|
|
'Non-zero automatic-backup price was omitted from the administrator request.',
|
||
|
|
);
|
||
|
|
|
||
|
|
console.log('Administrator offer submit preserves the automatic-backup price.');
|