diff --git a/src/modules/ad/adapters/primaries/ad.presenter.ts b/src/modules/ad/adapters/primaries/ad.presenter.ts index b4e58e4..1a4d67c 100644 --- a/src/modules/ad/adapters/primaries/ad.presenter.ts +++ b/src/modules/ad/adapters/primaries/ad.presenter.ts @@ -3,7 +3,4 @@ import { AutoMap } from '@automapper/classes'; export class AdPresenter { @AutoMap() uuid: string; - - @AutoMap() - driver: boolean; } diff --git a/src/modules/ad/domain/entities/ad.ts b/src/modules/ad/domain/entities/ad.ts index b4e95e0..0350f1a 100644 --- a/src/modules/ad/domain/entities/ad.ts +++ b/src/modules/ad/domain/entities/ad.ts @@ -3,7 +3,4 @@ import { AutoMap } from '@automapper/classes'; export class Ad { @AutoMap() uuid: string; - - @AutoMap() - driver: boolean; } diff --git a/src/modules/ad/tests/unit/find-ad-by-uuid.usecase.spec.ts b/src/modules/ad/tests/unit/find-ad-by-uuid.usecase.spec.ts new file mode 100644 index 0000000..7e124df --- /dev/null +++ b/src/modules/ad/tests/unit/find-ad-by-uuid.usecase.spec.ts @@ -0,0 +1,71 @@ +import { NotFoundException } from '@nestjs/common'; +import { Test, TestingModule } from '@nestjs/testing'; +import { Messager } from '../../adapters/secondaries/messager'; +import { FindAdByUuidQuery } from '../../queries/find-ad-by-uuid.query'; +import { AdsRepository } from '../../adapters/secondaries/ads.repository'; +import { FindAdByUuidUseCase } from '../../domain/usecases/find-ad-by-uuid.usecase'; +import { FindAdByUuidRequest } from '../../domain/dtos/find-ad-by-uuid.request'; + +const mockAd = { + uuid: 'bb281075-1b98-4456-89d6-c643d3044a91', +}; + +const mockAdRepository = { + findOneByUuid: jest + .fn() + // eslint-disable-next-line @typescript-eslint/no-unused-vars + .mockImplementationOnce((query?: FindAdByUuidQuery) => { + return Promise.resolve(mockAd); + }) + .mockImplementation(() => { + return Promise.resolve(undefined); + }), +}; + +const mockMessager = { + publish: jest.fn().mockImplementation(), +}; + +describe('FindAdByUuidUseCase', () => { + let findAdByUuidUseCase: FindAdByUuidUseCase; + beforeAll(async () => { + const module: TestingModule = await Test.createTestingModule({ + imports: [], + providers: [ + { + provide: AdsRepository, + useValue: mockAdRepository, + }, + { + provide: Messager, + useValue: mockMessager, + }, + FindAdByUuidUseCase, + ], + }).compile(); + + findAdByUuidUseCase = module.get(FindAdByUuidUseCase); + }); + it('should be defined', () => { + expect(findAdByUuidUseCase).toBeDefined(); + }); + describe('execute', () => { + it('should return an ad', async () => { + const findAdByUuidRequest: FindAdByUuidRequest = + new FindAdByUuidRequest(); + findAdByUuidRequest.uuid = 'bb281075-1b98-4456-89d6-c643d3044a91'; + const ad = await findAdByUuidUseCase.execute( + new FindAdByUuidQuery(findAdByUuidRequest), + ); + expect(ad).toBe(mockAd); + }); + it('should throw an error if ad does not exist', async () => { + const findAdByUuidRequest: FindAdByUuidRequest = + new FindAdByUuidRequest(); + findAdByUuidRequest.uuid = 'bb281075-1b98-4456-89d6-c643d3044a90'; + await expect( + findAdByUuidUseCase.execute(new FindAdByUuidQuery(findAdByUuidRequest)), + ).rejects.toBeInstanceOf(NotFoundException); + }); + }); +}); diff --git a/src/modules/ad/tests/unit/messager.spec.ts b/src/modules/ad/tests/unit/messager.spec.ts new file mode 100644 index 0000000..2296553 --- /dev/null +++ b/src/modules/ad/tests/unit/messager.spec.ts @@ -0,0 +1,47 @@ +import { AmqpConnection } from '@golevelup/nestjs-rabbitmq'; +import { ConfigService } from '@nestjs/config'; +import { Test, TestingModule } from '@nestjs/testing'; +import { Messager } from '../../adapters/secondaries/messager'; + +const mockAmqpConnection = { + publish: jest.fn().mockImplementation(), +}; + +const mockConfigService = { + get: jest.fn().mockResolvedValue({ + RMQ_EXCHANGE: 'mobicoop', + }), +}; + +describe('Messager', () => { + let messager: Messager; + + beforeAll(async () => { + const module: TestingModule = await Test.createTestingModule({ + imports: [], + providers: [ + Messager, + { + provide: AmqpConnection, + useValue: mockAmqpConnection, + }, + { + provide: ConfigService, + useValue: mockConfigService, + }, + ], + }).compile(); + + messager = module.get(Messager); + }); + + it('should be defined', () => { + expect(messager).toBeDefined(); + }); + + it('should publish a message', async () => { + jest.spyOn(mockAmqpConnection, 'publish'); + messager.publish('ad.create.info', 'my-test'); + expect(mockAmqpConnection.publish).toHaveBeenCalledTimes(1); + }); +});