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.
virt/web/tests/AdminOfferSubmitBehaviorTes...

107 lines
3.0 KiB
JavaScript

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')],
['quarterly_price', new Element('56.70')],
['annual_price', new Element('199.00')],
['backup_price', new Element('4.25')],
['backup_quarterly_price', new Element('12.00')],
['backup_annual_price', new Element('42.00')],
['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',
quarterly_price: '56.70',
annual_price: '199.00',
backup_price: '4.25',
backup_quarterly_price: '12.00',
backup_annual_price: '42.00',
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 every instance and automatic-backup cycle price.');