/** * Tests for the EditArticlePage (M6): create/edit form validation, * reference-list prefill in create mode, prefill in edit mode, save * flows (success navigation + error display) and the load-failure view. * The Lexical editor is stubbed; its HTML contract is exercised through * the `editor-body` textarea. */ import { fireEvent, render, screen, waitFor } from '@testing-library/react'; import { MemoryRouter, Route, Routes, useLocation } from 'react-router-dom'; import { beforeEach, describe, expect, it, vi } from 'vitest'; import { adminCreateArticle, adminGetArticle, adminListAuthors, adminListCategories, adminUpdateArticle, } from '../../api/admin'; import { ApiError } from '../../api/client'; import { EditArticlePage as EditPage } from '../../pages/EditArticlePage'; import type { AdminArticle } from '../../types/admin'; vi.mock('../../api/admin', () => ({ adminListArticles: vi.fn(), adminGetArticle: vi.fn(), adminCreateArticle: vi.fn(), adminUpdateArticle: vi.fn(), adminDeleteArticle: vi.fn(), adminListAuthors: vi.fn(), adminListCategories: vi.fn(), })); vi.mock('../../components/LexicalEditor', () => ({ LexicalEditor: ({ initialHtml, onChange, }: { initialHtml: string; onChange: (html: string) => void; placeholder?: string; }) => (
Original body
', author_id: 2, category_id: 1, author: AUTHORS[1], category: CATEGORIES[0], published: true, created_at: '2024-05-01T12:00:00Z', }; function DashboardStub() { const location = useLocation(); const notice = (location.state as { notice?: string } | null)?.notice ?? ''; return ({notice}
}Body here
' }, }); screen.getByRole('button', { name: /create article/i }).click(); await waitFor(() => expect(vi.mocked(adminCreateArticle)).toHaveBeenCalledWith({ title: 'Fresh story', content: 'Body here
', author_id: 2, category_id: 2, published: true, }), ); expect(await screen.findByTestId('dashboard-stub')).toBeInTheDocument(); expect(screen.getByTestId('dashboard-notice')).toHaveTextContent( '“Fresh story” was saved.', ); }); it('disables the create button while the request is in flight', async () => { let resolveCreate!: (value: AdminArticle) => void; vi.mocked(adminCreateArticle).mockReturnValueOnce( new PromiseBody
' }, }); screen.getByRole('button', { name: /create article/i }).click(); const button = screen.getByRole('button', { name: /create article/i }); await waitFor(() => expect(button).toBeDisabled()); resolveCreate({ ...ARTICLE, id: 99, title: 'Pending' }); expect(await screen.findByTestId('dashboard-stub')).toBeInTheDocument(); }); it('shows the backend error when the create request fails', async () => { vi.mocked(adminCreateArticle).mockRejectedValue( new ApiError(422, 'Validation failed', 'validation_error'), ); renderAt('/admin/articles/new'); await screen.findByTestId('lexical-stub'); await waitFor(() => expect(screen.getByLabelText(/^Author/)).toHaveValue('1'), ); fireEvent.change(screen.getByLabelText(/^Title/), { target: { value: 'x' } }); fireEvent.change(screen.getByTestId('editor-body'), { target: { value: 'Body
' }, }); screen.getByRole('button', { name: /create article/i }).click(); expect(await screen.findByRole('alert')).toHaveTextContent('Validation failed'); }); }); describe('EditArticlePage (edit)', () => { beforeEach(() => { vi.clearAllMocks(); vi.mocked(adminListAuthors).mockResolvedValue(AUTHORS); vi.mocked(adminListCategories).mockResolvedValue(CATEGORIES); vi.mocked(adminGetArticle).mockResolvedValue(ARTICLE); vi.mocked(adminUpdateArticle).mockResolvedValue(ARTICLE); }); it('loads and pre-fills the article, then updates it on save', async () => { renderAt('/admin/articles/5/edit'); // Loading state first. expect(screen.getByRole('status')).toHaveTextContent(/loading article/i); await screen.findByTestId('lexical-stub'); expect(screen.getByRole('heading', { level: 1 })).toHaveTextContent( 'Edit article #5', ); expect(screen.getByLabelText(/^Title/)).toHaveValue('Original headline'); expect(screen.getByLabelText(/^Author/)).toHaveValue('2'); expect(screen.getByLabelText(/^Category/)).toHaveValue('1'); expect(screen.getByLabelText(/^Published/)).toBeChecked(); expect(screen.getByTestId('editor-body')).toHaveValue('Original body
'); fireEvent.change(screen.getByLabelText(/^Title/), { target: { value: 'Retitled' }, }); screen.getByRole('button', { name: /save changes/i }).click(); await waitFor(() => expect(vi.mocked(adminUpdateArticle)).toHaveBeenCalledWith(5, { title: 'Retitled', content: 'Original body
', author_id: 2, category_id: 1, published: true, }), ); expect(await screen.findByTestId('dashboard-stub')).toBeInTheDocument(); }); it('shows the not-found view when the article cannot be loaded', async () => { vi.mocked(adminGetArticle).mockRejectedValue( new ApiError(404, 'Article not found', 'not_found'), ); renderAt('/admin/articles/999/edit'); expect(await screen.findByRole('heading', { level: 1 })).toHaveTextContent( /article not found/i, ); expect(screen.getByRole('link', { name: /back to articles/i })).toHaveAttribute( 'href', '/admin', ); }); });