forked from HernanVelasquz/Clean-Architecture-NesJs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: ✨ Implementando pruebas unitarias
Agregando las pruebas unitarias faltantes a los casos de uso
- Loading branch information
1 parent
4b44663
commit 23cde45
Showing
26 changed files
with
450 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/application/transfer/factories/test/transfor-factory.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/application/transfer/use-cases/abstracts/dependency-transfer.abstracts.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/application/transfer/use-cases/getHistoryTransfer.use-case.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
src/application/transfer/use-cases/register-transfer.use-case.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletions
171
src/application/transfer/use-cases/test/getHistoryTransfer.use-case.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
}); | ||
}); | ||
}); |
118 changes: 118 additions & 0 deletions
118
src/application/transfer/use-cases/test/register-transfer.use-case.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,6 @@ describe('Create User Factory', () => { | |
typeDocument: 'DNI', | ||
numberDocument: '12345678', | ||
email: '[email protected]', | ||
password: 'password123', | ||
transactions: [], | ||
deposit: 1000, | ||
}; | ||
|
@@ -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); | ||
}); | ||
|
Oops, something went wrong.