import { screen } from '@testing-library/react';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import type { Mock } from 'vitest';
import { getHealth } from '../api/client';
import Layout from '../components/ui/Layout';
import { renderWithAuth } from './utils';
vi.mock('../api/client', () => ({
getHealth: vi.fn(),
TOKEN_KEY: 'AUTH_TOKEN',
login: vi.fn(),
register: vi.fn(),
me: vi.fn(),
logout: vi.fn(),
}));
describe('Layout', () => {
beforeEach(() => {
vi.clearAllMocks();
});
it('renders the header, main content area and footer', () => {
(getHealth as Mock).mockRejectedValue(new Error('offline'));
renderWithAuth(
Page content
,
);
expect(screen.getByRole('banner')).toBeInTheDocument();
expect(screen.getByRole('main')).toHaveTextContent('Page content');
expect(screen.getByRole('contentinfo')).toBeInTheDocument();
// Brand is reachable from the layout header.
expect(screen.getByRole('link', { name: 'newzy' })).toHaveAttribute('href', '/');
});
it('shows the API as online when the health check succeeds', async () => {
(getHealth as Mock).mockResolvedValue({ status: 'ok', version: '0.1.0' });
renderWithAuth(
content
,
);
expect(await screen.findByTestId('api-status')).toHaveTextContent(/api:\s*online/i);
expect(getHealth).toHaveBeenCalledTimes(1);
});
it('shows the API as offline when the health check fails', async () => {
(getHealth as Mock).mockRejectedValue(new Error('offline'));
renderWithAuth(
content
,
);
expect(await screen.findByTestId('api-status')).toHaveTextContent(/api:\s*offline/i);
});
});