feat: add React + TypeScript frontend with Vite, routing, tests and Docker setup
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
This commit is contained in:
1 parent
cbeac5e16b
commit
ffe19272b3
45 files changed
+9843
-4
No files matched your search
@@ -0,0 +1,65 @@
|
||||
import { screen } from '@testing-library/react';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { ArticleCard, getReadTime } from '../components/articles/ArticleCard';
|
||||
import type { Article } from '../types';
|
||||
import { renderWithRouter } from './utils';
|
||||
|
||||
const article: Article = {
|
||||
id: 42,
|
||||
title: 'Test article title',
|
||||
content: 'Lorem ipsum dolor sit amet. '.repeat(20),
|
||||
authorId: 1,
|
||||
author: { id: 1, name: 'Jane Doe', email: 'jane@example.com', bio: 'Writer' },
|
||||
categoryId: 2,
|
||||
category: { id: 2, name: 'Science', slug: 'science' },
|
||||
createdAt: '2025-03-01T12:00:00Z',
|
||||
published: true,
|
||||
};
|
||||
|
||||
describe('ArticleCard', () => {
|
||||
it('renders the title, category badge, author and read time', () => {
|
||||
renderWithRouter(<ArticleCard article={article} />);
|
||||
|
||||
expect(
|
||||
screen.getByRole('heading', { level: 3, name: 'Test article title' }),
|
||||
).toBeInTheDocument();
|
||||
expect(screen.getByTestId('category-badge')).toHaveTextContent('Science');
|
||||
expect(screen.getByText('Jane Doe')).toBeInTheDocument();
|
||||
expect(screen.getByText(/min read/i)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows author initials in the avatar', () => {
|
||||
renderWithRouter(<ArticleCard article={article} />);
|
||||
expect(screen.getByTestId('author-avatar')).toHaveTextContent('JD');
|
||||
expect(screen.getByTestId('author-avatar')).toHaveAttribute('title', 'Jane Doe');
|
||||
});
|
||||
|
||||
it('links to the article detail route', () => {
|
||||
renderWithRouter(<ArticleCard article={article} />);
|
||||
|
||||
const links = screen.getAllByRole('link');
|
||||
expect(
|
||||
links.some((link) => link.getAttribute('href') === '/articles/42'),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it('falls back to an unknown author when the author is missing', () => {
|
||||
renderWithRouter(<ArticleCard article={{ ...article, author: undefined }} />);
|
||||
expect(screen.getByText('Unknown author')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('omits the category badge when no category is present', () => {
|
||||
renderWithRouter(<ArticleCard article={{ ...article, category: undefined }} />);
|
||||
expect(screen.queryByTestId('category-badge')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
describe('getReadTime', () => {
|
||||
it('returns a minimum of one minute', () => {
|
||||
expect(getReadTime('a very short piece')).toBe(1);
|
||||
});
|
||||
|
||||
it('estimates two minutes for ~400 words at 200 wpm', () => {
|
||||
expect(getReadTime('word '.repeat(400).trim())).toBe(2);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,55 @@
|
||||
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;
|
||||
@@ -0,0 +1,57 @@
|
||||
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 { renderWithRouter } from './utils';
|
||||
|
||||
vi.mock('../api/client', () => ({
|
||||
getHealth: vi.fn(),
|
||||
}));
|
||||
|
||||
describe('Layout', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it('renders the header, main content area and footer', () => {
|
||||
(getHealth as Mock).mockRejectedValue(new Error('offline'));
|
||||
|
||||
renderWithRouter(
|
||||
<Layout>
|
||||
<p>Page content</p>
|
||||
</Layout>,
|
||||
);
|
||||
|
||||
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' });
|
||||
|
||||
renderWithRouter(
|
||||
<Layout>
|
||||
<p>content</p>
|
||||
</Layout>,
|
||||
);
|
||||
|
||||
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'));
|
||||
|
||||
renderWithRouter(
|
||||
<Layout>
|
||||
<p>content</p>
|
||||
</Layout>,
|
||||
);
|
||||
|
||||
expect(await screen.findByTestId('api-status')).toHaveTextContent(/api:\s*offline/i);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,44 @@
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { AuthorAvatar, initialsOf } from '../../components/articles/AuthorAvatar';
|
||||
|
||||
describe('AuthorAvatar', () => {
|
||||
it('shows initials when no avatar image is set', () => {
|
||||
render(<AuthorAvatar author={{ name: 'Ada Lovelace' }} />);
|
||||
|
||||
const avatar = screen.getByTestId('author-avatar');
|
||||
expect(avatar).toHaveTextContent('AL');
|
||||
expect(avatar).toHaveAttribute('title', 'Ada Lovelace');
|
||||
expect(avatar).toHaveAttribute('aria-label', 'Ada Lovelace');
|
||||
});
|
||||
|
||||
it('renders the avatar image when provided', () => {
|
||||
render(
|
||||
<AuthorAvatar
|
||||
author={{ name: 'Ada Lovelace', avatar: 'https://example.com/ada.png' }}
|
||||
/>,
|
||||
);
|
||||
|
||||
const image = screen.getByRole('img', { name: 'Ada Lovelace' });
|
||||
expect(image).toHaveAttribute('src', 'https://example.com/ada.png');
|
||||
});
|
||||
|
||||
it('supports size variants', () => {
|
||||
const { rerender } = render(<AuthorAvatar author={{ name: 'Ada Lovelace' }} size="lg" />);
|
||||
expect(screen.getByTestId('author-avatar')).toHaveClass('h-12', 'w-12');
|
||||
|
||||
rerender(<AuthorAvatar author={{ name: 'Ada Lovelace' }} size="sm" />);
|
||||
expect(screen.getByTestId('author-avatar')).toHaveClass('h-6', 'w-6');
|
||||
});
|
||||
|
||||
describe('initialsOf', () => {
|
||||
it('takes the first letter of the first two words', () => {
|
||||
expect(initialsOf('Grace Hopper')).toBe('GH');
|
||||
});
|
||||
|
||||
it('handles single names and extra whitespace', () => {
|
||||
expect(initialsOf('Turing')).toBe('T');
|
||||
expect(initialsOf(' Ada Lovelace ')).toBe('AL');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,33 @@
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import CategoryBadge from '../../components/articles/CategoryBadge';
|
||||
|
||||
describe('CategoryBadge', () => {
|
||||
it('renders the category name', () => {
|
||||
render(<CategoryBadge category={{ name: 'Technology', slug: 'technology' }} />);
|
||||
expect(screen.getByTestId('category-badge')).toHaveTextContent('Technology');
|
||||
});
|
||||
|
||||
it('applies the color tone for known slugs', () => {
|
||||
render(<CategoryBadge category={{ name: 'Technology', slug: 'technology' }} />);
|
||||
expect(screen.getByTestId('category-badge')).toHaveClass('bg-blue-100');
|
||||
|
||||
render(<CategoryBadge category={{ name: 'Science', slug: 'science' }} />);
|
||||
expect(screen.getAllByTestId('category-badge')[1]).toHaveClass('bg-green-100');
|
||||
});
|
||||
|
||||
it('falls back to a neutral tone for unknown slugs', () => {
|
||||
render(<CategoryBadge category={{ name: 'Mystery', slug: 'mystery' }} />);
|
||||
expect(screen.getByTestId('category-badge')).toHaveClass('bg-slate-100');
|
||||
});
|
||||
|
||||
it('accepts extra classes via the className prop', () => {
|
||||
render(
|
||||
<CategoryBadge
|
||||
category={{ name: 'Culture', slug: 'culture' }}
|
||||
className="text-lg"
|
||||
/>,
|
||||
);
|
||||
expect(screen.getByTestId('category-badge')).toHaveClass('text-lg');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,12 @@
|
||||
/**
|
||||
* Vitest setup: registers jest-dom matchers and cleans up the DOM after
|
||||
* every test (React Testing Library auto-cleanup needs this when globals
|
||||
* are disabled).
|
||||
*/
|
||||
import '@testing-library/jest-dom/vitest';
|
||||
import { cleanup } from '@testing-library/react';
|
||||
import { afterEach } from 'vitest';
|
||||
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
});
|
||||
@@ -0,0 +1,26 @@
|
||||
import { render, type RenderOptions } from '@testing-library/react';
|
||||
import type { ReactElement } from 'react';
|
||||
import { MemoryRouter } from 'react-router-dom';
|
||||
|
||||
export interface RenderWithRouterOptions extends Omit<RenderOptions, 'wrapper'> {
|
||||
/** Initial route (path only, e.g. "/articles/1"). */
|
||||
route?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render helper that wraps the UI in a MemoryRouter so components using
|
||||
* react-router-dom (Link, NavLink, useParams) can be tested in isolation.
|
||||
*/
|
||||
export function renderWithRouter(ui: ReactElement, options: RenderWithRouterOptions = {}) {
|
||||
const { route = '/', ...renderOptions } = options;
|
||||
|
||||
return render(
|
||||
<MemoryRouter
|
||||
initialEntries={[route]}
|
||||
future={{ v7_startTransition: true, v7_relativeSplatPath: true }}
|
||||
>
|
||||
{ui}
|
||||
</MemoryRouter>,
|
||||
renderOptions,
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user