From 42f1b4de384bcbbdab3b80d8e5cc53b36fcffd50 Mon Sep 17 00:00:00 2001
From: Adam Mathes <adam@adammathes.com>
Date: Thu, 12 Feb 2026 21:50:56 -0800
Subject: Implement frontend login logic with >90% coverage

---
 frontend/src/components/Login.test.tsx | 76 ++++++++++++++++++++++++++++++++++
 1 file changed, 76 insertions(+)
 create mode 100644 frontend/src/components/Login.test.tsx

(limited to 'frontend/src/components/Login.test.tsx')

diff --git a/frontend/src/components/Login.test.tsx b/frontend/src/components/Login.test.tsx
new file mode 100644
index 0000000..44d1371
--- /dev/null
+++ b/frontend/src/components/Login.test.tsx
@@ -0,0 +1,76 @@
+import { render, screen, fireEvent, waitFor } from '@testing-library/react';
+import { BrowserRouter } from 'react-router-dom';
+import { describe, it, expect, vi, beforeEach } from 'vitest';
+import Login from './Login';
+
+// Mock fetch
+global.fetch = vi.fn();
+
+const renderLogin = () => {
+    render(
+        <BrowserRouter>
+            <Login />
+        </BrowserRouter>
+    );
+};
+
+describe('Login Component', () => {
+    beforeEach(() => {
+        vi.resetAllMocks();
+    });
+
+    it('renders login form', () => {
+        renderLogin();
+        expect(screen.getByLabelText(/password/i)).toBeInTheDocument();
+        expect(screen.getByRole('button', { name: /login/i })).toBeInTheDocument();
+    });
+
+    it('handles successful login', async () => {
+        (global.fetch as any).mockResolvedValueOnce({
+            ok: true,
+        });
+
+        renderLogin();
+
+        fireEvent.change(screen.getByLabelText(/password/i), { target: { value: 'secret' } });
+        fireEvent.click(screen.getByRole('button', { name: /login/i }));
+
+        await waitFor(() => {
+            expect(global.fetch).toHaveBeenCalledWith('/api/login', expect.objectContaining({
+                method: 'POST',
+            }));
+        });
+        // Navigation assertion is tricky without mocking useNavigate, 
+        // but if no error is shown, we assume success path was taken
+        expect(screen.queryByText(/login failed/i)).not.toBeInTheDocument();
+    });
+
+    it('handles failed login', async () => {
+        (global.fetch as any).mockResolvedValueOnce({
+            ok: false,
+            json: async () => ({ message: 'Bad credentials' }),
+        });
+
+        renderLogin();
+
+        fireEvent.change(screen.getByLabelText(/password/i), { target: { value: 'wrong' } });
+        fireEvent.click(screen.getByRole('button', { name: /login/i }));
+
+        await waitFor(() => {
+            expect(screen.getByText(/bad credentials/i)).toBeInTheDocument();
+        });
+    });
+
+    it('handles network error', async () => {
+        (global.fetch as any).mockRejectedValueOnce(new Error('Network error'));
+
+        renderLogin();
+
+        fireEvent.change(screen.getByLabelText(/password/i), { target: { value: 'secret' } });
+        fireEvent.click(screen.getByRole('button', { name: /login/i }));
+
+        await waitFor(() => {
+            expect(screen.getByText(/network error/i)).toBeInTheDocument();
+        });
+    });
+});
-- 
cgit v1.2.3

