import { describe, it, expect, vi, beforeEach } from 'vitest' import { render, screen, fireEvent, waitFor } from '@testing-library/react' import { CapaOwnerNotesForm } from '@/components/capa/capa-owner-notes-form' beforeEach(() => { vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ ok: true, json: async () => ({}) })) }) describe('CapaOwnerNotesForm', () => { it('shows the saved notes and saves edits with PATCH', async () => { render() const box = screen.getByLabelText('Remarks / Notes') as HTMLTextAreaElement expect(box.value).toBe('Ramp ground back') fireEvent.change(box, { target: { value: 'Ramp ground back and coated' } }) fireEvent.click(screen.getByRole('button', { name: 'Save Notes' })) await waitFor(() => expect(screen.getByText('Saved')).toBeTruthy()) expect(fetch).toHaveBeenCalledWith('/api/capa/c9', expect.objectContaining({ method: 'PATCH', body: JSON.stringify({ owner_notes: 'Ramp ground back and coated' }), })) }) it('shows an error when the save fails', async () => { vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ ok: false, json: async () => ({}) })) render() fireEvent.click(screen.getByRole('button', { name: 'Save Notes' })) expect((await screen.findByRole('alert')).textContent).toBe('Failed to save. Please try again.') }) })