98 lines
4.4 KiB
JavaScript
98 lines
4.4 KiB
JavaScript
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
import { render, screen, within } from '@testing-library/react'
|
|
import userEvent from '@testing-library/user-event'
|
|
import EmployeeDeliveries from '../../components/admin/EmployeeDeliveries'
|
|
import { getEmployeesApi, getEmployeeDeliveriesApi, getDeliveryByTicketCodeApi } from '../../services/admin'
|
|
|
|
vi.mock('../../services/admin', () => ({
|
|
getEmployeesApi: vi.fn(),
|
|
getEmployeeDeliveriesApi: vi.fn(),
|
|
getDeliveryByTicketCodeApi: vi.fn(),
|
|
}))
|
|
|
|
const MARC = { id: 10, firstname: 'Marc', lastname: 'Durand', email: 'marc@example.com', createdAt: '2026-09-01T00:00:00Z', mustChangePassword: false, deliveredCount: 1 }
|
|
const SOPHIE = { id: 11, firstname: 'Sophie', lastname: 'Bernard', email: 'sophie@example.com', createdAt: '2026-09-02T00:00:00Z', mustChangePassword: true, deliveredCount: 0 }
|
|
|
|
function renderComponent() {
|
|
return render(<EmployeeDeliveries refreshKey={0} onAuthError={() => false} />)
|
|
}
|
|
|
|
describe('EmployeeDeliveries', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
getEmployeesApi.mockResolvedValue({ employees: [MARC, SOPHIE] })
|
|
})
|
|
|
|
it('liste les employés et filtre par nom', async () => {
|
|
renderComponent()
|
|
|
|
expect(await screen.findByRole('button', { name: 'Marc Durand' })).toBeInTheDocument()
|
|
expect(screen.getByText('Première connexion en attente')).toBeInTheDocument()
|
|
|
|
await userEvent.type(screen.getByLabelText(/rechercher un employé/i), 'sophie')
|
|
|
|
expect(screen.queryByRole('button', { name: 'Marc Durand' })).not.toBeInTheDocument()
|
|
expect(screen.getByRole('button', { name: 'Sophie Bernard' })).toBeInTheDocument()
|
|
})
|
|
|
|
it("affiche les lots remis quand on clique sur le nom d'un employé", async () => {
|
|
getEmployeeDeliveriesApi.mockResolvedValue({
|
|
employee: MARC,
|
|
deliveries: [{
|
|
claimId: 1,
|
|
ticketCode: 'A1B2C3D4E5',
|
|
lot: { name: 'Infuseur à thé', value: 39 },
|
|
client: { id: 5, firstname: 'Chloé', lastname: 'Martin', email: 'chloe@example.com' },
|
|
deliveredAt: '2026-09-10T14:30:00Z',
|
|
}],
|
|
})
|
|
renderComponent()
|
|
|
|
await userEvent.click(await screen.findByRole('button', { name: 'Marc Durand' }))
|
|
|
|
expect(getEmployeeDeliveriesApi).toHaveBeenCalledWith(10)
|
|
expect(await screen.findByText('A1B2C3D4E5')).toBeInTheDocument()
|
|
expect(screen.getByText(/lots remis par marc durand/i)).toBeInTheDocument()
|
|
expect(screen.getByText('Infuseur à thé')).toBeInTheDocument()
|
|
})
|
|
|
|
it("retrouve l'employé et la date de remise à partir du numéro de ticket", async () => {
|
|
getDeliveryByTicketCodeApi.mockResolvedValue({
|
|
ticketCode: 'A1B2C3D4E5',
|
|
lot: { name: 'Infuseur à thé', value: 39 },
|
|
status: 'DELIVERED',
|
|
playedAt: '2026-09-01T10:00:00Z',
|
|
client: { id: 5, firstname: 'Chloé', lastname: 'Martin', email: 'chloe@example.com' },
|
|
deliveredBy: { id: 10, firstname: 'Marc', lastname: 'Durand', email: 'marc@example.com' },
|
|
deliveredAt: '2026-09-10T14:30:00Z',
|
|
})
|
|
getEmployeeDeliveriesApi.mockResolvedValue({ employee: MARC, deliveries: [] })
|
|
renderComponent()
|
|
|
|
await userEvent.type(screen.getByLabelText(/numéro du ticket/i), 'a1b2c3d4e5')
|
|
await userEvent.click(screen.getByRole('button', { name: /^rechercher$/i }))
|
|
|
|
const resultat = await screen.findByTestId('ticket-result')
|
|
expect(getDeliveryByTicketCodeApi).toHaveBeenCalledWith('a1b2c3d4e5')
|
|
expect(within(resultat).getByText('Lot remis')).toBeInTheDocument()
|
|
expect(within(resultat).getByText(/remis par/i)).toHaveTextContent(new Date('2026-09-10T14:30:00Z').toLocaleString('fr-FR', {
|
|
day: '2-digit', month: '2-digit', year: 'numeric', hour: '2-digit', minute: '2-digit',
|
|
}))
|
|
|
|
// Le nom de l'employé mène à la liste de ses remises.
|
|
await userEvent.click(within(resultat).getByRole('button', { name: 'Marc Durand' }))
|
|
expect(getEmployeeDeliveriesApi).toHaveBeenCalledWith(10)
|
|
expect(await screen.findByText(/n'a encore remis aucun lot/i)).toBeInTheDocument()
|
|
})
|
|
|
|
it("affiche l'erreur quand le ticket n'existe pas", async () => {
|
|
getDeliveryByTicketCodeApi.mockRejectedValue(new Error('Aucun ticket ne correspond à ce numéro.'))
|
|
renderComponent()
|
|
|
|
await userEvent.type(screen.getByLabelText(/numéro du ticket/i), 'INCONNU')
|
|
await userEvent.click(screen.getByRole('button', { name: /^rechercher$/i }))
|
|
|
|
expect(await screen.findByText('Aucun ticket ne correspond à ce numéro.')).toBeInTheDocument()
|
|
})
|
|
})
|