-
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.
- Loading branch information
1 parent
c97ca79
commit dc128db
Showing
2 changed files
with
80 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
||
import { ResourceNotFoundError } from "@/application/errors/resource-not-found"; | ||
import type { IUserRepository } from "@/domain/repositories/user-repository"; | ||
import { InMemoryUserRepository } from "@/infra/repositories/in-memory/in-memory-user-repository"; | ||
import { GetUserByIdUseCase } from "./get-user-by-id"; | ||
|
||
describe("GetUserByIdUseCase", () => { | ||
let userRepository: IUserRepository; | ||
let getUserByIdUseCase: GetUserByIdUseCase; | ||
|
||
beforeEach(() => { | ||
userRepository = new InMemoryUserRepository(); | ||
getUserByIdUseCase = new GetUserByIdUseCase(userRepository); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.resetAllMocks(); | ||
}); | ||
|
||
it("should be able to return the user when found", async () => { | ||
const input = { id: "123" }; | ||
|
||
userRepository.findById = vi.fn().mockResolvedValue({ | ||
id: "123", | ||
}); | ||
|
||
const { user } = await getUserByIdUseCase.execute(input); | ||
|
||
expect(user).toEqual( | ||
expect.objectContaining({ | ||
id: "123", | ||
}), | ||
); | ||
expect(userRepository.findById).toHaveBeenCalledWith("123"); | ||
expect(userRepository.findById).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it("should throw ResourceNotFoundError when user is not found", async () => { | ||
const input = { id: "123" }; | ||
|
||
userRepository.findById = vi.fn().mockResolvedValue(null); | ||
|
||
await expect(getUserByIdUseCase.execute(input)).rejects.toThrow( | ||
ResourceNotFoundError, | ||
); | ||
expect(userRepository.findById).toHaveBeenCalledWith("123"); | ||
expect(userRepository.findById).toHaveBeenCalledOnce(); | ||
}); | ||
}); |
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 type { User } from "@/domain/entities/user"; | ||
import type { UseCase } from "@/domain/interfaces/use-case"; | ||
import type { IUserRepository } from "@/domain/repositories/user-repository"; | ||
import { ResourceNotFoundError } from "../errors/resource-not-found"; | ||
|
||
export type GetUserByIdInput = { | ||
id: string; | ||
}; | ||
|
||
export type GetUserByIdOutput = { | ||
user: User; | ||
}; | ||
|
||
export class GetUserByIdUseCase | ||
implements UseCase<GetUserByIdInput, GetUserByIdOutput> | ||
{ | ||
constructor(private readonly userRepository: IUserRepository) {} | ||
|
||
async execute(input: GetUserByIdInput): Promise<GetUserByIdOutput> { | ||
const user = await this.userRepository.findById(input.id); | ||
|
||
if (!user) { | ||
throw new ResourceNotFoundError("User"); | ||
} | ||
|
||
return { | ||
user, | ||
}; | ||
} | ||
} |