Files
TradarrV2/frontend/tests/preferences-api.test.ts

25 lines
1.2 KiB
TypeScript

import { afterEach, expect, it, vi } from 'vitest'
import { preferencesApi } from '../src/api/preferences'
afterEach(() => {
vi.unstubAllGlobals()
localStorage.clear()
})
it('charge et sauvegarde les préférences du compte via GET et PUT /api/me/preferences', async () => {
localStorage.setItem('token', 'user-token')
const initial = { language: 'fr', ranking_mode: 'title', summary_prompt_token_budget: 250000 }
const updated = { language: 'en' as const, ranking_mode: 'title_content' as const, summary_prompt_token_budget: 80000 }
const fetchMock = vi.fn()
.mockResolvedValueOnce(new Response(JSON.stringify({ data: initial }), { status: 200 }))
.mockResolvedValueOnce(new Response(JSON.stringify({ data: updated }), { status: 200 }))
vi.stubGlobal('fetch', fetchMock)
expect(await preferencesApi.get()).toEqual(initial)
expect(await preferencesApi.update(updated)).toEqual(updated)
const headers = { 'Content-Type': 'application/json', Authorization: 'Bearer user-token' }
expect(fetchMock).toHaveBeenNthCalledWith(1, '/api/me/preferences', { headers })
expect(fetchMock).toHaveBeenNthCalledWith(2, '/api/me/preferences', {
method: 'PUT', headers, body: JSON.stringify(updated),
})
})