157 lines
5.9 KiB
TypeScript
157 lines
5.9 KiB
TypeScript
import { App, router } from '@inertiajs/react';
|
|
import { renderToStaticMarkup } from 'react-dom/server';
|
|
import { afterEach, expect, test, vi } from 'vitest';
|
|
import Payments from '@/pages/payments/index';
|
|
|
|
afterEach(() => vi.restoreAllMocks());
|
|
const base = 'payments:4:7:finance:finance';
|
|
const identity = `${base}:true:all:all`;
|
|
const invoice = {
|
|
id: 3,
|
|
number: 'INV-QA',
|
|
supplier_id: 1,
|
|
supplier_name: 'Synthetic supplier',
|
|
total_minor: 10000,
|
|
outstanding_minor: 10000,
|
|
currency: 'MZN',
|
|
bank_id: 5,
|
|
bank_revision: 2,
|
|
bank_state: 'approved',
|
|
banks: [
|
|
{
|
|
id: 5,
|
|
revision: 2,
|
|
label: 'Synthetic bank',
|
|
currency: 'MZN',
|
|
state: 'approved',
|
|
},
|
|
],
|
|
};
|
|
function render(overrides = {}, actor = 7, tenant = 4, context = 'finance') {
|
|
return renderToStaticMarkup(
|
|
<App
|
|
initialComponent={Payments}
|
|
initialPage={{
|
|
component: 'payments/index',
|
|
url: '/pagamentos',
|
|
version: null,
|
|
clearHistory: false,
|
|
encryptHistory: true,
|
|
props: {
|
|
errors: {},
|
|
auth: { user: { id: actor, tenant_id: tenant } },
|
|
workContext: { active: context },
|
|
organizationAccess: { permissions: ['finance'] },
|
|
canManage: true,
|
|
invoices: [invoice],
|
|
payments: {
|
|
data: [
|
|
{
|
|
id: 9,
|
|
invoice_id: 3,
|
|
created_by: 8,
|
|
status: 'pending',
|
|
amount_minor: 4000,
|
|
currency: 'MZN',
|
|
paid_on: '2026-11-28',
|
|
external_reference: null,
|
|
evidence_reference: null,
|
|
supplier_bank_detail_id: 5,
|
|
canReview: true,
|
|
},
|
|
],
|
|
prev_page_url: null,
|
|
next_page_url: null,
|
|
},
|
|
...overrides,
|
|
},
|
|
}}
|
|
/>,
|
|
);
|
|
}
|
|
test('external payment preparation restores its selected invoice without carrying values to another source or bank version', () => {
|
|
vi.spyOn(router, 'restore').mockImplementation((key) => {
|
|
if (key === `${identity}:invoice`) return '3';
|
|
if (
|
|
key ===
|
|
`${identity}:registration:3:5:2:approved:10000:5.2.approved:data`
|
|
)
|
|
return {
|
|
supplier_bank_detail_id: 5,
|
|
amount: '40.00',
|
|
paid_on: '2026-11-28',
|
|
external_reference: 'EXTERNAL-DRAFT-QA',
|
|
evidence_reference: 'Synthetic documentary evidence',
|
|
};
|
|
return undefined;
|
|
});
|
|
const post = vi.spyOn(router, 'post');
|
|
const html = render();
|
|
expect(html).toContain('EXTERNAL-DRAFT-QA');
|
|
expect(html).toContain('value="3" selected=""');
|
|
expect(html).toContain('form="external-payment-registration"');
|
|
expect(html).toContain('<form id="external-payment-registration"');
|
|
expect(html).toContain('data-preparation-dirty="true"');
|
|
expect(render({}, 8)).not.toContain('EXTERNAL-DRAFT-QA');
|
|
expect(render({}, 7, 5)).not.toContain('EXTERNAL-DRAFT-QA');
|
|
expect(render({}, 7, 4, 'procurement.internal')).not.toContain(
|
|
'EXTERNAL-DRAFT-QA',
|
|
);
|
|
expect(render({ invoices: [{ ...invoice, id: 4 }] })).not.toContain(
|
|
'EXTERNAL-DRAFT-QA',
|
|
);
|
|
expect(
|
|
render({ invoices: [{ ...invoice, bank_revision: 3 }] }),
|
|
).not.toContain('EXTERNAL-DRAFT-QA');
|
|
expect(
|
|
render({ invoices: [{ ...invoice, outstanding_minor: 6000 }] }),
|
|
).not.toContain('EXTERNAL-DRAFT-QA');
|
|
expect(render({ canManage: false })).not.toContain('EXTERNAL-DRAFT-QA');
|
|
expect(post).not.toHaveBeenCalled();
|
|
});
|
|
test('independent review restores only its pending payment and remains separate from registration', () => {
|
|
vi.spyOn(router, 'restore').mockImplementation((key) =>
|
|
key === `${base}:review:9:pending:4000:5:true:data`
|
|
? {
|
|
decision: 'rejected',
|
|
reason: 'REVIEW-DRAFT-QA',
|
|
independently_verified: true,
|
|
}
|
|
: undefined,
|
|
);
|
|
const post = vi.spyOn(router, 'post');
|
|
expect(render()).toContain('REVIEW-DRAFT-QA');
|
|
expect(render()).toContain('data-preparation-dirty="true"');
|
|
expect(render({}, 8)).not.toContain('REVIEW-DRAFT-QA');
|
|
expect(render({}, 7, 5)).not.toContain('REVIEW-DRAFT-QA');
|
|
expect(render({}, 7, 4, 'procurement.internal')).not.toContain(
|
|
'REVIEW-DRAFT-QA',
|
|
);
|
|
expect(
|
|
render({
|
|
payments: { data: [], prev_page_url: null, next_page_url: null },
|
|
}),
|
|
).not.toContain('REVIEW-DRAFT-QA');
|
|
expect(post).not.toHaveBeenCalled();
|
|
});
|
|
test('missing or settled remembered invoice is explicit and cannot enable registration', () => {
|
|
vi.spyOn(router, 'restore').mockImplementation((key) =>
|
|
key === `${identity}:invoice` ? '3' : undefined,
|
|
);
|
|
for (const invoices of [[], [{ ...invoice, outstanding_minor: 0 }]]) {
|
|
const html = render({ invoices });
|
|
expect(html).toContain(
|
|
'Factura seleccionada indisponível ou sem saldo',
|
|
);
|
|
expect(html).toMatch(
|
|
/<button[^>]*disabled=""[^>]*>Registar para conferência independente/,
|
|
);
|
|
}
|
|
});
|
|
test('unmodified payment forms remain clean without sending a command', () => {
|
|
vi.spyOn(router, 'restore').mockReturnValue(undefined);
|
|
const post = vi.spyOn(router, 'post');
|
|
expect(render()).not.toContain('data-preparation-dirty="true"');
|
|
expect(post).not.toHaveBeenCalled();
|
|
});
|