improve tests

This commit is contained in:
sbriat
2023-01-27 16:34:06 +01:00
parent ee820a2e84
commit d5cf17e734
10 changed files with 320 additions and 70 deletions

View File

@@ -3,7 +3,7 @@ import { InjectMapper } from '@automapper/nestjs';
import { Controller, UsePipes } from '@nestjs/common';
import { CommandBus, QueryBus } from '@nestjs/cqrs';
import { GrpcMethod, RpcException } from '@nestjs/microservices';
import { DatabaseException } from 'src/modules/database/src/exceptions/DatabaseException';
import { DatabaseException } from 'src/modules/database/src/exceptions/database.exception';
import { AddUsernameCommand } from '../../commands/add-username.command';
import { CreateAuthenticationCommand } from '../../commands/create-authentication.command';
import { DeleteAuthenticationCommand } from '../../commands/delete-authentication.command';

View File

@@ -1,7 +1,7 @@
import { TestingModule, Test } from '@nestjs/testing';
import { DatabaseModule } from '../../../database/database.module';
import { PrismaService } from '../../../database/src/adapters/secondaries/prisma-service';
import { DatabaseException } from '../../../database/src/exceptions/DatabaseException';
import { DatabaseException } from '../../../database/src/exceptions/database.exception';
import { AuthenticationRepository } from '../../adapters/secondaries/authentication.repository';
import { v4 } from 'uuid';
import * as bcrypt from 'bcrypt';

View File

@@ -1,7 +1,7 @@
import { TestingModule, Test } from '@nestjs/testing';
import { DatabaseModule } from '../../../database/database.module';
import { PrismaService } from '../../../database/src/adapters/secondaries/prisma-service';
import { DatabaseException } from '../../../database/src/exceptions/DatabaseException';
import { DatabaseException } from '../../../database/src/exceptions/database.exception';
import { v4 } from 'uuid';
import { Type } from '../../domain/dtos/type.enum';
import { UsernameRepository } from '../../adapters/secondaries/username.repository';

View File

@@ -0,0 +1,41 @@
import { AmqpConnection } from '@golevelup/nestjs-rabbitmq';
import { Test, TestingModule } from '@nestjs/testing';
import { AuthenticationMessager } from '../../adapters/secondaries/authentication.messager';
const mockAmqpConnection = {
publish: jest.fn().mockImplementation(),
};
describe('AuthenticationMessager', () => {
let authenticationMessager: AuthenticationMessager;
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [],
providers: [
AuthenticationMessager,
{
provide: AmqpConnection,
useValue: mockAmqpConnection,
},
],
}).compile();
authenticationMessager = module.get<AuthenticationMessager>(
AuthenticationMessager,
);
});
it('should be defined', () => {
expect(authenticationMessager).toBeDefined();
});
it('should publish a message', async () => {
jest.spyOn(mockAmqpConnection, 'publish');
await authenticationMessager.publish(
'authentication.create.info',
'my-test',
);
expect(mockAmqpConnection.publish).toHaveBeenCalledTimes(1);
});
});

View File

@@ -0,0 +1,36 @@
import { AmqpConnection } from '@golevelup/nestjs-rabbitmq';
import { Test, TestingModule } from '@nestjs/testing';
import { LoggingMessager } from '../../adapters/secondaries/logging.messager';
const mockAmqpConnection = {
publish: jest.fn().mockImplementation(),
};
describe('LoggingMessager', () => {
let loggingMessager: LoggingMessager;
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [],
providers: [
LoggingMessager,
{
provide: AmqpConnection,
useValue: mockAmqpConnection,
},
],
}).compile();
loggingMessager = module.get<LoggingMessager>(LoggingMessager);
});
it('should be defined', () => {
expect(LoggingMessager).toBeDefined();
});
it('should publish a message', async () => {
jest.spyOn(mockAmqpConnection, 'publish');
await loggingMessager.publish('authentication.create.info', 'my-test');
expect(mockAmqpConnection.publish).toHaveBeenCalledTimes(1);
});
});

View File

@@ -9,7 +9,7 @@ import { ValidateAuthenticationQuery } from '../../queries/validate-authenticati
import { UsernameRepository } from '../../adapters/secondaries/username.repository';
import { Type } from '../../domain/dtos/type.enum';
import { NotFoundException, UnauthorizedException } from '@nestjs/common';
import { DatabaseException } from '../../../database/src/exceptions/DatabaseException';
import { DatabaseException } from '../../../database/src/exceptions/database.exception';
import { ValidateAuthenticationRequest } from '../../domain/dtos/validate-authentication.request';
const mockAuthenticationRepository = {