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

145 lines
5.3 KiB
TypeScript

import { App, router } from '@inertiajs/react';
import { renderToStaticMarkup } from 'react-dom/server';
import { afterEach, expect, test, vi } from 'vitest';
import { QuotationExchangePanel } from '@/components/quotation-exchange-panel';
import Cancellation from '@/components/order-cancellation-request';
afterEach(() => vi.restoreAllMocks());
const identity = 'procurement-preparations:4:7:finance:finance';
function render(
exchange: boolean,
overrides = {},
actor = 7,
tenant = 4,
context = 'finance',
) {
return renderToStaticMarkup(
<App
initialComponent={exchange ? QuotationExchangePanel : Cancellation}
initialPage={{
component: 'test',
url: '/test',
version: null,
clearHistory: false,
encryptHistory: true,
props: {
errors: {},
auth: { user: { id: actor, tenant_id: tenant } },
workContext: { active: context },
organizationAccess: { permissions: ['finance'] },
requisitionId: 3,
processVersion: 2,
rates: [],
canRecord: true,
orderId: 11,
state: { enabled: true, state: 'issued', canRequest: true },
...overrides,
},
}}
/>,
);
}
test('documentary FX preparation follows process version and capability without freezing a rate', () => {
vi.spyOn(router, 'restore').mockImplementation((k) =>
k === `${identity}:quotation-exchange:3:2:true:data`
? {
operation_id: 'saved-qa-operation',
currency: 'USD',
rate: '60.1234567890',
quoted_on: '2026-11-28',
source: 'FX-PREPARATION-QA',
}
: undefined,
);
const post = vi.spyOn(router, 'post');
expect(render(true)).toContain('FX-PREPARATION-QA');
expect(render(true)).toContain('data-preparation-dirty="true"');
expect(render(true, {}, 8)).not.toContain('FX-PREPARATION-QA');
expect(render(true, {}, 7, 5)).not.toContain('FX-PREPARATION-QA');
expect(render(true, {}, 7, 4, 'procurement.internal')).not.toContain(
'FX-PREPARATION-QA',
);
expect(render(true, { requisitionId: 4 })).not.toContain(
'FX-PREPARATION-QA',
);
expect(render(true, { processVersion: 3 })).not.toContain(
'FX-PREPARATION-QA',
);
expect(render(true, { canRecord: false })).not.toContain(
'FX-PREPARATION-QA',
);
expect(
render(true, {
rates: [
{
id: 8,
currency: 'EUR',
rate: '70',
quoted_on: '2026-11-28',
source: 'Synthetic prior basis',
recorded_by: 9,
},
],
}),
).toContain('FX-PREPARATION-QA');
expect(post).not.toHaveBeenCalled();
});
test('cancellation request preparation is isolated by order and current native eligibility', () => {
vi.spyOn(router, 'restore').mockImplementation((k) =>
k === `${identity}:order-cancellation:11:true:issued:true:data`
? {
operation_key: 'saved-qa-operation',
reason: 'CANCELLATION-PREPARATION-QA',
evidence: 'Synthetic documentary evidence',
}
: undefined,
);
const post = vi.spyOn(router, 'post');
expect(render(false)).toContain('CANCELLATION-PREPARATION-QA');
expect(render(false)).toContain('data-preparation-dirty="true"');
expect(render(false, {}, 8)).not.toContain('CANCELLATION-PREPARATION-QA');
expect(render(false, {}, 7, 5)).not.toContain(
'CANCELLATION-PREPARATION-QA',
);
expect(render(false, {}, 7, 4, 'warehouse')).not.toContain(
'CANCELLATION-PREPARATION-QA',
);
expect(render(false, { orderId: 12 })).not.toContain(
'CANCELLATION-PREPARATION-QA',
);
for (const state of [
{ enabled: true, state: 'issued', canRequest: false },
{ enabled: false, state: 'issued', canRequest: false },
{ enabled: true, state: 'cancellation_pending', canRequest: false },
]) {
expect(render(false, { state })).not.toContain(
'CANCELLATION-PREPARATION-QA',
);
}
expect(post).not.toHaveBeenCalled();
});
test.each([true, false])(
'restored UUID alone stays clean for documentary command exchange=%s',
(exchange) => {
vi.spyOn(router, 'restore').mockImplementation((k) =>
k === `${identity}:quotation-exchange:3:2:true:data`
? {
operation_id: 'saved-qa-operation',
currency: '',
rate: '',
quoted_on: '',
source: '',
}
: k ===
`${identity}:order-cancellation:11:true:issued:true:data`
? {
operation_key: 'saved-qa-operation',
reason: '',
evidence: '',
}
: undefined,
);
expect(render(exchange)).toContain('data-preparation-dirty="false"');
},
);