Skip to content
Snippets Groups Projects
personal-offers.controller.spec.ts 5.67 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { Test } from '@nestjs/testing';
    
    import {
      createPersonalOffersDtoDataMock,
      personalOffersDataMock,
    
      updatePersonalOffersDtoDataMock,
    
    } from '../../test/mock/data/personalOffers.mock.data';
    
    import { mockIsPersonalOfferOwnerGuard } from '../../test/mock/guards/isPersonalOfferOwner.mock.guard';
    import { mockJwtAuthGuard } from '../../test/mock/guards/jwt-auth.mock.guard';
    import { PersonalOffersServiceMock } from '../../test/mock/services/personalOffers.mock.service';
    import { StructuresServiceMock } from '../../test/mock/services/structures.mock.service';
    
    import { UsersServiceMock } from '../../test/mock/services/user.mock.service';
    
    import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
    
    import { StructuresService } from '../structures/services/structures.service';
    
    import { IsPersonalOfferOwnerGuard } from '../users/guards/isPersonalOfferOwner.guard';
    import { UsersService } from '../users/services/users.service';
    import { PersonalOffersController } from './personal-offers.controller';
    import { PersonalOffersService } from './personal-offers.service';
    
    
    describe('PersonalOffersController', () => {
    
      let personalOffersController: PersonalOffersController;
    
        const module = await Test.createTestingModule({
    
          controllers: [PersonalOffersController],
          providers: [
            {
              provide: PersonalOffersService,
              useClass: PersonalOffersServiceMock,
            },
            {
              provide: UsersService,
              useClass: UsersServiceMock,
            },
            {
              provide: StructuresService,
              useClass: StructuresServiceMock,
            },
          ],
        })
          .overrideGuard(JwtAuthGuard)
          .useValue(mockJwtAuthGuard)
          .overrideGuard(IsPersonalOfferOwnerGuard)
          .useValue(mockIsPersonalOfferOwnerGuard)
          .compile();
    
    
        personalOffersController = module.get<PersonalOffersController>(PersonalOffersController);
    
      });
    
      it('should be defined', () => {
    
        expect(personalOffersController).toBeDefined();
    
      });
    
      describe('find personal offer', () => {
        it('should get personal offer', async () => {
    
          expect(await personalOffersController.find('1234ba0e2ab5775cfc01ed3e')).toBe(personalOffersDataMock[0]);
    
        });
        it('should get personal offer does not exist', async () => {
          try {
    
            await personalOffersController.find('abcd');
    
            expect(true).toBe(false);
          } catch (e) {
            expect(e.message).toBe('Personal offer does not exist');
            expect(e.status).toBe(404);
          }
        });
      });
    
      describe('create personal offer', () => {
    
        it('should return 204 if personal offer is empty', async () => {
          const req = { user: { _id: '6036721022462b001334c4bb' } };
          try {
    
            await personalOffersController.create(req, createPersonalOffersDtoDataMock[3]);
    
            expect(true).toBe(false);
          } catch (e) {
            expect(e.message).toBe('Empty personal offer');
            expect(e.status).toBe(204);
          }
        });
    
        it('should create personal offer for existing user and structure', async () => {
          const req = { user: { _id: '6036721022462b001334c4bb' } };
    
          expect(await personalOffersController.create(req, createPersonalOffersDtoDataMock[0])).toEqual(
            personalOffersDataMock[0]
          );
    
        });
        it('should return personal offer already exist in the structure', async () => {
          const req = { user: { _id: '6036721022462b001334c4bb' } };
          try {
    
            await personalOffersController.create(req, createPersonalOffersDtoDataMock[1]);
    
            expect(true).toBe(false);
          } catch (e) {
            expect(e.message).toBe('Personal offer already exist in the structure');
            expect(e.status).toBe(400);
          }
        });
        it('should return structure not found for the personal offer attachment', async () => {
          const req = { user: { _id: '6036721022462b001334c4bb' } };
          try {
    
            await personalOffersController.create(req, createPersonalOffersDtoDataMock[2]);
    
            expect(true).toBe(false);
          } catch (e) {
            expect(e.message).toBe('Structure not found for the personal offer attachment');
            expect(e.status).toBe(400);
          }
        });
        it('should return user not found for the personal offer attachment', async () => {
          const req = { user: { _id: 'unIdQuiExistePasTropTrop' } };
          try {
    
            await personalOffersController.create(req, createPersonalOffersDtoDataMock[0]);
    
            expect(true).toBe(false);
          } catch (e) {
            expect(e.message).toBe('User not found for the personal offer attachment');
            expect(e.status).toBe(400);
          }
        });
      });
    
      describe('update personal offer', () => {
        it('should get updated personal offer', async () => {
    
          expect(
            await personalOffersController.update('2345ba0e2ab5775cfc01ed4d', updatePersonalOffersDtoDataMock[1])
          ).toEqual(personalOffersDataMock[1]);
    
        });
        it('should get invalid personal offer id', async () => {
          try {
    
            await personalOffersController.update('abcd', updatePersonalOffersDtoDataMock[1]);
    
            expect(true).toBe(false);
          } catch (e) {
            expect(e.message).toBe('Invalid personal offer id for update');
            expect(e.status).toBe(400);
          }
        });
      });
    
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
      describe('delete personal offer', () => {
    
        it('should get deleted personal offer', async () => {
    
          expect(await personalOffersController.delete('2345ba0e2ab5775cfc01ed4d')).toEqual(personalOffersDataMock[1]);
    
        });
        it('should get invalid personal offer id', async () => {
          try {
    
            await personalOffersController.delete('abcd');
    
            expect(true).toBe(false);
          } catch (e) {
            expect(e.message).toBe('Invalid personal offer id for deletion');
            expect(e.status).toBe(400);
          }
        });
      });
    });