|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest' |
| 2 | +import { render, screen, waitFor } from '@testing-library/react' |
| 3 | +import { QueryClient, QueryClientProvider } from '@tanstack/react-query' |
| 4 | +import { ScheduleJobDialog } from './ScheduleJobDialog' |
| 5 | + |
| 6 | +// jsdom does not implement scrollIntoView |
| 7 | +Element.prototype.scrollIntoView = vi.fn() |
| 8 | + |
| 9 | +// Reproduces the "Maximum update depth exceeded" crash: while the prompt |
| 10 | +// templates query is loading, the hook returns `data: undefined`. The |
| 11 | +// component's `= EMPTY_TEMPLATES` fallback must stay referentially stable so |
| 12 | +// the init effect does not re-fire every render. |
| 13 | +vi.mock('@/hooks/usePromptTemplates', () => ({ |
| 14 | + usePromptTemplates: () => ({ data: undefined, isLoading: true }), |
| 15 | + useCreatePromptTemplate: () => ({ mutate: vi.fn(), isPending: false }), |
| 16 | + useUpdatePromptTemplate: () => ({ mutate: vi.fn(), isPending: false }), |
| 17 | + useDeletePromptTemplate: () => ({ mutate: vi.fn(), isPending: false }), |
| 18 | +})) |
| 19 | + |
| 20 | +vi.mock('@/api/providers', () => ({ |
| 21 | + getProvidersWithModels: () => Promise.resolve([]), |
| 22 | +})) |
| 23 | + |
| 24 | +vi.mock('@/api/opencode', () => ({ |
| 25 | + createOpenCodeClient: () => ({ |
| 26 | + listAgents: () => Promise.resolve([]), |
| 27 | + getConfig: () => Promise.resolve(null), |
| 28 | + }), |
| 29 | +})) |
| 30 | + |
| 31 | +vi.mock('@/api/settings', () => ({ |
| 32 | + settingsApi: { |
| 33 | + listManagedSkills: () => Promise.resolve([]), |
| 34 | + }, |
| 35 | +})) |
| 36 | + |
| 37 | +vi.mock('@/api/repos', () => ({ |
| 38 | + listRepos: () => Promise.resolve([]), |
| 39 | + listBranches: () => Promise.resolve({ branches: [], status: { ahead: 0, behind: 0 } }), |
| 40 | +})) |
| 41 | + |
| 42 | +function createWrapper() { |
| 43 | + const queryClient = new QueryClient({ |
| 44 | + defaultOptions: { |
| 45 | + queries: { retry: false }, |
| 46 | + mutations: { retry: false }, |
| 47 | + }, |
| 48 | + }) |
| 49 | + return ({ children }: { children: React.ReactNode }) => ( |
| 50 | + <QueryClientProvider client={queryClient}>{children}</QueryClientProvider> |
| 51 | + ) |
| 52 | +} |
| 53 | + |
| 54 | +describe('ScheduleJobDialog — templates loading', () => { |
| 55 | + beforeEach(() => { |
| 56 | + vi.clearAllMocks() |
| 57 | + }) |
| 58 | + |
| 59 | + it('renders without an infinite render loop while templates are loading', async () => { |
| 60 | + render( |
| 61 | + <ScheduleJobDialog |
| 62 | + open |
| 63 | + onOpenChange={vi.fn()} |
| 64 | + showRepoSelector |
| 65 | + repoId={undefined} |
| 66 | + onRepoChange={vi.fn()} |
| 67 | + onSubmit={vi.fn()} |
| 68 | + isSaving={false} |
| 69 | + />, |
| 70 | + { wrapper: createWrapper() }, |
| 71 | + ) |
| 72 | + |
| 73 | + await waitFor(() => { |
| 74 | + expect(screen.getByText('New schedule')).toBeInTheDocument() |
| 75 | + }) |
| 76 | + }) |
| 77 | +}) |
0 commit comments