|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen, fireEvent, waitFor } from '@testing-library/react'; |
| 3 | +import useSWR from 'swr'; |
| 4 | +import { toast } from 'sonner'; |
| 5 | +import PrdPage from '@/app/prd/page'; |
| 6 | +import { discoveryApi } from '@/lib/api'; |
| 7 | +import * as storage from '@/lib/workspace-storage'; |
| 8 | + |
| 9 | +// ── Mocks ──────────────────────────────────────────────────────────────── |
| 10 | + |
| 11 | +jest.mock('swr'); |
| 12 | +jest.mock('sonner', () => ({ |
| 13 | + toast: { |
| 14 | + success: jest.fn(), |
| 15 | + error: jest.fn(), |
| 16 | + }, |
| 17 | +})); |
| 18 | + |
| 19 | +jest.mock('@/lib/workspace-storage', () => ({ |
| 20 | + getSelectedWorkspacePath: jest.fn(), |
| 21 | +})); |
| 22 | + |
| 23 | +jest.mock('@/lib/api', () => ({ |
| 24 | + prdApi: { getLatest: jest.fn(), createVersion: jest.fn() }, |
| 25 | + tasksApi: { getAll: jest.fn() }, |
| 26 | + discoveryApi: { generateTasks: jest.fn() }, |
| 27 | +})); |
| 28 | + |
| 29 | + |
| 30 | +// Stub out heavy child components |
| 31 | +jest.mock('@/components/prd', () => ({ |
| 32 | + PRDView: ({ |
| 33 | + onGenerateTasks, |
| 34 | + isGeneratingTasks, |
| 35 | + }: { |
| 36 | + onGenerateTasks: () => void; |
| 37 | + isGeneratingTasks: boolean; |
| 38 | + }) => ( |
| 39 | + <div> |
| 40 | + <button onClick={onGenerateTasks} disabled={isGeneratingTasks}> |
| 41 | + Generate Tasks |
| 42 | + </button> |
| 43 | + </div> |
| 44 | + ), |
| 45 | +})); |
| 46 | + |
| 47 | +jest.mock('@/components/prd/UploadPRDModal', () => ({ |
| 48 | + UploadPRDModal: () => null, |
| 49 | +})); |
| 50 | + |
| 51 | +jest.mock('next/link', () => { |
| 52 | + const MockLink = ({ href, children }: { href: string; children: React.ReactNode }) => ( |
| 53 | + <a href={href}>{children}</a> |
| 54 | + ); |
| 55 | + MockLink.displayName = 'MockLink'; |
| 56 | + return MockLink; |
| 57 | +}); |
| 58 | + |
| 59 | +// ── Helpers ─────────────────────────────────────────────────────────────── |
| 60 | + |
| 61 | +const mockUseSWR = useSWR as jest.MockedFunction<typeof useSWR>; |
| 62 | +const mockGetSelectedWorkspacePath = storage.getSelectedWorkspacePath as jest.MockedFunction< |
| 63 | + typeof storage.getSelectedWorkspacePath |
| 64 | +>; |
| 65 | +const mockGenerateTasks = discoveryApi.generateTasks as jest.MockedFunction< |
| 66 | + typeof discoveryApi.generateTasks |
| 67 | +>; |
| 68 | + |
| 69 | +const WORKSPACE = '/home/user/project'; |
| 70 | + |
| 71 | +const fakePrd = { |
| 72 | + id: 'prd-1', |
| 73 | + title: 'My PRD', |
| 74 | + content: '# Overview', |
| 75 | + version: 1, |
| 76 | + created_at: '2026-01-01T00:00:00Z', |
| 77 | + workspace_path: WORKSPACE, |
| 78 | +}; |
| 79 | + |
| 80 | +function setupSWR() { |
| 81 | + mockUseSWR.mockImplementation((key) => { |
| 82 | + if (typeof key === 'string' && key.includes('prd')) { |
| 83 | + return { data: fakePrd, error: undefined, isLoading: false, mutate: jest.fn() } as ReturnType<typeof useSWR>; |
| 84 | + } |
| 85 | + return { data: { tasks: [], by_status: {} }, error: undefined, isLoading: false, mutate: jest.fn() } as ReturnType<typeof useSWR>; |
| 86 | + }); |
| 87 | +} |
| 88 | + |
| 89 | +// ── Tests ───────────────────────────────────────────────────────────────── |
| 90 | + |
| 91 | +describe('PrdPage — handleGenerateTasks', () => { |
| 92 | + beforeEach(() => { |
| 93 | + jest.clearAllMocks(); |
| 94 | + mockGetSelectedWorkspacePath.mockReturnValue(WORKSPACE); |
| 95 | + setupSWR(); |
| 96 | + }); |
| 97 | + |
| 98 | + it('shows success toast with task count after generation', async () => { |
| 99 | + mockGenerateTasks.mockResolvedValueOnce({ task_count: 8, tasks: [] }); |
| 100 | + |
| 101 | + render(<PrdPage />); |
| 102 | + |
| 103 | + fireEvent.click(screen.getByRole('button', { name: /generate tasks/i })); |
| 104 | + |
| 105 | + await waitFor(() => { |
| 106 | + expect(toast.success).toHaveBeenCalledWith( |
| 107 | + 'Generated 8 tasks from PRD', |
| 108 | + expect.objectContaining({ |
| 109 | + duration: 4000, |
| 110 | + action: expect.objectContaining({ label: 'Go to Tasks →' }), |
| 111 | + }) |
| 112 | + ); |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + it('uses singular "task" when count is 1', async () => { |
| 117 | + mockGenerateTasks.mockResolvedValueOnce({ task_count: 1, tasks: [] }); |
| 118 | + |
| 119 | + render(<PrdPage />); |
| 120 | + fireEvent.click(screen.getByRole('button', { name: /generate tasks/i })); |
| 121 | + |
| 122 | + await waitFor(() => { |
| 123 | + expect(toast.success).toHaveBeenCalledWith( |
| 124 | + 'Generated 1 task from PRD', |
| 125 | + expect.anything() |
| 126 | + ); |
| 127 | + }); |
| 128 | + }); |
| 129 | + |
| 130 | + it('shows error toast with API error detail on failure', async () => { |
| 131 | + mockGenerateTasks.mockRejectedValueOnce({ detail: 'No PRD content found' }); |
| 132 | + |
| 133 | + render(<PrdPage />); |
| 134 | + fireEvent.click(screen.getByRole('button', { name: /generate tasks/i })); |
| 135 | + |
| 136 | + await waitFor(() => { |
| 137 | + expect(toast.error).toHaveBeenCalledWith('No PRD content found'); |
| 138 | + }); |
| 139 | + }); |
| 140 | + |
| 141 | + it('shows fallback error message when no detail provided', async () => { |
| 142 | + mockGenerateTasks.mockRejectedValueOnce({}); |
| 143 | + |
| 144 | + render(<PrdPage />); |
| 145 | + fireEvent.click(screen.getByRole('button', { name: /generate tasks/i })); |
| 146 | + |
| 147 | + await waitFor(() => { |
| 148 | + expect(toast.error).toHaveBeenCalledWith( |
| 149 | + 'Failed to generate tasks. Please try again.' |
| 150 | + ); |
| 151 | + }); |
| 152 | + }); |
| 153 | +}); |
0 commit comments