@@ -0,0 +1,188 @@
|
||||
import { App, router } from '@inertiajs/react';
|
||||
import { renderToStaticMarkup } from 'react-dom/server';
|
||||
import { afterEach, expect, test, vi } from 'vitest';
|
||||
import Equipment, { EquipmentDemo } from '@/pages/equipment/index';
|
||||
|
||||
afterEach(() => vi.restoreAllMocks());
|
||||
const identity = 'inventory-events:4:7:warehouse:/equipamentos:true:true:false';
|
||||
const asset = {
|
||||
id: 3,
|
||||
version: 2,
|
||||
code: 'EQ-QA',
|
||||
description: 'Original synthetic equipment',
|
||||
area: 'Production',
|
||||
serial_number: null,
|
||||
department_id: 1,
|
||||
stock_item_id: null,
|
||||
criticality_level_id: 1,
|
||||
status: 'active',
|
||||
};
|
||||
const part = {
|
||||
id: 4,
|
||||
version: 2,
|
||||
stock_item_id: 1,
|
||||
criticality_level_id: 1,
|
||||
minimum_milli: 1000,
|
||||
balance_milli: 0,
|
||||
below_minimum: true,
|
||||
};
|
||||
function render(overrides = {}, actor = 7, tenant = 4, context = 'warehouse') {
|
||||
return renderToStaticMarkup(
|
||||
<App
|
||||
initialComponent={Equipment}
|
||||
initialPage={{
|
||||
component: 'equipment/index',
|
||||
url: '/equipamentos',
|
||||
version: null,
|
||||
clearHistory: false,
|
||||
encryptHistory: true,
|
||||
props: {
|
||||
errors: {},
|
||||
auth: { user: { id: actor, tenant_id: tenant } },
|
||||
workContext: { active: context },
|
||||
equipment: [asset],
|
||||
parts: [part],
|
||||
levels: [
|
||||
{
|
||||
id: 1,
|
||||
code: 'L-QA',
|
||||
name: 'Synthetic level',
|
||||
criteria: 'Synthetic criteria',
|
||||
is_critical: true,
|
||||
},
|
||||
],
|
||||
links: [],
|
||||
items: [
|
||||
{
|
||||
id: 1,
|
||||
code: 'ITEM-QA',
|
||||
name: 'Synthetic item',
|
||||
unit: 'un',
|
||||
},
|
||||
],
|
||||
departments: [{ id: 1, name: 'Production' }],
|
||||
history: {
|
||||
data: [],
|
||||
prev_page_url: null,
|
||||
next_page_url: null,
|
||||
},
|
||||
canManage: true,
|
||||
canConfigure: true,
|
||||
...overrides,
|
||||
},
|
||||
}}
|
||||
/>,
|
||||
);
|
||||
}
|
||||
test('asset preparation follows selected immutable source and never overwrites the original catalogue display', () => {
|
||||
vi.spyOn(router, 'restore').mockImplementation((k) =>
|
||||
k === `${identity}:selected-asset`
|
||||
? '3'
|
||||
: k === `${identity}:asset:3:2:data`
|
||||
? {
|
||||
geography_reference: null,
|
||||
id: '3',
|
||||
version: '2',
|
||||
code: 'PRIVATE-ASSET-DRAFT',
|
||||
description: 'Prepared description',
|
||||
area: 'Production',
|
||||
serial_number: '',
|
||||
department_id: '1',
|
||||
stock_item_id: '',
|
||||
criticality_level_id: '1',
|
||||
status: 'stopped',
|
||||
}
|
||||
: undefined,
|
||||
);
|
||||
const post = vi.spyOn(router, 'post');
|
||||
expect(render()).toContain('PRIVATE-ASSET-DRAFT');
|
||||
expect(render()).toContain('Original synthetic equipment');
|
||||
expect(render()).toContain('data-preparation-dirty="true"');
|
||||
expect(render({ equipment: [{ ...asset, version: 3 }] })).not.toContain(
|
||||
'PRIVATE-ASSET-DRAFT',
|
||||
);
|
||||
expect(render({}, 8)).not.toContain('PRIVATE-ASSET-DRAFT');
|
||||
expect(render({}, 7, 5)).not.toContain('PRIVATE-ASSET-DRAFT');
|
||||
expect(render({}, 7, 4, 'finance')).not.toContain('PRIVATE-ASSET-DRAFT');
|
||||
expect(render({ canManage: false })).not.toContain('PRIVATE-ASSET-DRAFT');
|
||||
expect(post).not.toHaveBeenCalled();
|
||||
});
|
||||
test('part preparation restores by selected part and version independently of asset editing', () => {
|
||||
vi.spyOn(router, 'restore').mockImplementation((k) =>
|
||||
k === `${identity}:selected-part`
|
||||
? '4'
|
||||
: k === `${identity}:part:4:2:data`
|
||||
? {
|
||||
id: '4',
|
||||
version: '2',
|
||||
stock_item_id: '1',
|
||||
criticality_level_id: '1',
|
||||
minimum_quantity: '4.567',
|
||||
}
|
||||
: undefined,
|
||||
);
|
||||
expect(render()).toContain('value="4.567"');
|
||||
expect(render()).toContain('data-preparation-dirty="true"');
|
||||
expect(render({ parts: [{ ...part, version: 3 }] })).not.toContain(
|
||||
'value="4.567"',
|
||||
);
|
||||
expect(render({}, 8)).not.toContain('value="4.567"');
|
||||
});
|
||||
test('category preparation does not confer configuration rights', () => {
|
||||
vi.spyOn(router, 'restore').mockImplementation((k) =>
|
||||
k === `${identity}:category:data`
|
||||
? {
|
||||
code: 'CAT-QA',
|
||||
name: 'CATEGORY-DRAFT-QA',
|
||||
criteria: 'Synthetic criterion',
|
||||
is_critical: '1',
|
||||
}
|
||||
: undefined,
|
||||
);
|
||||
expect(render()).toContain('CATEGORY-DRAFT-QA');
|
||||
expect(render({ canConfigure: false })).not.toContain('CATEGORY-DRAFT-QA');
|
||||
});
|
||||
test('equipment part association preparation retains explicit native selections without associating them', () => {
|
||||
vi.spyOn(router, 'restore').mockImplementation((k) =>
|
||||
k === `${identity}:association:data`
|
||||
? { equipment_id: '3', spare_part_id: '4' }
|
||||
: undefined,
|
||||
);
|
||||
const post = vi.spyOn(router, 'post');
|
||||
expect(render()).toContain('<option value="3" selected=""');
|
||||
expect(render()).toContain('<option value="4" selected=""');
|
||||
expect(post).not.toHaveBeenCalled();
|
||||
});
|
||||
test('a remembered source absent from the native catalogue cannot turn into a new record implicitly', () => {
|
||||
vi.spyOn(router, 'restore').mockImplementation((k) =>
|
||||
k === `${identity}:selected-asset`
|
||||
? '99'
|
||||
: k === `${identity}:selected-part`
|
||||
? '98'
|
||||
: undefined,
|
||||
);
|
||||
const html = render();
|
||||
expect(html.match(/Ficha seleccionada indisponível/g)).toHaveLength(2);
|
||||
expect(html).not.toContain('Guardar equipamento');
|
||||
});
|
||||
test('equipment demonstration renders without private identity and unmodified private forms are clean', () => {
|
||||
vi.spyOn(router, 'restore').mockReturnValue(undefined);
|
||||
const post = vi.spyOn(router, 'post');
|
||||
expect(render()).not.toContain('data-preparation-dirty="true"');
|
||||
expect(
|
||||
renderToStaticMarkup(
|
||||
<App
|
||||
initialComponent={EquipmentDemo}
|
||||
initialPage={{
|
||||
component: 'preview/index',
|
||||
url: '/preview',
|
||||
version: null,
|
||||
clearHistory: false,
|
||||
encryptHistory: false,
|
||||
props: { errors: {} },
|
||||
}}
|
||||
/>,
|
||||
),
|
||||
).toContain('Equipamentos e peças sobressalentes');
|
||||
expect(post).not.toHaveBeenCalled();
|
||||
});
|
||||
Reference in New Issue
Block a user