Files
Daniel Samson ffe19272b3 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
2026-10-30 14:10:29 +00:00

110 lines
3.5 KiB
TypeScript

import { expect, test, type Page } from '@playwright/test';
const HEALTH_OK = { status: 'ok', version: '0.1.0' };
const API_ARTICLE = {
id: 100,
title: 'Live API Article',
content: 'This story was served by the (mocked) newzy backend API. '.repeat(20),
author_id: 7,
category_id: 1,
created_at: '2025-06-01T08:00:00Z',
published: true,
author: {
id: 7,
name: 'Ada Lovelace',
email: 'ada@example.com',
bio: null,
},
category: { id: 1, name: 'Technology', slug: 'technology' },
};
/** Mock the health endpoint so tests do not depend on a running backend. */
async function mockHealth(page: Page): Promise<void> {
await page.route('**/api/v1/health', (route) =>
route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify(HEALTH_OK),
}),
);
}
/**
* API interactions: verifies the app talks to the backend through the
* typed client (base URL /api/v1, JSON accept header) and reflects the
* responses in the UI.
*/
test.describe('api interactions', () => {
test('requests the backend health endpoint on load and reports it online', async ({
page,
}) => {
await mockHealth(page);
const healthRequest = page.waitForRequest(
(request) => request.url().includes('/api/v1/health') && request.method() === 'GET',
);
await page.goto('/');
const request = await healthRequest;
expect(request.headers()['accept']).toBe('application/json');
await expect(page.getByTestId('api-status')).toHaveText(/api:\s*online/i);
});
test('renders articles returned by the API', async ({ page }) => {
await mockHealth(page);
await page.route('**/api/v1/articles', (route) =>
route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify([API_ARTICLE]),
}),
);
await page.goto('/');
// The API article replaces the sample feed.
const cards = page.getByTestId('article-card');
await expect(cards).toHaveCount(1);
await expect(cards.first()).toContainText('Live API Article');
await expect(cards.first()).toContainText('Ada Lovelace');
await expect(cards.first()).toContainText('Technology');
// Sample-data fallback note is hidden when real data is present.
await expect(page.getByTestId('sample-data-note')).toHaveCount(0);
});
test('falls back to sample data and reports the API as offline', async ({ page }) => {
// Block every API call to simulate the backend being down.
await page.route('**/api/v1/**', (route) => route.abort());
await page.goto('/');
await expect(page.getByTestId('sample-data-note')).toBeVisible();
await expect(page.getByTestId('article-card').first()).toBeVisible();
await expect(page.getByTestId('api-status')).toHaveText(/api:\s*offline/i);
});
test('survives API error responses (non-2xx envelope)', async ({ page }) => {
await mockHealth(page);
await page.route('**/api/v1/articles', (route) =>
route.fulfill({
status: 404,
contentType: 'application/json',
body: JSON.stringify({ detail: 'Not Found', code: 'not_found' }),
}),
);
const pageErrors: string[] = [];
page.on('pageerror', (error) => pageErrors.push(error.message));
await page.goto('/');
// 404 from the articles endpoint -> graceful sample-data fallback.
await expect(page.getByTestId('sample-data-note')).toBeVisible();
await expect(page.getByTestId('article-card').first()).toBeVisible();
expect(pageErrors).toEqual([]);
});
});