M3 frontend infrastructure for the newzy news application: - Vite + React 18 + TypeScript project (Node >=18) in frontend/ - React Router v6: BrowserRouter in App.tsx, routes for /, /articles, /articles/:id and /categories, Link/NavLink-based navigation - lucide-react icons (Home, FileText, Settings, Search, ...) in the responsive Header (desktop + mobile menu) and article components - Reusable components: ArticleCard, AuthorAvatar, CategoryBadge (CVA), Layout shell with backend API status indicator in the footer - Typed API client (fetch wrapper, ApiError, endpoint helpers) with types matching the backend models; sample-data fallback until the backend article endpoints exist - Tailwind CSS global/component styles, cn() helper (clsx + tailwind-merge) - Unit tests: Vitest + React Testing Library (23 tests, 100% statement coverage on components) - E2E tests: Playwright (9 tests) covering page shell, feed rendering, client-side navigation, article route and API interactions - ESLint (react + react-hooks + typescript) configured and passing - Multi-stage Dockerfile (Vite build -> Nginx) with SPA fallback and an /api/ proxy to the backend service; docker-compose frontend service on port 3000 in both frontend/ and the root docker-compose.yml - README with setup, API documentation, testing and Docker instructions
56 lines
2.3 KiB
TypeScript
56 lines
2.3 KiB
TypeScript
import { fireEvent, screen, within } from '@testing-library/react';
|
|
import { describe, expect, it } from 'vitest';
|
|
import Header from '../components/ui/Header';
|
|
import { renderWithRouter } from './utils';
|
|
|
|
describe('Header', () => {
|
|
it('renders the brand with the logo', () => {
|
|
renderWithRouter(<Header />);
|
|
const brand = screen.getByRole('link', { name: 'newzy' });
|
|
expect(brand).toHaveAttribute('href', '/');
|
|
expect(brand.querySelector('img')).toHaveAttribute('src', expect.stringContaining('logo'));
|
|
});
|
|
|
|
it('renders the primary navigation with router links and lucide icons', () => {
|
|
renderWithRouter(<Header />);
|
|
|
|
const nav = screen.getByRole('navigation', { name: 'Main navigation' });
|
|
expect(within(nav).getByRole('link', { name: /home/i })).toHaveAttribute('href', '/');
|
|
expect(within(nav).getByRole('link', { name: /articles/i })).toHaveAttribute('href', '/articles');
|
|
expect(within(nav).getByRole('link', { name: /categories/i })).toHaveAttribute('href', '/categories');
|
|
|
|
// Lucide icons render as inline SVGs (one per nav item).
|
|
expect(nav.querySelectorAll('svg').length).toBeGreaterThanOrEqual(NAV_ITEM_COUNT);
|
|
});
|
|
|
|
it('marks the active route in the navigation', () => {
|
|
renderWithRouter(<Header />, { route: '/categories' });
|
|
|
|
const nav = screen.getByRole('navigation', { name: 'Main navigation' });
|
|
expect(within(nav).getByRole('link', { name: /categories/i })).toHaveClass('bg-blue-50');
|
|
expect(within(nav).getByRole('link', { name: /home/i })).not.toHaveClass('bg-blue-50');
|
|
});
|
|
|
|
it('toggles the mobile menu', () => {
|
|
renderWithRouter(<Header />);
|
|
|
|
// Mobile menu is closed by default.
|
|
expect(screen.queryByRole('navigation', { name: 'Mobile navigation' })).not.toBeInTheDocument();
|
|
|
|
fireEvent.click(screen.getByRole('button', { name: 'Open menu' }));
|
|
const mobileNav = screen.getByRole('navigation', { name: 'Mobile navigation' });
|
|
expect(mobileNav).toBeInTheDocument();
|
|
expect(within(mobileNav).getByRole('link', { name: /articles/i })).toHaveAttribute(
|
|
'href',
|
|
'/articles',
|
|
);
|
|
|
|
fireEvent.click(screen.getByRole('button', { name: 'Close menu' }));
|
|
expect(
|
|
screen.queryByRole('navigation', { name: 'Mobile navigation' }),
|
|
).not.toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
const NAV_ITEM_COUNT = 3;
|