targo-backend/test/bank-codes.e2e-spec.ts

126 lines
3.7 KiB
TypeScript

import * as request from 'supertest';
import { INestApplication } from '@nestjs/common';
import { PrismaService } from 'src/prisma/prisma.service';
import { createApp } from './utils/testing-app';
// import { resetDb } from './utils/reset-db';
import { makeBankCode, makeInvalidBankCode } from './factories/bank-code.factory';
describe('BankCodes (e2e)', () => {
let app: INestApplication;
const BASE = '/bank-codes';
const RESET = process.env.E2E_RESET_DB === '1';
beforeAll(async () => {
app = await createApp();
});
beforeEach(async () => {
// if (RESET) await resetDb(app);
});
afterAll(async () => {
const prisma = app.get(PrismaService);
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 = makeBankCode();
const createRes = await request(app.getHttpServer())
.post(BASE)
.send(payload);
expect(createRes.status).toBe(201);
expect(createRes.body).toEqual(
expect.objectContaining({
id: expect.any(Number),
type: payload.type,
categorie: payload.categorie,
modifier: payload.modifier,
bank_code: payload.bank_code,
}),
);
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,
type: payload.type,
categorie: payload.categorie,
modifier: payload.modifier,
bank_code: payload.bank_code,
}),
);
});
it(`PATCH ${BASE}/:id → 200 (modifier mis à jour)`, async () => {
const create = await request(app.getHttpServer())
.post(BASE)
.send(makeBankCode());
expect(create.status).toBe(201);
const id = create.body.id;
const updated = { modifier: 2 };
const patchRes = await request(app.getHttpServer())
.patch(`${BASE}/${id}`)
.send(updated);
expect([200, 204]).toContain(patchRes.status);
const getRes = await request(app.getHttpServer()).get(`${BASE}/${id}`);
expect(getRes.status).toBe(200);
expect(getRes.body.modifier).toBe(updated.modifier);
});
it(`POST ${BASE} (invalid payload) → 400`, async () => {
const res = await request(app.getHttpServer())
.post(BASE)
.send(makeInvalidBankCode());
expect(res.status).toBeGreaterThanOrEqual(400);
expect(res.status).toBeLessThan(500);
});
it(`POST ${BASE} (duplicate bank_code) → 409 (ou 400)`, async () => {
const payload = makeBankCode();
const first = await request(app.getHttpServer()).post(BASE).send(payload);
expect(first.status).toBe(201);
const dup = await request(app.getHttpServer()).post(BASE).send({
...payload,
type: 'ANOTHER_TYPE_USING_SAME_BANK_CODE',
});
expect(dup.status).toBe(201);
});
it(`DELETE ${BASE}/:id → 200/204`, async () => {
const create = await request(app.getHttpServer())
.post(BASE)
.send(makeBankCode());
expect(create.status).toBe(201);
const id = create.body.id;
const del = await request(app.getHttpServer()).delete(`${BASE}/${id}`);
expect([200, 204]).toContain(del.status);
const after = await request(app.getHttpServer()).get(`${BASE}/${id}`);
expect(after.status).toBe(404);
});
it(`GET ${BASE}/:id (not found) → 404`, async () => {
const res = await request(app.getHttpServer()).get(`${BASE}/999999`);
expect([404, 400]).toContain(res.status);
});
});