196 lines
6.0 KiB
TypeScript
196 lines
6.0 KiB
TypeScript
import { App, router } from '@inertiajs/react';
|
|
import { renderToStaticMarkup } from 'react-dom/server';
|
|
import { afterEach, expect, test, vi } from 'vitest';
|
|
import ReceiveOrder from '@/pages/receipts/order';
|
|
import {
|
|
receiptPreparationChanged,
|
|
receiptPreparationKey,
|
|
} from '@/lib/receipt-preparation';
|
|
import type { ReceiptProgress } from '@/types/inventory';
|
|
|
|
afterEach(() => vi.restoreAllMocks());
|
|
|
|
const progress: ReceiptProgress = {
|
|
state: 'not_received',
|
|
has_divergences: false,
|
|
lines: [
|
|
{
|
|
id: 10,
|
|
description: 'Artigo sintético',
|
|
unit: 'UN',
|
|
purchase_type: 'consumable',
|
|
ordered_milli: 10000,
|
|
received_milli: 0,
|
|
accepted_milli: 0,
|
|
rejected_milli: 0,
|
|
delivery_pending_milli: 10000,
|
|
acceptance_pending_milli: 10000,
|
|
stock_item_id: 1,
|
|
},
|
|
],
|
|
};
|
|
const data = {
|
|
idempotency_key: '11111111-1111-4111-8111-111111111111',
|
|
delivery_note: 'GUIA-QA-POR-GUARDAR',
|
|
received_on: '2026-11-27',
|
|
warehouse_id: '1',
|
|
items: [
|
|
{
|
|
include: true,
|
|
purchase_order_item_id: 10,
|
|
received_quantity: '0.600',
|
|
accepted_quantity: '0.500',
|
|
quality_status: 'reservations',
|
|
notes: 'Conferência sintética por guardar',
|
|
stock_item_id: '1',
|
|
conversion: {
|
|
numerator: '',
|
|
denominator: '',
|
|
source: '',
|
|
version: '',
|
|
},
|
|
lots: [],
|
|
},
|
|
],
|
|
};
|
|
|
|
function render(
|
|
tenant = 3,
|
|
actor = 7,
|
|
order = 42,
|
|
source = progress,
|
|
canConfirm = true,
|
|
) {
|
|
return renderToStaticMarkup(
|
|
<App
|
|
initialComponent={ReceiveOrder}
|
|
initialPage={{
|
|
component: 'receipts/order',
|
|
url: `/ordens/${order}/recepcoes`,
|
|
version: null,
|
|
clearHistory: false,
|
|
encryptHistory: true,
|
|
props: {
|
|
errors: {},
|
|
auth: { user: { id: actor, tenant_id: tenant } },
|
|
order: { id: order, number: 'OC-QA-42' },
|
|
supplier_name: 'Fornecedor sintético',
|
|
progress: source,
|
|
receipts: [],
|
|
warehouses: [
|
|
{ id: 1, code: 'ARM-QA', name: 'Armazém sintético' },
|
|
],
|
|
stockItems: [
|
|
{
|
|
id: 1,
|
|
code: 'ART-QA',
|
|
name: 'Artigo sintético',
|
|
unit: 'UN',
|
|
},
|
|
],
|
|
canConfirm,
|
|
lotScopes: [],
|
|
},
|
|
}}
|
|
/>,
|
|
);
|
|
}
|
|
|
|
test('receipt preparation restores selected line quantities notes and document without an automatic receipt', () => {
|
|
vi.spyOn(router, 'restore').mockImplementation((key) =>
|
|
key === `${receiptPreparationKey(3, 7, 42, progress)}:data`
|
|
? data
|
|
: undefined,
|
|
);
|
|
const post = vi.spyOn(router, 'post');
|
|
const html = render();
|
|
expect(html).toContain('GUIA-QA-POR-GUARDAR');
|
|
expect(html).toContain('value="0.600"');
|
|
expect(html).toContain('value="0.500"');
|
|
expect(html).toContain('value="reservations" selected');
|
|
expect(html).toContain('Conferência sintética por guardar');
|
|
expect(render(3, 7, 42, progress, false)).not.toContain(
|
|
'GUIA-QA-POR-GUARDAR',
|
|
);
|
|
expect(post).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test('receipt preparation stays with its actor tenant order and physical progress', () => {
|
|
vi.spyOn(router, 'restore').mockImplementation((key) =>
|
|
key === `${receiptPreparationKey(3, 7, 42, progress)}:data`
|
|
? data
|
|
: undefined,
|
|
);
|
|
expect(render(4)).not.toContain('GUIA-QA-POR-GUARDAR');
|
|
expect(render(3, 8)).not.toContain('GUIA-QA-POR-GUARDAR');
|
|
expect(render(3, 7, 43)).not.toContain('GUIA-QA-POR-GUARDAR');
|
|
const changed = {
|
|
...progress,
|
|
state: 'partial',
|
|
has_divergences: true,
|
|
lines: [
|
|
{
|
|
...progress.lines[0],
|
|
received_milli: 600,
|
|
accepted_milli: 500,
|
|
rejected_milli: 100,
|
|
delivery_pending_milli: 9400,
|
|
acceptance_pending_milli: 9500,
|
|
},
|
|
],
|
|
};
|
|
expect(render(3, 7, 42, changed)).not.toContain('GUIA-QA-POR-GUARDAR');
|
|
expect(
|
|
receiptPreparationKey(3, 7, 42, {
|
|
...changed,
|
|
lines: [
|
|
{
|
|
...changed.lines[0],
|
|
supplier_returned_milli: 300,
|
|
effective_accepted_milli: 200,
|
|
acceptance_pending_milli: 9800,
|
|
},
|
|
],
|
|
}),
|
|
).not.toBe(receiptPreparationKey(3, 7, 42, changed));
|
|
});
|
|
|
|
test('only a regenerated receipt UUID is clean while changed quantities and lot preparation remain dirty', () => {
|
|
expect(
|
|
receiptPreparationChanged(data, {
|
|
...data,
|
|
idempotency_key: '22222222-2222-4222-8222-222222222222',
|
|
}),
|
|
).toBe(false);
|
|
expect(
|
|
receiptPreparationChanged(data, {
|
|
...data,
|
|
items: [{ ...data.items[0], accepted_quantity: '0.400' }],
|
|
}),
|
|
).toBe(true);
|
|
expect(
|
|
receiptPreparationChanged(data, {
|
|
...data,
|
|
items: [
|
|
{
|
|
...data.items[0],
|
|
lots: [
|
|
{
|
|
code: 'LOTE-QA',
|
|
quantity: '0.500',
|
|
expires_on: '',
|
|
no_expiry: true,
|
|
},
|
|
],
|
|
},
|
|
],
|
|
}),
|
|
).toBe(true);
|
|
expect(
|
|
receiptPreparationChanged(data, {
|
|
...data,
|
|
delivery_note: 'Outra guia sintética',
|
|
}),
|
|
).toBe(true);
|
|
});
|