company-drivers.e2e-spec.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { Test, TestingModule } from '@nestjs/testing';
  2. import { INestApplication } from '@nestjs/common';
  3. import * as request from 'supertest';
  4. import { AppModule } from '../../src/app.module';
  5. import { ValidationPipe } from '@nestjs/common';
  6. import { TransformInterceptor } from '../../src/presentation/interceptors/transform.interceptor';
  7. describe('Company Drivers Endpoint (e2e)', () => {
  8. let app: INestApplication;
  9. beforeEach(async () => {
  10. const moduleFixture: TestingModule = await Test.createTestingModule({
  11. imports: [AppModule],
  12. }).compile();
  13. app = moduleFixture.createNestApplication();
  14. // Appliquer les mêmes pipes et intercepteurs que dans l'application principale
  15. app.useGlobalPipes(
  16. new ValidationPipe({
  17. whitelist: true,
  18. transform: true,
  19. }),
  20. );
  21. app.useGlobalInterceptors(new TransformInterceptor());
  22. await app.init();
  23. });
  24. afterEach(async () => {
  25. await app.close();
  26. });
  27. it('/api/companies/:id/drivers (GET) - should return drivers for a company', () => {
  28. const companyId = 1; // Utiliser un ID de compagnie existant
  29. return request(app.getHttpServer())
  30. .get(`/api/companies/${companyId}/drivers`)
  31. .expect(200)
  32. .expect((res) => {
  33. expect(res.body).toHaveProperty('data');
  34. expect(res.body).toHaveProperty('total');
  35. expect(res.body).toHaveProperty('page');
  36. expect(res.body).toHaveProperty('limit');
  37. expect(Array.isArray(res.body.data)).toBe(true);
  38. });
  39. });
  40. it('/api/companies/:id/drivers (GET) - should return 404 for non-existent company', () => {
  41. const nonExistentCompanyId = 99999;
  42. return request(app.getHttpServer())
  43. .get(`/api/companies/${nonExistentCompanyId}/drivers`)
  44. .expect(404);
  45. });
  46. it('/api/companies/:id/drivers (GET) - should support pagination', () => {
  47. const companyId = 1;
  48. return request(app.getHttpServer())
  49. .get(`/api/companies/${companyId}/drivers?page=1&limit=5`)
  50. .expect(200)
  51. .expect((res) => {
  52. expect(res.body.page).toBe(1);
  53. expect(res.body.limit).toBe(5);
  54. });
  55. });
  56. });