Files
compras/tests/frontend/requisition-history.test.tsx
T
nala db1615fac1
tests / ci (push) Waiting to run
commit melhorias compras5
2026-11-22 19:39:09 +02:00

157 lines
4.8 KiB
TypeScript

import { App, router } from '@inertiajs/react';
import { renderToStaticMarkup } from 'react-dom/server';
import { afterEach, expect, test, vi } from 'vitest';
import RequisitionForm from '@/pages/requisitions/form';
import { hasPreparationChanges } from '@/lib/requisition-history';
afterEach(() => vi.restoreAllMocks());
test('new empty classification fields preserve an older recovered preparation while real classification changes remain dirty', () => {
const old = { items: [{ description: 'Artigo anterior' }] };
const current = {
items: [
{
description: 'Artigo anterior',
procurement_category: '',
category_reason: '',
},
],
};
expect(hasPreparationChanges(old, current)).toBe(false);
expect(hasPreparationChanges(current, old)).toBe(false);
expect(
hasPreparationChanges(old, {
items: [
{
...current.items[0],
procurement_category: 'office_equipment',
},
],
}),
).toBe(true);
expect(
hasPreparationChanges(old, {
items: [
{ ...current.items[0], category_reason: 'Razão deliberada' },
],
}),
).toBe(true);
});
test('restored technical identities do not mark a cleared preparation as changed', () => {
const original = {
operation_key: 'initial-operation',
justification: '',
items: [
{
key: 'initial-row',
description: '',
quantity: '1.000',
unit_price: '',
},
],
};
const recovered = {
items: [
{
unit_price: '',
quantity: '1.000',
description: '',
key: 'recovered-row',
},
],
justification: '',
operation_key: 'recovered-operation',
};
expect(hasPreparationChanges(original, recovered)).toBe(false);
expect(
hasPreparationChanges(original, {
...recovered,
justification: 'Still unsaved',
}),
).toBe(true);
expect(
hasPreparationChanges(original, {
...recovered,
items: [{ ...recovered.items[0], quantity: '2.000' }],
}),
).toBe(true);
expect(
hasPreparationChanges(original, {
...recovered,
items: [...recovered.items, recovered.items[0]],
}),
).toBe(true);
});
const restored = {
operation_key: 'kept-operation',
process_version: null,
cost_center_id: '',
priority: 'normal',
needed_by: '',
justification: 'Preparação ao regressar',
items: [
{
key: 'kept-line',
source_line_id: '',
catalogue_entry_id: '',
catalogue_version: '',
catalogue_code: '',
description: 'Artigo ainda por gravar',
category: 'Material',
purchase_type: 'consumable',
quantity: '2.000',
unit: 'UN',
unit_price: '',
tax_rate: '16.00',
equipment_id: '',
spare_part_id: '',
production_area: '',
downtime_started_at: '',
},
],
};
function render(userId = 7, tenantId = 3) {
return renderToStaticMarkup(
<App
initialComponent={RequisitionForm}
initialPage={{
component: 'requisitions/form',
url: '/requisicoes/criar',
version: null,
clearHistory: false,
encryptHistory: true,
props: {
errors: {},
auth: { user: { id: userId, tenant_id: tenantId } },
internalRequisitionReady: true,
incompleteDraftsAvailable: true,
costCenters: [],
requisition: null,
},
}}
/>,
);
}
test('native remembered history restores unfinished fields and line values without saving a process', () => {
vi.spyOn(router, 'restore').mockImplementation((key) =>
key === 'requisition:3:7:new:0:data' ? restored : undefined,
);
const html = render();
expect(html).toContain('Preparação ao regressar');
expect(html).toContain('Artigo ainda por gravar');
expect(html).toContain('value="2.000"');
expect(html).not.toContain('value="0.00"');
});
test('remembered preparation is separated by user and company', () => {
vi.spyOn(router, 'restore').mockImplementation((key) =>
key === 'requisition:3:7:new:0:data' ? restored : undefined,
);
expect(render(8, 3)).not.toContain('Artigo ainda por gravar');
expect(render(7, 4)).not.toContain('Artigo ainda por gravar');
});