fix: request() lève une erreur claire sur réponse HTTP non-ok
This commit is contained in:
@@ -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')
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user