Files
compras/tests/frontend/company-preparation.test.tsx
T
nala eb32c74c56
tests / ci (push) Waiting to run
commit compras1 2811
2026-11-28 07:11:35 +02:00

304 lines
10 KiB
TypeScript

import { App, router } from '@inertiajs/react';
import { renderToStaticMarkup } from 'react-dom/server';
import { afterEach, expect, test, vi } from 'vitest';
import Setup from '@/pages/company/setup';
import Team from '@/pages/company/index';
import Parameters from '@/pages/company/parameters';
afterEach(() => vi.restoreAllMocks());
const base = 'procurement-preparations:4:7:procurement.internal:admin';
const root = `${base}:company-parameters:1:true:true`;
const comparison = {
revision: 1,
configured: true,
minimum_proposals: 3,
minimum_evaluators: 2,
weights: { price: 40, quality: 20, delivery: 15, history: 15, payment: 10 },
};
const profile = {
id: 2,
name: 'Original finance profile',
permissions: ['finance'],
work_contexts: [],
};
const parameterProps = {
companyDetails: null,
companyDetailsReady: false,
ready: true,
active: true,
version: 1,
permissions: {
admin: 'Administração',
finance: 'Finanças',
requester: 'Requisitante',
},
profiles: [profile],
memberships: [],
users: [
{
id: 8,
name: 'Existing QA',
email: 'existing@example.test',
department_id: 1,
},
],
departments: [{ id: 1, name: 'Department QA' }],
costCenters: [{ id: 1, code: 'CC-QA', name: 'Centre QA' }],
companyRules: {
first_limit: '100.00',
second_limit: '1000.00',
includes_tax: true,
provisional: false,
default_tax_rate: '16',
},
comparisonPolicy: comparison,
};
function render(
kind: 'setup' | 'team' | 'parameters',
overrides = {},
actor = 7,
tenant: number | null = 4,
context = 'procurement.internal',
) {
return renderToStaticMarkup(
<App
initialComponent={
kind === 'setup' ? Setup : kind === 'team' ? Team : Parameters
}
initialPage={{
component: `company/${kind}`,
url: '/empresa',
version: null,
clearHistory: false,
encryptHistory: true,
props: {
errors: {},
auth: { user: { id: actor, tenant_id: tenant } },
workContext: { active: context },
organizationAccess: { permissions: ['admin'] },
...(kind === 'parameters'
? parameterProps
: {
company: {
id: 4,
name: 'Company QA',
first_limit_minor: 10000,
second_limit_minor: 100000,
approval_includes_tax: true,
rules_provisional: false,
},
departments: [{ id: 1, name: 'Department QA' }],
costCenters: [],
users: [],
roles: [
{ value: 'requester', label: 'Requisitante' },
],
}),
...overrides,
},
}}
/>,
);
}
test.each([
['department', { name: 'DEPARTMENT-PREP-QA' }, 'DEPARTMENT-PREP-QA'],
[
'center',
{ department_id: '1', code: 'CENTER-PREP-QA', name: 'Centre' },
'CENTER-PREP-QA',
],
[
'user',
{
version: 1,
name: 'USER-PREP-QA',
email: 'prepared@example.test',
department_id: '1',
profile_ids: [2],
reason: 'Synthetic account proposal',
},
'USER-PREP-QA',
],
[
'rules',
{
version: 1,
first_limit: '100.00',
second_limit: '1000.00',
includes_tax: false,
reason: 'RULES-PREP-QA',
},
'RULES-PREP-QA',
],
[
'tax',
{ version: 1, default_tax_rate: '16', reason: 'TAX-PREP-QA' },
'TAX-PREP-QA',
],
[
'comparison',
{
version: 1,
minimum_proposals: 3,
minimum_evaluators: 2,
weights: comparison.weights,
reason: 'COMPARISON-PREP-QA',
},
'COMPARISON-PREP-QA',
],
] as const)(
'company %s preparation is isolated by actor company context and exact configuration version without a command',
(suffix, data, marker) => {
vi.spyOn(router, 'restore').mockImplementation((k) =>
k === `${root}:${suffix}:data` ? data : undefined,
);
const post = vi.spyOn(router, 'post');
expect(render('parameters')).toContain(marker);
expect(render('parameters')).toContain('data-preparation-dirty="true"');
expect(render('parameters', {}, 9)).not.toContain(marker);
expect(render('parameters', {}, 7, 5)).not.toContain(marker);
expect(render('parameters', {}, 7, 4, 'finance')).not.toContain(marker);
expect(render('parameters', { version: 2 })).not.toContain(marker);
expect(post).not.toHaveBeenCalled();
},
);
test('profile preparation follows selected source while the original profile remains unchanged', () => {
vi.spyOn(router, 'restore').mockImplementation((k) =>
k === `${root}:selected-profile`
? '2'
: k === `${root}:profile:2:data`
? {
version: 1,
id: '2',
name: 'PROFILE-PREP-QA',
permissions: ['finance'],
work_contexts: [],
}
: undefined,
);
expect(render('parameters')).toContain('PROFILE-PREP-QA');
expect(render('parameters')).toContain('Original finance profile');
expect(render('parameters', { version: 2 })).not.toContain(
'PROFILE-PREP-QA',
);
expect(render('parameters', { profiles: [] })).toContain(
'Perfil seleccionado indisponível',
);
});
test('access assignment preparation follows its person and native version without granting access', () => {
vi.spyOn(router, 'restore').mockImplementation((k) =>
k === `${base}:company-assignment:8:1:data`
? {
version: 1,
user_id: 8,
profile_ids: [2],
maintenance: false,
expires_at: '',
reason: 'ASSIGNMENT-PREP-QA',
}
: undefined,
);
const post = vi.spyOn(router, 'post');
expect(render('parameters')).toContain('ASSIGNMENT-PREP-QA');
expect(render('parameters', { version: 2 })).not.toContain(
'ASSIGNMENT-PREP-QA',
);
expect(render('parameters', { users: [] })).not.toContain(
'ASSIGNMENT-PREP-QA',
);
expect(post).not.toHaveBeenCalled();
});
test('activation preparation remains explicit and does not activate the company on restore', () => {
vi.spyOn(router, 'restore').mockImplementation((k) =>
k === `${base}:company-parameters:1:false:true:activate:data`
? { version: 1, confirm: true, reason: 'ACTIVATION-PREP-QA' }
: undefined,
);
const post = vi.spyOn(router, 'post');
expect(render('parameters', { active: false })).toContain(
'ACTIVATION-PREP-QA',
);
expect(render('parameters', { active: false, version: 2 })).not.toContain(
'ACTIVATION-PREP-QA',
);
expect(post).not.toHaveBeenCalled();
});
test('bootstrap restores company metadata for the exact account and leaves passwords empty', () => {
const key =
'procurement-preparations:null:7:procurement.internal:admin:company-setup';
vi.spyOn(router, 'restore').mockImplementation((k) =>
k === `${key}:data`
? {
name: 'SETUP-PREP-QA',
department: 'Department',
second_name: 'Second QA',
second_email: 'second@example.test',
first_limit: '100.00',
second_limit: '1000.00',
includes_tax: true,
}
: undefined,
);
const post = vi.spyOn(router, 'post');
const html = render('setup', {}, 7, null);
expect(html).toContain('SETUP-PREP-QA');
expect(html.match(/type="password"[^>]*value=""/g)).toHaveLength(2);
expect(render('setup', {}, 9, null)).not.toContain('SETUP-PREP-QA');
expect(post).not.toHaveBeenCalled();
});
test('legacy team account metadata is private without restoring a password', () => {
vi.spyOn(router, 'restore').mockImplementation((k) =>
k === `${base}:company-team:4:user:data`
? {
name: 'TEAM-PREP-QA',
email: 'team@example.test',
department_id: '1',
purchasing_role: 'requester',
}
: undefined,
);
const html = render('team');
expect(html).toContain('TEAM-PREP-QA');
expect(html.match(/type="password"[^>]*value=""/g)).toHaveLength(2);
expect(render('team', {}, 9)).not.toContain('TEAM-PREP-QA');
});
test('omitted non-remembered password fields do not make clean metadata dirty', () => {
vi.spyOn(router, 'restore').mockImplementation((k) =>
k === `${root}:user:data`
? {
version: 1,
name: '',
email: '',
department_id: '',
profile_ids: [],
reason: '',
}
: k === `${base}:company-team:4:user:data`
? {
name: '',
email: '',
department_id: '',
purchasing_role: 'requester',
}
: k ===
'procurement-preparations:null:7:procurement.internal:admin:company-setup:data'
? {
name: '',
department: '',
second_name: '',
second_email: '',
first_limit: '',
second_limit: '',
includes_tax: true,
}
: undefined,
);
const parameters = render('parameters');
expect(parameters).not.toContain('data-preparation-dirty="true"');
expect(parameters.match(/type="password"[^>]*value=""/g)).toHaveLength(2);
expect(render('team')).not.toContain('data-preparation-dirty="true"');
expect(render('setup', {}, 7, null)).not.toContain(
'data-preparation-dirty="true"',
);
});