122 lines
3.5 KiB
JavaScript
122 lines
3.5 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
import { loadFrontendModule } from './helpers/frontendModule.js';
|
|
|
|
async function harness(result) {
|
|
const requests = [];
|
|
const api = await loadFrontendModule(
|
|
new URL(
|
|
'../../src/pages/DemoReservations/reservationsApi.js',
|
|
import.meta.url,
|
|
),
|
|
{
|
|
'../../helpers/websiteApi': {
|
|
requestWebsite: async (...args) => {
|
|
requests.push(args);
|
|
if (result instanceof Error) throw result;
|
|
return result;
|
|
},
|
|
},
|
|
},
|
|
);
|
|
return { ...api, requests };
|
|
}
|
|
const response = (extra = {}) => ({
|
|
page: 1,
|
|
page_size: 10,
|
|
total: 1,
|
|
items: [
|
|
{
|
|
id: 4,
|
|
name: '张晨',
|
|
contact: '+86 13800000001',
|
|
companyName: '示例科技',
|
|
position: '',
|
|
industry: '',
|
|
interest: '企业 AI 运营与治理',
|
|
requirement: '第一行\n第二行',
|
|
},
|
|
],
|
|
...extra,
|
|
});
|
|
|
|
test('uses backend pagination and preserves all business fields and line breaks', async () => {
|
|
const data = response({ page: 2, page_size: 20, total: 21 });
|
|
const api = await harness(data);
|
|
assert.equal(
|
|
await api.fetchDemoReservations({ page: 2, pageSize: 20 }),
|
|
data,
|
|
);
|
|
const [method, path, , , { params }] = api.requests[0];
|
|
assert.equal(method, 'get');
|
|
assert.equal(path, '/api/website/demo-requests');
|
|
assert.equal(params.p, 2);
|
|
assert.equal(params.page_size, 20);
|
|
assert.equal('pageNum' in params, false);
|
|
assert.equal('pageSize' in params, false);
|
|
});
|
|
|
|
test('encodes three trimmed fuzzy AND filters without losing literal plus and wildcard characters', async () => {
|
|
const api = await harness(response());
|
|
await api.fetchDemoReservations({
|
|
name: ' 张 ',
|
|
contact: ' +86 138%_!& ',
|
|
companyName: ' 测试科技 ',
|
|
});
|
|
const { params } = api.requests[0][4];
|
|
assert.deepEqual(params, {
|
|
name: '张',
|
|
contact: '+86 138%_!&',
|
|
companyName: '测试科技',
|
|
p: 1,
|
|
page_size: 10,
|
|
});
|
|
});
|
|
|
|
test('returns empty matches and out-of-range pages with the actual backend total', async () => {
|
|
for (const total of [0, 23]) {
|
|
const data = response({ items: [], total, page: 5 });
|
|
const api = await harness(data);
|
|
assert.equal(await api.fetchDemoReservations({ page: 5 }), data);
|
|
}
|
|
});
|
|
|
|
test('fails visibly on malformed responses or backend errors', async () => {
|
|
for (const value of [
|
|
null,
|
|
{},
|
|
response({ items: null }),
|
|
response({ total: -1 }),
|
|
response({ page: 0 }),
|
|
]) {
|
|
const api = await harness(value);
|
|
await assert.rejects(api.fetchDemoReservations(), /响应格式/);
|
|
}
|
|
const api = await harness(new Error('没有平台管理员权限'));
|
|
await assert.rejects(api.fetchDemoReservations(), /没有平台管理员权限/);
|
|
});
|
|
|
|
test('sends status and delete mutations through the website API', async () => {
|
|
const api = await harness(null);
|
|
await api.setDemoReservationEnabled(4, false);
|
|
await api.deleteDemoReservation(4);
|
|
assert.deepEqual(api.requests[0].slice(0, 3), [
|
|
'put',
|
|
'/api/website/demo-requests/4/status',
|
|
{ enabled: false },
|
|
]);
|
|
assert.deepEqual(api.requests[1].slice(0, 2), [
|
|
'delete',
|
|
'/api/website/demo-requests/4',
|
|
]);
|
|
});
|
|
|
|
test('passes an abort signal to the list request so refreshes bypass pending GETs', async () => {
|
|
const api = await harness(response());
|
|
const { signal } = new AbortController();
|
|
await api.fetchDemoReservations({ page: 2 }, { signal });
|
|
const options = api.requests[0][4];
|
|
assert.equal(options.signal, signal);
|
|
assert.equal(options.params.p, 2);
|
|
});
|