import { describe, expect, it, vi } from 'vitest'; import { getFieldLibrary, updateFieldLibrary } from './library'; describe('field library API', () => { it('loads the backend field library envelope', async () => { vi.mocked(fetch).mockResolvedValueOnce({ ok: true, status: 200, headers: new Headers({ 'content-type': 'application/json' }), json: () => Promise.resolve({ data: { library: { formFields: [], customTimeFormats: [], multiSelectOptions: {}, anesthesiaOptions: [] } } }), text: () => Promise.resolve(''), } as Response); await expect(getFieldLibrary()).resolves.toMatchObject({ formFields: [] }); }); it('patches field library changes', async () => { const fetchMock = vi.mocked(fetch); fetchMock.mockResolvedValueOnce({ ok: true, status: 200, headers: new Headers({ 'content-type': 'application/json' }), json: () => Promise.resolve({ data: { library: { formFields: [], customTimeFormats: ['HH:mm'], multiSelectOptions: {}, anesthesiaOptions: [] } } }), text: () => Promise.resolve(''), } as Response); await updateFieldLibrary({ customTimeFormats: ['HH:mm'] }); expect(fetchMock).toHaveBeenCalledWith('/api/library/fields', expect.objectContaining({ method: 'PATCH', credentials: 'include', })); }); });