Files
compras/tests/frontend/public-navigation.test.tsx
T
nala 47649bd46c
tests / ci (push) Canceled after 0s
commit compras1 3011
2026-11-30 09:33:36 +02:00

76 lines
3.0 KiB
TypeScript

import { readFileSync } from 'node:fs';
import { App } from '@inertiajs/react';
import type { Page } from '@inertiajs/core';
import type { ComponentType } from 'react';
import { renderToString } from 'react-dom/server';
import { expect, test } from 'vitest';
import AppLayout from '@/layouts/app-layout';
import { procurementNavigation } from '@/lib/procurement-navigation';
const fixtures = JSON.parse(
readFileSync(
new URL('../Fixtures/public-navigation.json', import.meta.url),
'utf8',
),
) as Record<string, Page>;
const modules = import.meta.glob<{
default: ComponentType<Record<string, unknown>>;
}>('../../resources/js/pages/**/*.tsx', { eager: true });
test.each(Object.entries(fixtures))(
'real public page %s renders with the actual application shell and no authenticated user',
(name, initialPage) => {
const Screen = modules[`../../resources/js/pages/${name}.tsx`]?.default;
expect(Screen).toBeDefined();
const Component = (props: Record<string, unknown>) => (
<AppLayout>
<Screen {...props} />
</AppLayout>
);
const html = renderToString(
<App initialPage={initialPage} initialComponent={Component} />,
);
expect(html).toContain('Consulta pública');
expect(html).toContain('Consulta pública sem sessão');
expect(html).toContain('Entrar na aplicação');
expect(html).toContain('href="/login"');
expect(html).not.toContain('Consulta pública de desenvolvimento');
expect(html).not.toContain('/consulta?');
expect(html).toContain('/dashboard');
},
);
test('product entry presents six areas and directs work to the application login', () => {
const Home = modules['../../resources/js/pages/home.tsx'].default;
const initialPage = { ...Object.values(fixtures)[0], component: 'home' };
const html = renderToString(
<App initialPage={initialPage} initialComponent={Home} />,
);
for (const title of [
'Pedidos e aprovações',
'Fornecedores e compras',
'Recepção e armazém',
'Orçamento e finanças',
'Importações',
'Pessoas e contratações',
]) {
expect(html).toContain(title);
}
expect(html).toContain('href="/login"');
expect(html).not.toContain('href="/consulta');
});
test('public menu links only to the explicit public screens and never grants a staff role', () => {
const paths = (
fixtures.dashboard.props.publicNavigation as { paths: string[] }
).paths;
const groups = procurementNavigation('', false, paths);
expect(groups.length).toBeGreaterThan(3);
for (const entry of groups.flatMap((group) => group.entries))
expect(paths).toContain(new URL(entry.path, 'http://local').pathname);
expect(
groups
.flatMap((group) => group.entries)
.some((entry) => entry.path.startsWith('/administracao')),
).toBe(false);
expect(fixtures.dashboard.props.auth).toEqual({ user: null });
});