Files
compras/tests/frontend/quotation-history.test.tsx
T
nala eb32c74c56
tests / ci (push) Waiting to run
commit compras1 2811
2026-11-28 07:11:35 +02:00

204 lines
6.7 KiB
TypeScript

import { App, router } from '@inertiajs/react';
import type { ComponentProps } from 'react';
import { renderToStaticMarkup } from 'react-dom/server';
import { afterEach, expect, test, vi } from 'vitest';
import Comparison from '@/pages/quotations/show';
import { QuotationEvaluationForm } from '@/components/quotation-evaluation-form';
afterEach(() => vi.restoreAllMocks());
const policy = {
revision: 1,
configured: true,
minimum_proposals: 3,
minimum_evaluators: 2,
weights: { price: 40, quality: 20, delivery: 15, history: 15, payment: 10 },
};
const data = {
currency: 'USD',
supplier_id: '7',
reference: 'QA-PREPARACAO-EXACTA',
delivery_days: '8',
valid_until: '2026-12-15',
payment_terms: 'Texto sintético por guardar',
no_conflict: false,
items: [
{
requisition_item_id: 10,
unit_price: '12.34',
discount_type: 'amount',
discount_value: '0.20',
},
],
};
function renderComparison(
tenant = 3,
actor = 7,
version = 4,
canCreate = true,
choicesAvailable = true,
) {
const props: ComponentProps<typeof Comparison> = {
requisition: {
id: 42,
process_version: version,
number: 'REQ-QA-42',
justification: 'Dados sintéticos',
items: [
{
id: 10,
description: 'Artigo sintético',
quantity: '1.000',
tax_rate: '16.00',
unit_price_minor: 100,
total_minor: 116,
},
],
},
quotations: [],
suppliers: choicesAvailable
? [{ id: 7, name: 'Fornecedor sintético' }]
: [],
canCreate,
canEvaluate: false,
ready: false,
topIds: [],
comparisonPolicy: policy,
exchangeAvailable: true,
exchangeRates: choicesAvailable
? [
{
id: 2,
currency: 'USD',
rate: '60.0000000000',
quoted_on: '2026-11-27',
source: 'Fonte sintética',
recorded_by: 9,
},
]
: [],
};
return renderToStaticMarkup(
<App
initialComponent={Comparison}
initialPage={{
component: 'quotations/show',
url: '/requisicoes/42/cotacoes',
version: null,
clearHistory: false,
encryptHistory: true,
props: {
errors: {},
auth: { user: { id: actor, tenant_id: tenant } },
...props,
},
}}
/>,
);
}
test('quotation preparation restores original currency prices discounts and text without saving or opening an unauthorized form', () => {
const spy = vi.spyOn(router, 'post');
vi.spyOn(router, 'restore').mockImplementation((key) =>
key === 'quotation-preparation:3:7:42:4:adding'
? true
: key === 'quotation-preparation:3:7:42:4:data'
? data
: undefined,
);
const html = renderComparison();
expect(html).toContain('QA-PREPARACAO-EXACTA');
expect(html).toContain('Texto sintético por guardar');
expect(html).toContain('value="12.34"');
expect(html).toContain('value="0.20"');
expect(html).toContain('value="USD" selected');
expect(renderComparison(3, 7, 4, false)).not.toContain(
'QA-PREPARACAO-EXACTA',
);
expect(spy).not.toHaveBeenCalled();
});
test('quotation preparation never crosses actor company or source version', () => {
vi.spyOn(router, 'restore').mockImplementation((key) =>
key === 'quotation-preparation:3:7:42:4:adding'
? true
: key === 'quotation-preparation:3:7:42:4:data'
? data
: undefined,
);
expect(renderComparison(3, 8)).not.toContain('QA-PREPARACAO-EXACTA');
expect(renderComparison(4, 7)).not.toContain('QA-PREPARACAO-EXACTA');
expect(renderComparison(3, 7, 5)).not.toContain('QA-PREPARACAO-EXACTA');
});
test('a remembered unavailable supplier or currency remains explicit without inventing eligibility or an exchange rate', () => {
vi.spyOn(router, 'restore').mockImplementation((key) =>
key === 'quotation-preparation:3:7:42:4:adding'
? true
: key === 'quotation-preparation:3:7:42:4:data'
? data
: undefined,
);
const html = renderComparison(3, 7, 4, true, false);
expect(html).toContain('value="USD" selected');
expect(html).toContain('taxa indisponível nesta consulta');
expect(html).toContain('Fornecedor preservado · confirme a elegibilidade');
expect(html).toContain('QA-PREPARACAO-EXACTA');
expect(html).not.toContain('taxa 0 MZN');
});
function renderEvaluation(quotationId: number, actor = 7, tenant = 3) {
function Screen() {
return (
<QuotationEvaluationForm
requisitionId={42}
quotationId={quotationId}
/>
);
}
return renderToStaticMarkup(
<App
initialComponent={Screen}
initialPage={{
component: 'quotations/show',
url: '/requisicoes/42/cotacoes',
version: null,
clearHistory: false,
encryptHistory: true,
props: {
errors: {},
auth: { user: { id: actor, tenant_id: tenant } },
},
}}
/>,
);
}
test('each independent proposal evaluation recovers only its own remembered preparation', () => {
vi.spyOn(router, 'restore').mockImplementation((key) =>
key === 'quotation-evaluation:3:7:42:11:data'
? {
quality: '8.25',
history: '6',
payment: '7',
comment: 'Fundamento sintético por guardar da proposta onze',
no_conflict: false,
}
: undefined,
);
expect(renderEvaluation(11)).toContain(
'Fundamento sintético por guardar da proposta onze',
);
expect(renderEvaluation(11)).toContain('value="8.25"');
expect(renderEvaluation(12)).not.toContain(
'Fundamento sintético por guardar da proposta onze',
);
expect(renderEvaluation(11, 8)).not.toContain(
'Fundamento sintético por guardar da proposta onze',
);
expect(renderEvaluation(11, 7, 4)).not.toContain(
'Fundamento sintético por guardar da proposta onze',
);
});