Files
compras/tests/frontend/capital-register.test.tsx
T
nala 9cf9764710
tests / ci (push) Waiting to run
commit compras3 2711
2026-11-27 18:56:24 +02:00

133 lines
4.3 KiB
TypeScript

import { App } from '@inertiajs/react';
import { renderToStaticMarkup } from 'react-dom/server';
import { expect, test } from 'vitest';
import CapitalRegister from '../../resources/js/pages/equipment/capital-register';
import CapitalAssignments from '../../resources/js/pages/requisitions/capital-assignments';
import CapitalAssignmentSummary from '../../resources/js/components/capital-assignment-summary';
const source = {
id: 2,
version: 3,
record_key: 'source',
state: 'archived',
reason: 'Synthetic archive reason',
data: {
type: 'project',
code: 'PRJ-QA',
name: '<script>QA</script>',
reference: 'Frozen documentary reference',
department_id: 1,
equipment_id: null,
},
};
const page = <T,>(data: T[]) => ({
data,
prev_page_url: null,
next_page_url: null,
});
const assignment = {
id: 5,
version: 1,
state: 'linked',
reason: 'Synthetic deliberate assignment',
data: { requisition_item_id: 1 },
snapshot: { item: { description: 'Synthetic item' }, source },
};
function render(
component: typeof CapitalRegister | typeof CapitalAssignments,
props: object,
) {
return renderToStaticMarkup(
<App
initialComponent={component}
initialPage={{
component: 'qa',
url: '/qa',
version: null,
clearHistory: false,
encryptHistory: true,
props: { errors: {}, ...props },
}}
/>,
);
}
test('read-only capital register preserves archived identity and history without management forms', () => {
const html = render(CapitalRegister, {
records: page([source]),
history: page([source]),
departments: [],
equipment: [],
canManage: false,
actorId: 1,
tenantId: 1,
});
expect(html).toContain('Arquivado');
expect(html).toContain('Histórico do registo');
expect(html).toContain('&lt;script&gt;QA&lt;/script&gt;');
expect(html).not.toContain('<form');
});
test('approved requisitions display the preserved source independently of available new choices', () => {
const html = render(CapitalAssignments, {
requisition: { id: 1, number: 'REQ-QA', status: 'approved' },
items: [],
current: [assignment],
choices: page([]),
history: page([assignment]),
canWrite: false,
actorId: 1,
tenantId: 1,
});
expect(html).toContain('PRJ-QA');
expect(html).toContain('versão 3');
expect(html).not.toContain('<form');
const frozen = renderToStaticMarkup(
<CapitalAssignmentSummary records={{ 1: assignment }} />,
);
expect(frozen).toContain('Frozen documentary reference');
expect(frozen).toContain('&lt;script&gt;QA&lt;/script&gt;');
expect(renderToStaticMarkup(<CapitalAssignmentSummary />)).toBe('');
});
test('capital lineage distinguishes the original receipt from current returns and reversal', () => {
const html = render(CapitalRegister, {
records: page([source]),
history: page([source]),
departments: [],
equipment: [],
canManage: false,
actorId: 1,
tenantId: 1,
trace: {
orders: page([
{
id: 7,
description: 'Synthetic item',
quantity: '2.000',
unit: 'un',
order_id: 8,
order_number: 'OC-QA',
},
]),
receipts: page([
{
id: 9,
description: 'Synthetic item',
unit: 'un',
receipt_id: 10,
order_id: 8,
received_on: '2026-11-27',
quality_status: 'compliant',
accepted_quantity: '2.000',
reversed: true,
returned_quantity: '1.000',
},
]),
},
});
expect(html).toContain('Aceite no original: 2.000');
expect(html).toContain('Devolução ao fornecedor: 1.000');
expect(html).toContain('Recepção estornada');
expect(html).toContain('/ordens/8');
expect(html).not.toContain('<form');
});