diff --git a/frontend/src/test/unit/admin.service.test.js b/frontend/src/test/unit/admin.service.test.js new file mode 100644 index 0000000..c61fd69 --- /dev/null +++ b/frontend/src/test/unit/admin.service.test.js @@ -0,0 +1,161 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest' +import { + getStatsApi, + getUsersApi, + drawWinnerApi, + getEmailingContactsApi, + getWinnerApi, + getEmployeesApi, + getEmployeeDeliveriesApi, + getDeliveryByTicketCodeApi, + createEmployeeApi, +} from '../../services/admin' + +describe('admin service', () => { + beforeEach(() => { + localStorage.clear() + localStorage.setItem('token', 'test-token') + vi.restoreAllMocks() + }) + + function mockFetch(data, ok = true, status = 200) { + vi.stubGlobal( + 'fetch', + vi.fn().mockResolvedValue({ + ok, + status, + json: vi.fn().mockResolvedValue(data), + }), + ) + } + + it('getStatsApi retourne les statistiques', async () => { + mockFetch({ users: 10 }) + + const result = await getStatsApi() + + expect(result).toEqual({ users: 10 }) + expect(fetch).toHaveBeenCalledWith( + '/api/admin/stats', + expect.objectContaining({ + headers: expect.objectContaining({ + Authorization: 'Bearer test-token', + }), + }), + ) + }) + + it('getUsersApi retourne les utilisateurs', async () => { + mockFetch({ users: [{ id: 1 }] }) + + await expect(getUsersApi()).resolves.toEqual({ + users: [{ id: 1 }], + }) + }) + + it('drawWinnerApi effectue un POST', async () => { + mockFetch({ winner: { id: 1 } }) + + const result = await drawWinnerApi() + + expect(result).toEqual({ winner: { id: 1 } }) + expect(fetch).toHaveBeenCalledWith( + '/api/admin/draw', + expect.objectContaining({ + method: 'POST', + }), + ) + }) + + it('getEmailingContactsApi retourne les contacts', async () => { + mockFetch({ contacts: ['test@example.com'] }) + + await expect(getEmailingContactsApi()).resolves.toEqual({ + contacts: ['test@example.com'], + }) + }) + + it('getWinnerApi retourne null si aucun gagnant', async () => { + mockFetch({}, false, 404) + + await expect(getWinnerApi()).resolves.toBeNull() + }) + + it('getWinnerApi retourne le gagnant', async () => { + mockFetch({ winner: { id: 5 } }) + + await expect(getWinnerApi()).resolves.toEqual({ + winner: { id: 5 }, + }) + }) + + it('getEmployeesApi retourne les employés', async () => { + mockFetch({ employees: [{ id: 10 }] }) + + await expect(getEmployeesApi()).resolves.toEqual({ + employees: [{ id: 10 }], + }) + }) + + it('getEmployeeDeliveriesApi retourne les remises', async () => { + mockFetch({ + employee: { id: 10 }, + deliveries: [], + }) + + await expect(getEmployeeDeliveriesApi(10)).resolves.toEqual({ + employee: { id: 10 }, + deliveries: [], + }) + }) + + it('getDeliveryByTicketCodeApi encode le code du ticket', async () => { + mockFetch({ delivery: { id: 1 } }) + + await getDeliveryByTicketCodeApi(' ABC 123 ') + + expect(fetch).toHaveBeenCalledWith( + '/api/admin/deliveries/ABC%20123', + expect.anything(), + ) + }) + + it('createEmployeeApi crée un employé', async () => { + mockFetch({ employee: { id: 20 } }) + + await expect( + createEmployeeApi({ + firstname: 'Jean', + lastname: 'Dupont', + email: 'jean@example.com', + }), + ).resolves.toEqual({ + employee: { id: 20 }, + }) + + expect(fetch).toHaveBeenCalledWith( + '/api/admin/employees', + expect.objectContaining({ + method: 'POST', + body: JSON.stringify({ + firstname: 'Jean', + lastname: 'Dupont', + email: 'jean@example.com', + }), + }), + ) + }) + + it('transforme les erreurs API en Error', async () => { + mockFetch( + { message: 'Erreur serveur' }, + false, + 500, + ) + + await expect(getStatsApi()).rejects.toMatchObject({ + message: 'Erreur serveur', + status: 500, + }) + }) +})