113 lines
3.5 KiB
TypeScript
113 lines
3.5 KiB
TypeScript
// test/timesheets.e2e-spec.ts
|
|
const request = require('supertest');
|
|
import { INestApplication } from '@nestjs/common';
|
|
import { PrismaService } from 'src/prisma/prisma.service';
|
|
import {
|
|
makeTimesheet,
|
|
makeInvalidTimesheet,
|
|
} from './factories/timesheet.factory';
|
|
import { makeEmployee } from './factories/employee.factory';
|
|
import { createApp } from './utils/testing-app';
|
|
|
|
describe('Timesheets (e2e) — autonome', () => {
|
|
const BASE = '/timesheets';
|
|
let app: INestApplication;
|
|
let employeeIdForCreation: number;
|
|
|
|
beforeAll(async () => {
|
|
app = await createApp();
|
|
|
|
// ✅ Crée un employé dédié pour cette suite
|
|
const empRes = await request(app.getHttpServer())
|
|
.post('/employees')
|
|
.send(makeEmployee());
|
|
if (empRes.status !== 201) {
|
|
// eslint-disable-next-line no-console
|
|
console.warn('Impossible de créer un employé pour les tests timesheets:', empRes.body || empRes.text);
|
|
throw new Error('Setup employé échoué');
|
|
}
|
|
employeeIdForCreation = empRes.body.id;
|
|
});
|
|
|
|
afterAll(async () => {
|
|
const prisma = app.get(PrismaService);
|
|
await app.close();
|
|
await prisma.$disconnect();
|
|
});
|
|
|
|
it(`GET ${BASE} → 200`, 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 = makeTimesheet(employeeIdForCreation, { is_approved: true });
|
|
|
|
const createRes = await request(app.getHttpServer()).post(BASE).send(payload);
|
|
if (createRes.status !== 201) {
|
|
// eslint-disable-next-line no-console
|
|
console.log('Create error:', createRes.body || createRes.text);
|
|
}
|
|
expect(createRes.status).toBe(201);
|
|
expect(createRes.body).toEqual(
|
|
expect.objectContaining({
|
|
id: expect.any(Number),
|
|
employee_id: payload.employee_id,
|
|
}),
|
|
);
|
|
const id = createRes.body.id;
|
|
|
|
const getRes = await request(app.getHttpServer()).get(`${BASE}/${id}`);
|
|
expect(getRes.status).toBe(200);
|
|
expect(getRes.body).toEqual(
|
|
expect.objectContaining({
|
|
id,
|
|
employee_id: payload.employee_id,
|
|
}),
|
|
);
|
|
});
|
|
|
|
it(`PATCH ${BASE}/:id → 200 (is_approved toggled)`, async () => {
|
|
const created = await request(app.getHttpServer())
|
|
.post(BASE)
|
|
.send(makeTimesheet(employeeIdForCreation));
|
|
expect(created.status).toBe(201);
|
|
const id = created.body.id;
|
|
|
|
const updated = await request(app.getHttpServer())
|
|
.patch(`${BASE}/${id}`)
|
|
.send({ is_approved: true });
|
|
expect(updated.status).toBe(200);
|
|
expect(updated.body).toEqual(
|
|
expect.objectContaining({
|
|
id,
|
|
is_approved: true,
|
|
}),
|
|
);
|
|
});
|
|
|
|
it(`POST ${BASE} (invalid payload) → 400`, async () => {
|
|
const bad = await request(app.getHttpServer())
|
|
.post(BASE)
|
|
.send(makeInvalidTimesheet());
|
|
|
|
expect(bad.status).toBeGreaterThanOrEqual(400);
|
|
expect(bad.status).toBeLessThan(500);
|
|
});
|
|
|
|
it(`DELETE ${BASE}/:id → 200/204`, async () => {
|
|
const created = await request(app.getHttpServer())
|
|
.post(BASE)
|
|
.send(makeTimesheet(employeeIdForCreation));
|
|
expect(created.status).toBe(201);
|
|
const id = created.body.id;
|
|
|
|
const delRes = await request(app.getHttpServer()).delete(`${BASE}/${id}`);
|
|
expect([200, 204]).toContain(delRes.status);
|
|
|
|
const getRes = await request(app.getHttpServer()).get(`${BASE}/${id}`);
|
|
expect(getRes.status).toBe(404);
|
|
});
|
|
});
|