Merge branch 'testing' into 'main'

Testing

See merge request v3/service/ad!2
This commit is contained in:
Sylvain Briat 2023-05-04 13:36:12 +00:00
commit 2399315e8a
4 changed files with 118 additions and 6 deletions

View File

@ -3,7 +3,4 @@ import { AutoMap } from '@automapper/classes';
export class AdPresenter { export class AdPresenter {
@AutoMap() @AutoMap()
uuid: string; uuid: string;
@AutoMap()
driver: boolean;
} }

View File

@ -3,7 +3,4 @@ import { AutoMap } from '@automapper/classes';
export class Ad { export class Ad {
@AutoMap() @AutoMap()
uuid: string; uuid: string;
@AutoMap()
driver: boolean;
} }

View File

@ -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>(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);
});
});
});

View File

@ -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>(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);
});
});