Files
ManagerAccount/backend/asset-export-ui.test.js
2026-11-10 11:01:24 +07:00

74 lines
5.0 KiB
JavaScript

const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
function fixture() {
const nodes = { borrowAssetIdInput: { value: '1' }, borrowAssetUserInput: { value: 'An' },
borrowAssetProjectInput: { value: 'Dự án A' }, borrowQuantityInput: { value: '2' }, borrowAssetNoteInput: { value: '' } };
const requests = [], messages = [];
const button = { disabled: false };
const sandbox = { document: { addEventListener() {}, getElementById: id => nodes[id], querySelector: () => button, querySelectorAll: () => [] },
window: {}, location: { hash: '#assets' }, console,
fetch: async (url, options) => { requests.push({ url, options }); return { ok: true, json: async () => ({ success: true, data: { RequestStatus: 'pending' } }) }; } };
sandbox.window.fetch = sandbox.fetch;
vm.runInNewContext(`${fs.readFileSync(path.join(__dirname, '../public/js/app.js'), 'utf8')}\napp = Object.create(AccountManager.prototype); globalThis.manager = app;`, sandbox);
const manager = sandbox.manager;
Object.assign(manager, { currentUser: { UserId: 1, Role: 'user', FullName: 'An' }, apiBase: '/api',
selectedAssetIds: new Set([1]), assets: [{ AssetId: 1, AssetCode: 'TS-001', Quantity: 5, ExportInPeriod: 0, EndingBalance: 5 }],
notifySuccess: message => messages.push(message), notifyFailure: message => messages.push(message), notifyWarning: message => messages.push(message),
closeModals() {} });
return { manager, sandbox, requests, messages, nodes, button };
}
test('ordinary users submit existing project allocations as pending proposals without downloading or changing stock', async () => {
const f = fixture();
await f.manager.handleBorrowAssetSubmit({ preventDefault() {} });
assert.equal(f.requests.length, 1); assert.equal(f.requests[0].url, '/api/asset-export-requests');
assert.deepEqual(JSON.parse(f.requests[0].options.body).items, [{ assetId: 1, allocations: [{ projectName: 'Dự án A', quantity: 2 }] }]);
assert.equal(f.manager.assets[0].EndingBalance, 5); assert.equal(f.manager.lastExportRequestDocument, undefined);
assert.equal(f.sandbox.location.hash, '#asset-exports'); assert.match(f.messages[0], /chờ admin\/asset duyệt/);
assert.equal(f.manager.selectedAssetIds.size, 0); assert.equal(f.button.disabled, false);
});
test('proposal forms block duplicate submissions and retain selection when the server rejects them', async () => {
const f = fixture(); let complete;
f.sandbox.fetch = (url, options) => { f.requests.push({ url, options }); return new Promise(resolve => { complete = resolve; }); };
const first = f.manager.handleBorrowAssetSubmit({ preventDefault() {} });
await f.manager.handleBorrowAssetSubmit({ preventDefault() {} });
assert.equal(f.requests.length, 1); assert.equal(f.button.disabled, true);
complete({ ok: false, json: async () => ({ success: false, message: 'Không đủ tồn' }) });
await first;
assert.equal(f.manager.selectedAssetIds.size, 1); assert.equal(f.button.disabled, false); assert.equal(f.messages[0], 'Không đủ tồn');
});
test('proposal forms reject missing projects, fractional quantities and quantities exceeding stock before submission', async () => {
for (const [field, value] of [['borrowAssetProjectInput', ''], ['borrowQuantityInput', '1.5'], ['borrowQuantityInput', '6']]) {
const f = fixture(); f.nodes[field].value = value;
await f.manager.handleBorrowAssetSubmit({ preventDefault() {} });
assert.equal(f.requests.length, 0); assert.equal(f.messages.length, 1);
}
});
test('export lists filter by creator, status and project, and show processing controls only to managers', () => {
const f = fixture();
const own = { ExportRequestId: 1, CreatedBy: 1, RequesterName: 'An', RequestStatus: 'pending', Items: [
{ assetCode: 'TS-001', assetName: '<Máy khoan>', allocations: [{ projectName: 'Dự án A', quantity: 2 }] }
] };
f.manager.assetExportRequests = [own, { ...own, ExportRequestId: 2, CreatedBy: 2 }];
f.manager.assetExportTab = 'requests';
assert.equal(f.manager.getFilteredAssetExportRows().length, 1);
f.manager.assetExportSearchTerm = 'dự án a'; assert.equal(f.manager.getFilteredAssetExportRows().length, 1);
f.manager.assetExportStatusFilter = 'rejected'; assert.equal(f.manager.getFilteredAssetExportRows().length, 0);
const userRows = f.manager.renderAssetExportRequestsRows([own]);
assert.doesNotMatch(userRows, /process-asset-export|<Máy khoan>/); assert.match(userRows, /&lt;Máy khoan&gt;/);
f.manager.currentUser.Role = 'asset'; assert.match(f.manager.renderAssetExportRequestsRows([own]), /Duyệt xuất/);
});
test('ordinary users cannot invoke the proposal decision UI', async () => {
const f = fixture();
await f.manager.processAssetExportRequest(1, 'approved', '');
assert.equal(f.requests.length, 0);
});