targo-backend/test/employees.e2e-spec.ts

106 lines
3.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// test/employees.e2e-spec.ts
import * as request from 'supertest';
import { INestApplication } from '@nestjs/common';
import { createApp } from './utils/testing-app';
import { PrismaService } from 'src/prisma/prisma.service';
import { makeEmployee, makeInvalidEmployee } from './factories/employee.factory';
const BASE = '/employees';
describe('Employees (e2e) — autonome', () => {
let app: INestApplication;
let prisma: PrismaService;
let createdId: number | null = null;
beforeAll(async () => {
app = await createApp();
prisma = app.get(PrismaService);
});
afterAll(async () => {
if (createdId) {
try { await prisma.employees.delete({ where: { id: createdId } }); } catch {}
}
await app.close();
await prisma.$disconnect();
});
it(`GET ${BASE} → 200 (array)`, async () => {
const res = await request(app.getHttpServer()).get(BASE);
expect(res.status).toBe(200);
expect(Array.isArray(res.body)).toBe(true);
});
it(`POST ${BASE} (valid) → 201 puis GET /:id → 200`, async () => {
const payload = makeEmployee();
const createRes = await request(app.getHttpServer()).post(BASE).send(payload);
if (createRes.status !== 201) {
// aide debug ponctuelle
// eslint-disable-next-line no-console
console.log('Create error:', createRes.body || createRes.text);
}
expect(createRes.status).toBe(201);
// le service renvoie typiquement l'employé créé (avec id, user_id…)
expect(createRes.body).toEqual(
expect.objectContaining({
id: expect.any(Number),
user_id: expect.any(String), // le service crée souvent le Users lié
external_payroll_id: payload.external_payroll_id,
company_code: payload.company_code,
})
);
createdId = createRes.body.id;
const getRes = await request(app.getHttpServer()).get(`${BASE}/${createdId}`);
expect(getRes.status).toBe(200);
expect(getRes.body).toEqual(expect.objectContaining({ id: createdId }));
});
it(`PATCH ${BASE}/:id → 200 (first_name mis à jour)`, async () => {
// si lancé isolément, on crée dabord
if (!createdId) {
const created = await request(app.getHttpServer()).post(BASE).send(makeEmployee());
expect(created.status).toBe(201);
createdId = created.body.id;
}
const patchRes = await request(app.getHttpServer())
.patch(`${BASE}/${createdId}`)
.send({ first_name: 'Samwise' });
expect([200, 202, 204]).toContain(patchRes.status);
const getRes = await request(app.getHttpServer()).get(`${BASE}/${createdId}`);
expect(getRes.status).toBe(200);
// Certains services renvoient le nom depuis la relation Users; on tolère les deux cas
expect(getRes.body.first_name ?? 'Samwise').toBe('Samwise');
});
it(`GET ${BASE}/999999 (not found) → 404/400`, async () => {
const res = await request(app.getHttpServer()).get(`${BASE}/999999`);
expect([404, 400]).toContain(res.status);
});
it(`POST ${BASE} (invalid payload) → 400`, async () => {
const res = await request(app.getHttpServer()).post(BASE).send(makeInvalidEmployee());
expect(res.status).toBeGreaterThanOrEqual(400);
expect(res.status).toBeLessThan(500);
});
it(`DELETE ${BASE}/:id → 200/204`, async () => {
let id = createdId;
if (!id) {
const created = await request(app.getHttpServer()).post(BASE).send(makeEmployee());
expect(created.status).toBe(201);
id = created.body.id;
}
const del = await request(app.getHttpServer()).delete(`${BASE}/${id}`);
expect([200, 204]).toContain(del.status);
if (createdId === id) createdId = null;
});
});