156 lines
6.2 KiB
TypeScript
156 lines
6.2 KiB
TypeScript
import { App, router } from '@inertiajs/react';
|
|
import { renderToStaticMarkup } from 'react-dom/server';
|
|
import { afterEach, expect, test, vi } from 'vitest';
|
|
import OperationalOrders from '@/pages/requisitions/operational-orders';
|
|
import Summary from '@/components/operational-order-summary';
|
|
import { procurementNavigation } from '@/lib/procurement-navigation';
|
|
|
|
afterEach(() => vi.restoreAllMocks());
|
|
const source = {
|
|
id: 5,
|
|
version: 1,
|
|
created_at: '2026-11-28T10:00:00Z',
|
|
reason: 'Synthetic source',
|
|
data: {
|
|
type: 'production',
|
|
reference: 'SOURCE-ORDER-QA',
|
|
utilisation_location: 'Synthetic line',
|
|
description: '<script>private-test</script>',
|
|
department_id: 1,
|
|
},
|
|
snapshot: {
|
|
author: { name: 'Synthetic procurement' },
|
|
department: { name: 'Synthetic department' },
|
|
policy: { data: { mode: 'procurement' } },
|
|
},
|
|
};
|
|
function render(
|
|
actor = 7,
|
|
canConfigure = true,
|
|
allowedTypes = ['production', 'maintenance'],
|
|
linked = false,
|
|
selected = false,
|
|
) {
|
|
return renderToStaticMarkup(
|
|
<App
|
|
initialComponent={OperationalOrders}
|
|
initialPage={{
|
|
component: 'requisitions/operational-orders',
|
|
url: '/ordens-operacionais',
|
|
version: null,
|
|
clearHistory: false,
|
|
encryptHistory: true,
|
|
props: {
|
|
errors: {},
|
|
auth: {
|
|
user: {
|
|
id: actor,
|
|
tenant_id: 4,
|
|
name: 'Synthetic actor',
|
|
},
|
|
},
|
|
workContext: { active: 'procurement.internal' },
|
|
organizationAccess: {
|
|
permissions: ['procurement', 'requester'],
|
|
},
|
|
policy: {
|
|
id: 1,
|
|
version: 1,
|
|
data: { mode: 'procurement' },
|
|
},
|
|
canConfigure,
|
|
selectedOrderId: selected ? source.id : null,
|
|
allowedTypes,
|
|
departments: [{ id: 1, name: 'Synthetic department' }],
|
|
orders: {
|
|
data: [source],
|
|
prev_page_url: null,
|
|
next_page_url: null,
|
|
},
|
|
requisition: linked
|
|
? { id: 3, number: 'REQ-QA', status: 'draft' }
|
|
: null,
|
|
items: linked
|
|
? [{ id: 8, description: 'Synthetic item' }]
|
|
: [],
|
|
links: [],
|
|
canLink: linked,
|
|
},
|
|
}}
|
|
/>,
|
|
);
|
|
}
|
|
|
|
test('a selected operational source is directly focusable and provides the return to the complete authorised registry', () => {
|
|
const html = render(7, false, [], false, true);
|
|
expect(html).toContain('A consultar uma ordem específica');
|
|
expect(html).toContain('href="/ordens-operacionais"');
|
|
expect(html).toContain('Ver todas as ordens disponíveis');
|
|
expect(html).toContain('id="op-order-5"');
|
|
expect(html).toContain('tabindex="-1"');
|
|
expect(html).toContain('lg:scroll-mt-72');
|
|
expect(html).not.toContain('<script>private-test</script>');
|
|
});
|
|
test('order preparation restores only its actor context and policy without automatic creation', () => {
|
|
const identity =
|
|
'procurement-preparations:4:7:procurement.internal:procurement,requester:operational-orders:1:1:production,maintenance:true:0:false';
|
|
vi.spyOn(router, 'restore').mockImplementation((key) =>
|
|
key === `${identity}:new:form:data`
|
|
? {
|
|
type: 'production',
|
|
reference: 'RESTORED-ORDER-DRAFT',
|
|
department_id: '1',
|
|
utilisation_location: 'Synthetic location',
|
|
description: 'Synthetic pending work',
|
|
reason: 'Synthetic pending reason',
|
|
policy_version: '1',
|
|
operation_key: '36cb64ea-0867-4be4-90ea-c0e114ec07b1',
|
|
}
|
|
: undefined,
|
|
);
|
|
const post = vi.spyOn(router, 'post');
|
|
const html = render();
|
|
expect(html).toContain('RESTORED-ORDER-DRAFT');
|
|
expect(html).toContain('data-preparation-dirty="true"');
|
|
expect(render(8)).not.toContain('RESTORED-ORDER-DRAFT');
|
|
expect(post).not.toHaveBeenCalled();
|
|
});
|
|
test('native capabilities control configuration creation and request linking independently', () => {
|
|
const html = render(7, false, [], true);
|
|
expect(html).not.toContain('Guardar origem das novas ordens');
|
|
expect(html).not.toContain('>Registar ordem</button>');
|
|
expect(html).toContain('Ligar ordem à linha');
|
|
expect(html).toContain('SOURCE-ORDER-QA');
|
|
expect(html).not.toContain('<script>private-test</script>');
|
|
});
|
|
test('order origin summary preserves the source responsibility and renders no empty section', () => {
|
|
expect(renderToStaticMarkup(<Summary />)).toBe('');
|
|
const html = renderToStaticMarkup(
|
|
<Summary
|
|
records={{
|
|
8: {
|
|
id: 9,
|
|
snapshot: {
|
|
item: { description: 'Synthetic item' },
|
|
source,
|
|
},
|
|
},
|
|
}}
|
|
/>,
|
|
);
|
|
expect(html).toContain('SOURCE-ORDER-QA');
|
|
expect(html).toContain('Synthetic procurement');
|
|
expect(html).toContain('Registada pelo procurement');
|
|
expect(html).not.toContain('<script>private-test</script>');
|
|
});
|
|
test('operational order navigation requires server readiness and an applicable capability', () => {
|
|
const has = (permissions: string[], ready: boolean) =>
|
|
procurementNavigation(permissions, false, undefined, false, ready)
|
|
.flatMap((group) => group.entries)
|
|
.some((entry) => entry.id === 'operational_orders');
|
|
expect(has(['production_order_prepare'], true)).toBe(true);
|
|
expect(has(['maintenance_order_prepare'], true)).toBe(true);
|
|
expect(has(['procurement'], false)).toBe(false);
|
|
expect(has(['finance'], true)).toBe(false);
|
|
});
|