test: increase frontend admin service coverage
Some checks failed
Tests / Backend - Tests skipped: 8, passed: 74
Backend Coverage #### Project Overview
No changes detected, that affect the code coverage.
* Line Coverage: 78.38% (551/703)
* Lines of Code: 703
#### Quality Gates Summary
No active quality gates.
Tests / Frontend - Lint, Tests et Build src/test/fonctionnel/Accueil.test.jsx.Accueil - dates du jeu-concours > affiche les dates fixées dans le tableau de bord admin failed
THE-TIP-TOP-MULTIBRANCH/pipeline/head There was a failure building this commit
Some checks failed
Tests / Backend - Tests skipped: 8, passed: 74
Backend Coverage #### Project Overview
No changes detected, that affect the code coverage.
* Line Coverage: 78.38% (551/703)
* Lines of Code: 703
#### Quality Gates Summary
No active quality gates.
Tests / Frontend - Lint, Tests et Build src/test/fonctionnel/Accueil.test.jsx.Accueil - dates du jeu-concours > affiche les dates fixées dans le tableau de bord admin failed
THE-TIP-TOP-MULTIBRANCH/pipeline/head There was a failure building this commit
This commit is contained in:
161
frontend/src/test/unit/admin.service.test.js
Normal file
161
frontend/src/test/unit/admin.service.test.js
Normal file
@@ -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,
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user