-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #107 from 4lessandrodev/feat/delete-budget-box
test(application): added tests to delete budget box use case
- Loading branch information
Showing
14 changed files
with
253 additions
and
6 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
src/modules/budget-box/application/use-cases/delete-budget-box/delete-budget-box.dto.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,6 @@ | ||
export interface DeleteBudgetBoxDto { | ||
userId: string; | ||
budgetBoxId: string; | ||
} | ||
|
||
export default DeleteBudgetBoxDto; |
98 changes: 98 additions & 0 deletions
98
...les/budget-box/application/use-cases/delete-budget-box/delete-budget-box.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,98 @@ | ||
|
||
import { IBudgetBoxRepository } from '@modules/budget-box/domain/interfaces/budget-box.repository.interface'; | ||
import budgetBoxMockRepo from '@modules/budget-box/application/mocks/budget-box-repo.mock'; | ||
import DeleteBudgetBoxUseCase from './delete-budget-box.use-case'; | ||
import { BudgetBoxMock } from '@modules/budget-box/domain/tests/mock/budget-box.mock'; | ||
import { CURRENCY } from '@config/env'; | ||
|
||
describe('delete-budget-box.use-case', () => { | ||
|
||
let repo: IBudgetBoxRepository; | ||
const budgetBoxMock = new BudgetBoxMock(); | ||
|
||
beforeEach(() => { | ||
repo = budgetBoxMockRepo; | ||
jest.spyOn(repo, 'findOne').mockClear(); | ||
}); | ||
|
||
it('should execute delete-budget-box use case with success', async () => { | ||
|
||
const aggregate = budgetBoxMock.domain({ | ||
balanceAvailable: { | ||
currency: CURRENCY, | ||
value: 0 | ||
} | ||
}).getResult(); | ||
|
||
jest.spyOn(repo, 'findOne').mockResolvedValueOnce(aggregate); | ||
|
||
const useCase = new DeleteBudgetBoxUseCase(repo); | ||
const useCaseSpy = jest.spyOn(useCase, 'execute'); | ||
|
||
const dto = { userId: 'valid_id', budgetBoxId: 'valid_id' }; | ||
|
||
const result = await useCase.execute(dto); | ||
|
||
expect(aggregate.domainEvents).toHaveLength(1); | ||
|
||
expect(result.isSuccess).toBeTruthy(); | ||
expect(useCaseSpy).toHaveBeenCalledWith(dto); | ||
}); | ||
|
||
it('should fails if budget box does not exists', async () => { | ||
|
||
jest.spyOn(repo, 'findOne').mockResolvedValueOnce(null); | ||
|
||
const useCase = new DeleteBudgetBoxUseCase(repo); | ||
const saveSpy = jest.spyOn(repo, 'save'); | ||
|
||
const dto = { userId: 'valid_id', budgetBoxId: 'valid_id' }; | ||
|
||
const result = await useCase.execute(dto); | ||
|
||
expect(result.isFailure).toBeTruthy(); | ||
expect(result.error).toBe('Budget Box Does Not Exists'); | ||
expect(saveSpy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should fails if budget box has balance', async () => { | ||
|
||
const aggregate = budgetBoxMock.domain({ | ||
balanceAvailable: { | ||
currency: CURRENCY, | ||
value: 1 | ||
} | ||
}).getResult(); | ||
|
||
jest.spyOn(repo, 'findOne').mockResolvedValueOnce(aggregate); | ||
|
||
const useCase = new DeleteBudgetBoxUseCase(repo); | ||
const saveSpy = jest.spyOn(repo, 'save'); | ||
|
||
const dto = { userId: 'valid_id', budgetBoxId: 'valid_id' }; | ||
|
||
const result = await useCase.execute(dto); | ||
|
||
expect(result.isFailure).toBeTruthy(); | ||
expect(result.error).toBe('The budget box must have a zero balance'); | ||
expect(saveSpy).not.toHaveBeenCalledWith(dto); | ||
}); | ||
|
||
it('should fails if repository throws', async () => { | ||
|
||
jest.spyOn(repo, 'findOne').mockImplementationOnce(async () => { | ||
throw new Error("error"); | ||
}); | ||
|
||
const useCase = new DeleteBudgetBoxUseCase(repo); | ||
const saveSpy = jest.spyOn(repo, 'save'); | ||
|
||
const dto = { userId: 'valid_id', budgetBoxId: 'valid_id' }; | ||
|
||
const result = await useCase.execute(dto); | ||
|
||
expect(result.isFailure).toBeTruthy(); | ||
expect(result.error).toBe('Internal Server Error on Delete Budget BoxUse Case'); | ||
expect(saveSpy).not.toHaveBeenCalled(); | ||
}); | ||
}); |
41 changes: 41 additions & 0 deletions
41
src/modules/budget-box/application/use-cases/delete-budget-box/delete-budget-box.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { IBudgetBoxRepository } from '@modules/budget-box/domain/interfaces/budget-box.repository.interface'; | ||
import { Inject } from '@nestjs/common'; | ||
import { IUseCase, Result } from 'types-ddd'; | ||
import DeleteBudgetBoxDto from './delete-budget-box.dto'; | ||
|
||
export class DeleteBudgetBoxUseCase implements IUseCase<DeleteBudgetBoxDto, Result<void, string>>{ | ||
constructor ( | ||
@Inject('BudgetBoxRepository') | ||
private readonly budgetBoxRepo: IBudgetBoxRepository | ||
) { } | ||
|
||
async execute ({ budgetBoxId: id, userId: ownerId }: DeleteBudgetBoxDto): Promise<Result<void, string>> { | ||
try { | ||
|
||
const budgetBoxOrNull = await this.budgetBoxRepo.findOne({ id, ownerId }); | ||
|
||
if (!budgetBoxOrNull) { | ||
return Result.fail('Budget Box Does Not Exists', 'NOT_FOUND'); | ||
} | ||
|
||
const budgetBox = budgetBoxOrNull; | ||
|
||
const hasNotBalance = budgetBox.balanceAvailable.isEqualTo(0); | ||
|
||
if (!hasNotBalance) { | ||
return Result.fail('The budget box must have a zero balance', 'CONFLICT'); | ||
} | ||
|
||
budgetBox.delete(); | ||
|
||
await this.budgetBoxRepo.delete({ id }); | ||
|
||
return Result.success(); | ||
|
||
} catch (error) { | ||
return Result.fail('Internal Server Error on Delete Budget BoxUse Case', 'INTERNAL_SERVER_ERROR'); | ||
} | ||
} | ||
} | ||
|
||
export default DeleteBudgetBoxUseCase; |
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
19 changes: 19 additions & 0 deletions
19
src/modules/budget-box/domain/events/budget-box-deleted.event.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,19 @@ | ||
import { IDomainEvent, UniqueEntityID } from "types-ddd"; | ||
import BudgetBoxAggregate from "../budget-box.aggregate"; | ||
|
||
export class BudgetBoxDeletedEvent implements IDomainEvent { | ||
public dateTimeOccurred: Date; | ||
public budgetBox: BudgetBoxAggregate; | ||
|
||
constructor (budgetBox: BudgetBoxAggregate) { | ||
this.budgetBox = budgetBox; | ||
this.dateTimeOccurred = new Date(); | ||
} | ||
|
||
getAggregateId (): UniqueEntityID { | ||
return this.budgetBox.id.value; | ||
} | ||
|
||
} | ||
|
||
export default BudgetBoxDeletedEvent; |
22 changes: 22 additions & 0 deletions
22
src/modules/budget-box/domain/subscription/after-budget-box-deleted.subscription.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,22 @@ | ||
import { DomainEvents, IHandle, Logger } from "types-ddd"; | ||
import BudgetBoxDeletedEvent from "../events/budget-box-deleted.event"; | ||
|
||
export class AfterBudgetBoxDeleted implements IHandle<BudgetBoxDeletedEvent>{ | ||
constructor () { | ||
this.setupSubscriptions(); | ||
} | ||
|
||
setupSubscriptions (): void { | ||
DomainEvents.register( | ||
(event) => this.dispatch(Object.assign(event)), | ||
BudgetBoxDeletedEvent.name | ||
); | ||
} | ||
|
||
async dispatch (event: BudgetBoxDeletedEvent): Promise<void> { | ||
Logger.info(`budget box deleted: ${event.budgetBox.id.uid}`); | ||
} | ||
|
||
} | ||
|
||
export default AfterBudgetBoxDeleted; |
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
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
9 changes: 9 additions & 0 deletions
9
src/modules/budget-box/infra/inputs/delete-budget-box.input.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,9 @@ | ||
import { Field, InputType } from "@nestjs/graphql"; | ||
|
||
@InputType() | ||
export class DeleteBudgetBoxInput { | ||
@Field(() => String) | ||
budgetBoxId!: string; | ||
} | ||
|
||
export default DeleteBudgetBoxInput; |
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
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
Large diffs are not rendered by default.
Oops, something went wrong.