feat(front) : util httpExternal + autocomplete adresse BAN (ERP-66)

- httpExternal : client dedie aux API publiques externes (URL absolue,
  sans cookie de session, timeout), seul point d'entree autorise pour un
  $fetch externe (regle frontend n°4).
- useAddressAutocomplete : implementation BAN (api-adresse.data.gouv.fr),
  recherche ville (type=municipality) et adresse, mapping GeoJSON, throw
  en cas d'erreur/timeout (mode degrade cote composant). La recherche
  d'adresse n'impose pas type=housenumber (sinon 0 resultat tant qu'aucun
  numero n'est saisi) — spec-front mise a jour en consequence.
- Tests Vitest : httpExternal, useAddressAutocomplete, et cas limites
  supplementaires pour formatPhoneFR.
This commit is contained in:
2026-06-03 13:29:45 +02:00
parent 1961bc62c8
commit 8376236a3c
6 changed files with 336 additions and 27 deletions
@@ -0,0 +1,56 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { httpExternal } from '../httpExternal'
// On mocke ofetch : httpExternal s'appuie sur $fetch sans jamais toucher le
// reseau pendant les tests. vi.mock est hoiste par Vitest au-dessus des imports.
const mockFetch = vi.hoisted(() => vi.fn())
vi.mock('ofetch', () => ({ $fetch: mockFetch }))
describe('httpExternal', () => {
beforeEach(() => {
mockFetch.mockReset()
})
it('retourne le JSON parse renvoye par $fetch', async () => {
mockFetch.mockResolvedValueOnce({ ok: true })
const res = await httpExternal<{ ok: boolean }>('https://example.test/api')
expect(res).toEqual({ ok: true })
})
it('transmet la query, coupe le cookie (credentials omit) et pose un timeout par defaut', async () => {
mockFetch.mockResolvedValueOnce([])
await httpExternal('https://example.test/search', {
query: { q: '80000', type: 'municipality' },
})
expect(mockFetch).toHaveBeenCalledWith(
'https://example.test/search',
expect.objectContaining({
query: { q: '80000', type: 'municipality' },
credentials: 'omit',
retry: 0,
timeout: 5000,
}),
)
})
it('permet de surcharger le timeout', async () => {
mockFetch.mockResolvedValueOnce(null)
await httpExternal('https://example.test', { timeoutMs: 1000 })
expect(mockFetch).toHaveBeenCalledWith(
'https://example.test',
expect.objectContaining({ timeout: 1000 }),
)
})
it('propage l\'erreur reseau / timeout (throw)', async () => {
mockFetch.mockRejectedValueOnce(new Error('network down'))
await expect(httpExternal('https://example.test')).rejects.toThrow('network down')
})
})