fix: request() lève une erreur claire sur réponse HTTP non-ok

This commit is contained in:
2026-07-04 01:17:06 +04:00
parent 3f30f699f7
commit 24f4f65853
2 changed files with 45 additions and 2 deletions
+37
View File
@@ -0,0 +1,37 @@
import {describe, it, expect, vi, afterEach} from 'vitest'
import {jwennAnTeks} from '../oki-api'
describe('request (via jwennAnTeks)', () => {
const originalFetch = global.fetch
afterEach(() => {
global.fetch = originalFetch
vi.restoreAllMocks()
})
it('returns the parsed body on a successful response', async () => {
global.fetch = vi.fn().mockResolvedValue({
ok: true,
json: async () => ({id: 1, titre: 'Test'})
})
const result = await jwennAnTeks(1, 'token')
expect(result).toEqual({id: 1, titre: 'Test'})
})
it('throws a clear error when Strapi returns an error response', async () => {
global.fetch = vi.fn().mockResolvedValue({
ok: false,
status: 400,
json: async () => ({data: null, error: {status: 400, name: 'ValidationError', message: 'Invalid key'}})
})
await expect(jwennAnTeks(1, 'token')).rejects.toThrow('Eshwe pu jwenn done-a')
})
it('throws when the network request itself fails', async () => {
global.fetch = vi.fn().mockRejectedValue(new TypeError('network down'))
await expect(jwennAnTeks(1, 'token')).rejects.toThrow('Eshwe pu jwenn done-a')
})
})