import { describe, it, expect, vi, beforeEach } from 'vitest' import { useWeighingTicketReferentials } from '../useWeighingTicketReferentials' const mockApiGet = vi.hoisted(() => vi.fn()) vi.stubGlobal('useApi', () => ({ get: mockApiGet })) /** * Tests des référentiels Client/Fournisseur de l'écran ticket de pesée (M5). * Contrat couvert (ERP-208) : `load(siteId)` filtre les deux endpoints par site * courant via `siteId[]` ; sans site → listes complètes (param absent). */ describe('useWeighingTicketReferentials', () => { beforeEach(() => { mockApiGet.mockReset() mockApiGet.mockResolvedValue({ member: [] }) }) it('passe siteId[] aux deux endpoints quand un site courant est fourni', async () => { const { load } = useWeighingTicketReferentials() await load(7) const clientsCall = mockApiGet.mock.calls.find(c => c[0] === '/clients') const suppliersCall = mockApiGet.mock.calls.find(c => c[0] === '/suppliers') expect(clientsCall?.[1]).toMatchObject({ pagination: 'false', 'siteId[]': [7] }) expect(suppliersCall?.[1]).toMatchObject({ pagination: 'false', 'siteId[]': [7] }) }) it('ne passe pas siteId[] quand aucun site (liste complète)', async () => { const { load } = useWeighingTicketReferentials() await load(null) const clientsCall = mockApiGet.mock.calls.find(c => c[0] === '/clients') expect(clientsCall?.[1]).not.toHaveProperty('siteId[]') expect(clientsCall?.[1]).toMatchObject({ pagination: 'false' }) }) it('mappe les membres Hydra en options { value: @id, label: companyName }', async () => { mockApiGet.mockResolvedValue({ member: [{ '@id': '/api/clients/3', companyName: 'ACME' }] }) const { load, clients } = useWeighingTicketReferentials() await load(7) expect(clients.value).toEqual([{ value: '/api/clients/3', label: 'ACME' }]) }) })