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..f895c65 --- /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 a user', async () => { + const findAdByUuidRequest: FindAdByUuidRequest = + new FindAdByUuidRequest(); + findAdByUuidRequest.uuid = 'bb281075-1b98-4456-89d6-c643d3044a91'; + const user = await findAdByUuidUseCase.execute( + new FindAdByUuidQuery(findAdByUuidRequest), + ); + expect(user).toBe(mockAd); + }); + it('should throw an error if user 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..4a3c396 --- /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('user.create.info', 'my-test'); + expect(mockAmqpConnection.publish).toHaveBeenCalledTimes(1); + }); +});