diff --git a/src/application/use-cases/add-money-to-wallet.spec.ts b/src/application/use-cases/add-money-to-wallet.spec.ts new file mode 100644 index 0000000..f91c31b --- /dev/null +++ b/src/application/use-cases/add-money-to-wallet.spec.ts @@ -0,0 +1,62 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; + +import { ResourceNotFoundError } from "@/application/errors/resource-not-found"; +import { AddMoneyToWalletUseCase } from "@/application/use-cases/add-money-to-wallet"; +import { Currencies } from "@/domain/entities/wallet"; +import { InMemoryWalletRepository } from "@/infra/repositories/in-memory/in-memory-wallet-repository"; + +describe("Use Case -> Add Money To Wallet Use Case", () => { + let sut: AddMoneyToWalletUseCase; + let walletId: string; + + beforeEach(async () => { + const walletRepository = new InMemoryWalletRepository(); + sut = new AddMoneyToWalletUseCase(walletRepository); + + const { id } = await walletRepository.create({ + currency: Currencies.BRL, + userId: "user_id", + }); + + walletId = id; + }); + + afterEach(() => { + vi.resetAllMocks(); + }); + + it("should be able to add money into the wallet", async () => { + const spy = vi.spyOn(sut, "execute"); + const amount = 763.13; + + await sut.execute({ + amount, + walletId, + }); + + expect(spy).toHaveBeenCalledOnce(); + }); + + it("should throw an error if amount is less or equal than 0", async () => { + const amount = -763.13; + + await expect( + sut.execute({ + amount, + walletId, + }), + ).rejects.toThrow("Invalid operation"); + }); + + it("should throw an error if wallet doesn't exits", async () => { + const amount = 763.13; + const walletId = "non_existent"; + + await expect( + sut.execute({ + amount, + walletId, + }), + ).rejects.toThrow(ResourceNotFoundError); + }); +}); diff --git a/src/application/use-cases/add-money-to-wallet.ts b/src/application/use-cases/add-money-to-wallet.ts index 5615ebb..54e80c9 100644 --- a/src/application/use-cases/add-money-to-wallet.ts +++ b/src/application/use-cases/add-money-to-wallet.ts @@ -2,32 +2,19 @@ import type { UseCase } from "@/domain/interfaces/use-case"; import type { IWalletRepository } from "@/domain/repositories/wallet-repository"; import { addBalance } from "@/utils/balance-helper"; import { ResourceNotFoundError } from "../errors/resource-not-found"; -import type { ValidateUserExists } from "./validate-user-exists"; interface AddMoneyToWalletInput { amount: number; - userId: string; + walletId: string; } export class AddMoneyToWalletUseCase implements UseCase { - constructor( - private readonly walletRepository: IWalletRepository, - private readonly validateUserExists: ValidateUserExists, - ) {} + constructor(private readonly walletRepository: IWalletRepository) {} async execute(input: AddMoneyToWalletInput): Promise { - const userExists = await this.validateUserExists.execute({ - field: "id", - value: input.userId, - }); - - if (!userExists) { - throw new ResourceNotFoundError("User"); - } - - const wallet = await this.walletRepository.findByUserId(input.userId); + const wallet = await this.walletRepository.findById(input.walletId); if (!wallet) { throw new ResourceNotFoundError("Wallet");