-
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.
As a student, I want to delete a registered task so that I can remove it from my tasks list. This commit includes: - Implementation of the use case for deleting a task - Implementation of unit tests related to the use case and adapters
- Loading branch information
Leonardo Giraldi Moreno Giuranno
committed
May 19, 2024
1 parent
57932ab
commit 554947d
Showing
25 changed files
with
236 additions
and
0 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
src/adapters/task/deleteTask/boundaries/deleteTask.controller.input.boundary.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,5 @@ | ||
interface DeleteTaskControllerInputBoundary { | ||
delete(id: string): Promise<void>; | ||
} | ||
|
||
export { DeleteTaskControllerInputBoundary }; |
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 @@ | ||
export * from "./deleteTask.controller.input.boundary"; |
16 changes: 16 additions & 0 deletions
16
src/adapters/task/deleteTask/controllers/deleteTask.controller.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,16 @@ | ||
import { DeleteTaskControllerInputBoundary } from ".."; | ||
import { DeleteTaskInputBoundary } from "@/useCases/task"; | ||
|
||
class DeleteTaskController implements DeleteTaskControllerInputBoundary { | ||
private inputBoundary: DeleteTaskInputBoundary; | ||
|
||
constructor(inputBoundary: DeleteTaskInputBoundary) { | ||
this.inputBoundary = inputBoundary; | ||
} | ||
|
||
async delete(id: string): Promise<void> { | ||
return await this.inputBoundary.delete(id); | ||
} | ||
} | ||
|
||
export { DeleteTaskController }; |
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 @@ | ||
export * from "./deleteTask.controller"; |
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,3 @@ | ||
export * from "./boundaries"; | ||
export * from "./controllers"; | ||
export * from "./presenters"; |
17 changes: 17 additions & 0 deletions
17
src/adapters/task/deleteTask/presenters/deleteTask.presenters.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,17 @@ | ||
import { ResourceNotFoundException } from "@/adapters/exceptions"; | ||
import { | ||
DeleteTaskNotFoundException, | ||
DeleteTaskOutputBoundary, | ||
} from "@/useCases/task"; | ||
|
||
class DeleteTaskPresenter implements DeleteTaskOutputBoundary { | ||
presentSuccess() { | ||
return; | ||
} | ||
|
||
presentDeleteTaskNotFound(error: DeleteTaskNotFoundException) { | ||
throw new ResourceNotFoundException(error.message); | ||
} | ||
} | ||
|
||
export { DeleteTaskPresenter }; |
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 @@ | ||
export * from "./deleteTask.presenters"; |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./createTask"; | ||
export * from "./listTaskById"; | ||
export * from "./updateTask"; | ||
export * from "./deleteTask"; |
19 changes: 19 additions & 0 deletions
19
src/tests/unit/adapters/task/deleteTask/deleteTask.controller.test.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 { deleteTaskInputBoundaryMock } from "@/tests/unit/mocks/task"; | ||
|
||
import { DeleteTaskController } from "@/adapters/task"; | ||
|
||
describe("DeleteTaskController", () => { | ||
let controller: DeleteTaskController; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
|
||
controller = new DeleteTaskController(deleteTaskInputBoundaryMock); | ||
}); | ||
|
||
describe("delete", () => { | ||
it("should return undefined", async () => { | ||
expect(await controller.delete("1")).toBeUndefined(); | ||
}); | ||
}); | ||
}); |
30 changes: 30 additions & 0 deletions
30
src/tests/unit/adapters/task/deleteTask/deleteTask.presenter.test.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,30 @@ | ||
import { DeleteTaskPresenter } from "@/adapters/task"; | ||
import { ResourceNotFoundException } from "@/adapters/exceptions"; | ||
|
||
import { DeleteTaskNotFoundException } from "@/useCases/task"; | ||
|
||
describe("DeleteTaskPresenter", () => { | ||
let presenter: DeleteTaskPresenter; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
|
||
presenter = new DeleteTaskPresenter(); | ||
}); | ||
|
||
describe("presentSuccess", () => { | ||
it("should return undefined", async () => { | ||
expect(presenter.presentSuccess()).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe("presentDeleteTaskNotFound", () => { | ||
it("should throw ResourceNotFoundException", async () => { | ||
const error = new DeleteTaskNotFoundException("Task not found"); | ||
|
||
expect(() => presenter.presentDeleteTaskNotFound(error)).toThrow( | ||
new ResourceNotFoundException(error.message) | ||
); | ||
}); | ||
}); | ||
}); |
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 @@ | ||
const deleteTaskDsGatewayMock = { | ||
existsById: jest.fn(), | ||
remove: jest.fn(), | ||
}; | ||
|
||
export { deleteTaskDsGatewayMock }; |
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,5 @@ | ||
const deleteTaskInputBoundaryMock = { | ||
delete: jest.fn(), | ||
}; | ||
|
||
export { deleteTaskInputBoundaryMock }; |
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 @@ | ||
const deleteTaskOutputBoundaryMock = { | ||
presentSuccess: jest.fn(), | ||
presentDeleteTaskNotFound: jest.fn(), | ||
}; | ||
|
||
export { deleteTaskOutputBoundaryMock }; |
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
48 changes: 48 additions & 0 deletions
48
src/tests/unit/useCases/task/deleteTask.interactor.test.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,48 @@ | ||
import { | ||
deleteTaskDsGatewayMock, | ||
deleteTaskOutputBoundaryMock, | ||
} from "@/tests/unit/mocks/task"; | ||
|
||
import { DeleteTaskInteractor } from "@/useCases/task"; | ||
|
||
describe("DeleteTaskInteractor", () => { | ||
let interactor: DeleteTaskInteractor; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
|
||
interactor = new DeleteTaskInteractor( | ||
deleteTaskDsGatewayMock, | ||
deleteTaskOutputBoundaryMock | ||
); | ||
}); | ||
|
||
describe("delete", () => { | ||
beforeEach(() => { | ||
deleteTaskDsGatewayMock.existsById.mockResolvedValue(true); | ||
deleteTaskDsGatewayMock.remove.mockResolvedValue(undefined); | ||
deleteTaskOutputBoundaryMock.presentSuccess.mockReturnValue( | ||
undefined | ||
); | ||
deleteTaskOutputBoundaryMock.presentDeleteTaskNotFound.mockImplementation( | ||
() => { | ||
throw new Error("Task not found"); | ||
} | ||
); | ||
}); | ||
|
||
it("should return success", async () => { | ||
const result = await interactor.delete("1"); | ||
|
||
expect(result).toBeUndefined(); | ||
}); | ||
|
||
it("should return exception when task is not found", async () => { | ||
deleteTaskDsGatewayMock.existsById.mockResolvedValue(false); | ||
|
||
await expect(interactor.delete("1")).rejects.toThrow( | ||
"Task not found" | ||
); | ||
}); | ||
}); | ||
}); |
5 changes: 5 additions & 0 deletions
5
src/useCases/task/deleteTask/boundaries/deleteTask.input.boundary.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,5 @@ | ||
interface DeleteTaskInputBoundary { | ||
delete(id: string): Promise<void>; | ||
} | ||
|
||
export { DeleteTaskInputBoundary }; |
9 changes: 9 additions & 0 deletions
9
src/useCases/task/deleteTask/boundaries/deleteTask.output.boundary.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 { DeleteTaskNotFoundException } from ".."; | ||
|
||
interface DeleteTaskOutputBoundary { | ||
presentSuccess(): void; | ||
|
||
presentDeleteTaskNotFound(error: DeleteTaskNotFoundException): void; | ||
} | ||
|
||
export { DeleteTaskOutputBoundary }; |
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,2 @@ | ||
export * from "./deleteTask.input.boundary"; | ||
export * from "./deleteTask.output.boundary"; |
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 { | ||
DeleteTaskDsGateway, | ||
DeleteTaskInputBoundary, | ||
DeleteTaskNotFoundException, | ||
DeleteTaskOutputBoundary, | ||
} from "."; | ||
|
||
class DeleteTaskInteractor implements DeleteTaskInputBoundary { | ||
private dsGateway: DeleteTaskDsGateway; | ||
|
||
private outputBoundary: DeleteTaskOutputBoundary; | ||
|
||
constructor( | ||
dsGateway: DeleteTaskDsGateway, | ||
outputBoundary: DeleteTaskOutputBoundary | ||
) { | ||
this.dsGateway = dsGateway; | ||
this.outputBoundary = outputBoundary; | ||
} | ||
|
||
async delete(id: string): Promise<void> { | ||
if (!(await this.dsGateway.existsById(id))) { | ||
return this.outputBoundary.presentDeleteTaskNotFound( | ||
new DeleteTaskNotFoundException( | ||
`Tarefa com id ${id} não encontrada` | ||
) | ||
); | ||
} | ||
|
||
await this.dsGateway.remove(id); | ||
|
||
return this.outputBoundary.presentSuccess(); | ||
} | ||
} | ||
|
||
export { DeleteTaskInteractor }; |
8 changes: 8 additions & 0 deletions
8
src/useCases/task/deleteTask/exceptions/deleteTask.notFound.exception.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,8 @@ | ||
class DeleteTaskNotFoundException extends Error { | ||
constructor(message: string) { | ||
super(message); | ||
this.name = "DeleteTaskNotFoundException"; | ||
} | ||
} | ||
|
||
export { DeleteTaskNotFoundException }; |
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 @@ | ||
export * from "./deleteTask.notFound.exception"; |
7 changes: 7 additions & 0 deletions
7
src/useCases/task/deleteTask/gateways/deleteTask.ds.gateway.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,7 @@ | ||
interface DeleteTaskDsGateway { | ||
existsById(id: string): Promise<boolean>; | ||
|
||
remove(id: string): Promise<void>; | ||
} | ||
|
||
export { DeleteTaskDsGateway }; |
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 @@ | ||
export * from "./deleteTask.ds.gateway"; |
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,4 @@ | ||
export * from "./boundaries"; | ||
export * from "./exceptions"; | ||
export * from "./gateways"; | ||
export * from "./deleteTask.interactor"; |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./createTask"; | ||
export * from "./listTaskById"; | ||
export * from "./updateTask"; | ||
export * from "./deleteTask"; |