targo-backend/test/timesheets-approval.e2e-spec.ts

69 lines
2.3 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.

const request = require('supertest');
import { INestApplication } from '@nestjs/common';
import { PrismaService } from 'src/prisma/prisma.service';
import { createApp } from './utils/testing-app';
import { makeEmployee } from './factories/employee.factory';
import { makeTimesheet } from './factories/timesheet.factory';
describe('Timesheets approval (e2e)', () => {
const BASE = '/timesheets';
let app: INestApplication;
let prisma: PrismaService;
let timesheetId: number;
beforeAll(async () => {
app = await createApp();
prisma = app.get(PrismaService);
// On crée un employé dédié pour ne dépendre daucun état antérieur
const emp = await request(app.getHttpServer())
.post('/employees')
.send(makeEmployee());
if (emp.status !== 201) {
// eslint-disable-next-line no-console
console.warn('Création employé échouée:', emp.body || emp.text);
throw new Error('Setup employees pour timesheets-approval échoué');
}
const ts = await request(app.getHttpServer())
.post(BASE)
.send(makeTimesheet(emp.body.id));
if (ts.status !== 201) {
// eslint-disable-next-line no-console
console.warn('Création timesheet échouée:', ts.body || ts.text);
throw new Error('Setup timesheet échoué');
}
timesheetId = ts.body.id;
});
afterAll(async () => {
await app.close();
await prisma.$disconnect();
});
it(`PATCH ${BASE}/:id/approval → 200 (true)`, async () => {
const res = await request(app.getHttpServer())
.patch(`${BASE}/${timesheetId}/approval`)
.send({ is_approved: true });
expect(res.status).toBe(200);
expect(res.body?.is_approved).toBe(true);
});
it(`PATCH ${BASE}/:id/approval → 200 (false)`, async () => {
const res = await request(app.getHttpServer())
.patch(`${BASE}/${timesheetId}/approval`)
.send({ is_approved: false });
expect(res.status).toBe(200);
expect(res.body?.is_approved).toBe(false);
});
it(`PATCH ${BASE}/:id/approval (invalid) → 400`, async () => {
const res = await request(app.getHttpServer())
.patch(`${BASE}/${timesheetId}/approval`)
.send({ is_approved: 'plop' });
expect(res.status).toBeGreaterThanOrEqual(400);
expect(res.status).toBeLessThan(500);
});
});