Skip to content

Commit

Permalink
test: ✨ Implementando pruebas unitarias
Browse files Browse the repository at this point in the history
Agregando las pruebas unitarias faltantes a los casos de uso
  • Loading branch information
HernanVelasquz committed Jul 4, 2023
1 parent 4b44663 commit 23cde45
Show file tree
Hide file tree
Showing 26 changed files with 450 additions and 138 deletions.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@
"!**/*index.ts"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
"testEnvironment": "node",
"coveragePathIgnorePatterns": [
"/src/migrations/*",
"/src/infrastructure/framework/typeorm/data/*.data.ts"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Test, TestingModule } from '@nestjs/testing';
import { TransferEntity, UserEntity } from '../../../../domain';
import { TransferFactoryService } from '../transfer-factory.service';

describe('Create Transfer Factory', () => {
let transferFactoyService: TransferFactoryService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [TransferFactoryService],
}).compile();

transferFactoyService = module.get<TransferFactoryService>(
TransferFactoryService,
);
});

describe('', () => {
it('should create a new transfer object', () => {
const createTransferDto: TransferEntity = {
id: '1',
fromEmail: '[email protected]',
toEmail: '[email protected]',
valueTransfer: 200,
date: new Date(),
user: new UserEntity(),
};

const newTransfer =
transferFactoyService.createNewTransfer(createTransferDto);

expect(newTransfer.fromEmail).toBe(createTransferDto.fromEmail);
expect(newTransfer.toEmail).toBe(createTransferDto.toEmail);
expect(newTransfer.user).toBeInstanceOf(UserEntity);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable } from '@nestjs/common';
import { TransferEntity } from 'src/domain';
import { TransferEntity } from '../../../domain';

/**
* @class TransferFactoryService
Expand Down
2 changes: 1 addition & 1 deletion src/application/transfer/transfer-use-case.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Module } from '@nestjs/common';
import { DataServicesModule } from 'src/infrastructure';
import { DataServicesModule } from '../../infrastructure';
import { UserUseCaseModule } from '../user';
import { TransferFactoryService } from './factories/transfer-factory.service';
import { GetHistoryTransferUseCase } from './use-cases';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable } from '@nestjs/common';
import { IDataServices } from 'src/domain';
import { IDataServices } from '../../../../domain';
import { TransferFactoryService } from '../../factories';

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@nestjs/common';
import { from, map, Observable } from 'rxjs';
import { TransferEntity } from 'src/domain';
import { TransferEntity } from '../../../domain';
import { DependencyTransferAbstract } from './abstracts';

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Injectable, InternalServerErrorException } from '@nestjs/common';
import { from, Observable, map, switchMap } from 'rxjs';

import { UserFactoryService } from 'src/application/user';
import { IDataServices, TransferEntity, UserEntity } from 'src/domain';
import { InsufficientFundsException } from 'src/infrastructure';
import { UserFactoryService } from '../../../application/user';
import { IDataServices, TransferEntity, UserEntity } from '../../../domain';
import { InsufficientFundsException } from '../../../infrastructure';
import { TransferFactoryService } from '../factories';
import { DependencyTransferAbstract } from './abstracts';

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
import { IDataServices, TransferEntity, UserEntity } from '../../../../domain';
import { TransferFactoryService } from '../../factories';
import { GetHistoryTransferUseCase } from '../getHistoryTransfer.use-case';
import { Observable } from 'rxjs';
describe('Get History Transfer Use Case', () => {
let getHistoryTransferUseCase: GetHistoryTransferUseCase;
let dataService: IDataServices;

beforeEach(() => {
dataService = {
transfer: {
getAll: jest.fn(),
},
} as any as IDataServices;

const userFactoryService = {
createNewTransfer: jest.fn(),
} as any as TransferFactoryService;

getHistoryTransferUseCase = new GetHistoryTransferUseCase(
dataService,
userFactoryService,
);
});

it('should return list history transfer', (done) => {
const idUser = '56a3203d-facf-436d-9faa-e2f739c4fb90';
const arrTransfer: TransferEntity[] = [
{
id: 'a0818ec2-51c8-4553-9615-2d6c162b6e6f',
toEmail: '[email protected]',
fromEmail: '[email protected]',
valueTransfer: 500,
date: new Date(),
user: {
id: '56a3203d-facf-436d-9faa-e2f739c4fb90',
fullName: 'MARIA FERNANDA BARRETO GOMEZ',
typeDocument: 'CC',
numberDocument: '10055620934',
email: '[email protected]',
deposit: 0,
} as UserEntity,
},
{
id: 'faceabdd-fced-4ea9-b840-555f6fd64831',
toEmail: '[email protected]',
fromEmail: '[email protected]',
valueTransfer: 500,
date: new Date(),
user: {
id: '56a3203d-facf-436d-9faa-e2f739c4fb90',
fullName: 'MARIA FERNANDA BARRETO GOMEZ',
typeDocument: 'CC ',
numberDocument: '10055620934',
email: '[email protected]',
deposit: 0,
} as UserEntity,
},
{
id: 'e2ddff42-0866-4512-9605-f135d398ce45',
toEmail: '[email protected]',
fromEmail: '[email protected]',
valueTransfer: 500,
date: new Date(),
user: {
id: '56a3203d-facf-436d-9faa-e2f739c4fb90',
fullName: 'MARIA FERNANDA BARRETO GOMEZ',
typeDocument: 'CC ',
numberDocument: '10055620934',
email: '[email protected]',
deposit: 0,
} as UserEntity,
},
{
id: '79d99614-21c6-48a3-be03-e6e9ec536997',
toEmail: '[email protected]',
fromEmail: '[email protected]',
valueTransfer: 500,
date: new Date(),
user: {
id: '56a3203d-facf-436d-9faa-e2f739c4fb90',
fullName: 'MARIA FERNANDA BARRETO GOMEZ',
typeDocument: 'CC ',
numberDocument: '10055620934',
email: '[email protected]',
deposit: 0,
} as UserEntity,
},
];

const arrExpect: TransferEntity[] = [
{
id: 'a0818ec2-51c8-4553-9615-2d6c162b6e6f',
toEmail: '[email protected]',
fromEmail: '[email protected]',
valueTransfer: 500,
date: new Date(),
user: {
id: '56a3203d-facf-436d-9faa-e2f739c4fb90',
fullName: 'MARIA FERNANDA BARRETO GOMEZ',
typeDocument: 'CC',
numberDocument: '10055620934',
email: '[email protected]',
deposit: 0,
} as UserEntity,
},
{
id: 'faceabdd-fced-4ea9-b840-555f6fd64831',
toEmail: '[email protected]',
fromEmail: '[email protected]',
valueTransfer: 500,
date: new Date(),
user: {
id: '56a3203d-facf-436d-9faa-e2f739c4fb90',
fullName: 'MARIA FERNANDA BARRETO GOMEZ',
typeDocument: 'CC ',
numberDocument: '10055620934',
email: '[email protected]',
deposit: 0,
} as UserEntity,
},
{
id: 'e2ddff42-0866-4512-9605-f135d398ce45',
toEmail: '[email protected]',
fromEmail: '[email protected]',
valueTransfer: 500,
date: new Date(),
user: {
id: '56a3203d-facf-436d-9faa-e2f739c4fb90',
fullName: 'MARIA FERNANDA BARRETO GOMEZ',
typeDocument: 'CC ',
numberDocument: '10055620934',
email: '[email protected]',
deposit: 0,
} as UserEntity,
},
{
id: '79d99614-21c6-48a3-be03-e6e9ec536997',
toEmail: '[email protected]',
fromEmail: '[email protected]',
valueTransfer: 500,
date: new Date(),
user: {
id: '56a3203d-facf-436d-9faa-e2f739c4fb90',
fullName: 'MARIA FERNANDA BARRETO GOMEZ',
typeDocument: 'CC ',
numberDocument: '10055620934',
email: '[email protected]',
deposit: 0,
} as UserEntity,
},
];

jest.spyOn(dataService.transfer, 'getAll').mockImplementation(
() =>
new Observable((subscribe) => {
subscribe.next(arrTransfer);
subscribe.complete();
}),
);

const $result = getHistoryTransferUseCase.historyTransferUseCase(idUser);

$result.subscribe({
next: (history) => {
expect(history).toEqual(arrExpect);
},
complete: () => done(),
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { Observable } from 'rxjs';
import { UserFactoryService } from '../../../../application/user';
import { IDataServices, TransferEntity, UserEntity } from '../../../../domain';
import { InsufficientFundsException } from '../../../../infrastructure';
import { TransferFactoryService } from '../../factories';
import { RegisterTransferUseCase } from '../register-transfer.use-case';

import { InternalServerErrorException } from '@nestjs/common';

describe('RegisterTransferUseCase', () => {
let registerTransferUseCase: RegisterTransferUseCase;
let dataServices: IDataServices;
let transferFactoryService: TransferFactoryService;
let userFactoryService: UserFactoryService;

const transferDto = new TransferEntity();
transferDto.fromEmail = '[email protected]';
transferDto.toEmail = '[email protected]';
transferDto.valueTransfer = 500;

const fromUser = new UserEntity();
fromUser.id = '1';
fromUser.fullName = 'From User';
fromUser.email = '[email protected]';
fromUser.deposit = 1000;
fromUser.transactions = [];

const toUser = new UserEntity();
toUser.id = '2';
toUser.fullName = 'To User';
toUser.email = '[email protected]';
toUser.deposit = 0;
toUser.transactions = [];

beforeEach(() => {
dataServices = {
user: {
getAll: jest.fn().mockReturnValue(Promise.resolve([fromUser, toUser])),
update: jest.fn(),
},
transfer: {
create: jest.fn(),
},
} as any as IDataServices;

transferFactoryService = {
createNewTransfer: jest.fn().mockReturnValue(transferDto),
};

userFactoryService = {
updateUser: jest.fn().mockReturnValue(fromUser),
} as any as UserFactoryService;

registerTransferUseCase = new RegisterTransferUseCase(
dataServices,
transferFactoryService,
userFactoryService,
);
});

it('should register a transfer and update user deposits', (done) => {
const transferFacoty =
transferFactoryService.createNewTransfer(transferDto);
const updatedFromUser = { ...fromUser };
const updatedToUser = { ...toUser };

jest.spyOn(dataServices.transfer, 'create').mockImplementation(
() =>
new Observable((subscriber) => {
subscriber.next(transferFacoty);
subscriber.complete();
}),
);

const $result = registerTransferUseCase.registerTransfer(transferDto);

$result.subscribe({
next: (transfer) => {
expect(transfer).toEqual(transferFacoty);
expect(updatedFromUser.deposit).toEqual(1000);
expect(updatedToUser.deposit).toEqual(0);
},
complete: () => done(),
});
});

it('should throw an InsufficientFundsException when the transfer value exceeds the fromUser deposit', (done) => {
transferDto.valueTransfer = 2000;

const $result = registerTransferUseCase.registerTransfer(transferDto);

$result.subscribe({
error: (error) => {
expect(error instanceof InsufficientFundsException).toBeTruthy();
done();
},
});
});

it('should throw an InternalServerErrorException when either the fromUser or toUser does not have a registered account', (done) => {
jest.spyOn(dataServices.user, 'getAll').mockImplementation(
() =>
new Observable((subscriber) => {
subscriber.next([fromUser]);
subscriber.complete();
}),
);

const $result = registerTransferUseCase.registerTransfer(transferDto);

$result.subscribe({
error: (error) => {
expect(error instanceof InternalServerErrorException).toBeTruthy();
done();
},
});
});
});
2 changes: 0 additions & 2 deletions src/application/user/fatories/test/create-factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ describe('Create User Factory', () => {
typeDocument: 'DNI',
numberDocument: '12345678',
email: '[email protected]',
password: 'password123',
transactions: [],
deposit: 1000,
};
Expand All @@ -32,7 +31,6 @@ describe('Create User Factory', () => {
expect(newUser.typeDocument).toBe(createUserDto.typeDocument);
expect(newUser.numberDocument).toBe(createUserDto.numberDocument);
expect(newUser.email).toBe(createUserDto.email);
expect(newUser.password).toBe(createUserDto.password);
expect(newUser.transactions).toEqual([]);
expect(newUser.deposit).toBe(1000);
});
Expand Down
Loading

0 comments on commit 23cde45

Please sign in to comment.