@@ -0,0 +1,137 @@
|
||||
import { App, router } from '@inertiajs/react';
|
||||
import { renderToStaticMarkup } from 'react-dom/server';
|
||||
import { afterEach, expect, test, vi } from 'vitest';
|
||||
import Budgets from '@/pages/budgets/index';
|
||||
|
||||
afterEach(() => vi.restoreAllMocks());
|
||||
const identity = 'budgets:4:7:finance:admin,finance';
|
||||
function render(
|
||||
actor = 7,
|
||||
tenant = 4,
|
||||
context = 'finance',
|
||||
permissions = ['finance', 'admin'],
|
||||
budgetId = 3,
|
||||
reviewId = 9,
|
||||
) {
|
||||
return renderToStaticMarkup(
|
||||
<App
|
||||
initialComponent={Budgets}
|
||||
initialPage={{
|
||||
component: 'budgets/index',
|
||||
url: '/orcamentos',
|
||||
version: null,
|
||||
clearHistory: false,
|
||||
encryptHistory: true,
|
||||
props: {
|
||||
errors: {},
|
||||
auth: { user: { id: actor, tenant_id: tenant } },
|
||||
workContext: { active: context },
|
||||
organizationAccess: { permissions },
|
||||
centers: [{ id: 1, code: 'CC-QA', name: 'Synthetic' }],
|
||||
budgets: [
|
||||
{
|
||||
id: budgetId,
|
||||
cost_center_id: 1,
|
||||
year: 2026,
|
||||
amount_minor: 10000,
|
||||
reserved_minor: 0,
|
||||
committed_minor: 0,
|
||||
consumed_minor: 0,
|
||||
available_minor: 10000,
|
||||
created_by: 8,
|
||||
approved_by: 8,
|
||||
reason: 'Synthetic budget',
|
||||
includes_tax: false,
|
||||
reinforced_minor: 0,
|
||||
approved_total_minor: 10000,
|
||||
kind: 'operating',
|
||||
},
|
||||
],
|
||||
reinforcements: {
|
||||
data: [
|
||||
{
|
||||
id: reviewId,
|
||||
budget_id: budgetId,
|
||||
reference: 'QA',
|
||||
amount_minor: 1000,
|
||||
reason: 'Synthetic reinforcement',
|
||||
status: 'pending',
|
||||
created_by: 99,
|
||||
reviewed_by: null,
|
||||
review_reason: null,
|
||||
},
|
||||
],
|
||||
prev_page_url: null,
|
||||
next_page_url: null,
|
||||
},
|
||||
},
|
||||
}}
|
||||
/>,
|
||||
);
|
||||
}
|
||||
|
||||
test.each([
|
||||
[
|
||||
'allocation',
|
||||
{
|
||||
cost_center_id: '1',
|
||||
year: 2026,
|
||||
amount: '123.45',
|
||||
reason: 'ALLOCATION-DRAFT-QA',
|
||||
includes_tax: '0',
|
||||
kind: 'investment',
|
||||
},
|
||||
'ALLOCATION-DRAFT-QA',
|
||||
],
|
||||
[
|
||||
'reinforcement:3',
|
||||
{
|
||||
reference: 'REF-QA',
|
||||
amount: '12.34',
|
||||
reason: 'REINFORCEMENT-DRAFT-QA',
|
||||
},
|
||||
'REINFORCEMENT-DRAFT-QA',
|
||||
],
|
||||
[
|
||||
'reinforcement-review:9',
|
||||
{ decision: 'rejected', reason: 'REVIEW-DRAFT-QA' },
|
||||
'REVIEW-DRAFT-QA',
|
||||
],
|
||||
])(
|
||||
'budget %s preparation restores only its private actor/company/context/capability scope without a command',
|
||||
(suffix, data, marker) => {
|
||||
vi.spyOn(router, 'restore').mockImplementation((key) =>
|
||||
key === `${identity}:${suffix}:data` ? data : undefined,
|
||||
);
|
||||
const post = vi.spyOn(router, 'post');
|
||||
const html = render();
|
||||
expect(html.match(new RegExp(marker, 'g'))).toHaveLength(1);
|
||||
expect(html).toContain('data-preparation-dirty="true"');
|
||||
expect(render(8)).not.toContain(marker);
|
||||
expect(render(7, 5)).not.toContain(marker);
|
||||
expect(render(7, 4, 'procurement.internal')).not.toContain(marker);
|
||||
expect(render(7, 4, 'finance', ['finance'])).not.toContain(marker);
|
||||
expect(render(7, 4, 'finance', ['admin', 'finance'])).toContain(marker);
|
||||
if (suffix.startsWith('reinforcement:'))
|
||||
expect(
|
||||
render(7, 4, 'finance', ['admin', 'finance'], 4),
|
||||
).not.toContain(marker);
|
||||
if (suffix.startsWith('reinforcement-review:'))
|
||||
expect(
|
||||
render(7, 4, 'finance', ['admin', 'finance'], 3, 10),
|
||||
).not.toContain(marker);
|
||||
expect(post).not.toHaveBeenCalled();
|
||||
},
|
||||
);
|
||||
|
||||
test('unchanged budget forms are clean and do not choose the company tax or classification decision', () => {
|
||||
vi.spyOn(router, 'restore').mockReturnValue(undefined);
|
||||
const post = vi.spyOn(router, 'post');
|
||||
const html = render();
|
||||
expect(html).not.toContain('data-preparation-dirty="true"');
|
||||
expect(html.match(/data-preparation-dirty="false"/g)).toHaveLength(3);
|
||||
expect(
|
||||
html.match(/<option value="" selected="">Decisão da empresa/g),
|
||||
).toHaveLength(2);
|
||||
expect(post).not.toHaveBeenCalled();
|
||||
});
|
||||
Reference in New Issue
Block a user