Skip to content

Commit

Permalink
test: ensure that is possible to add money to wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
patricks-js committed Nov 22, 2024
1 parent 95b08ba commit 4e35a15
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 16 deletions.
62 changes: 62 additions & 0 deletions src/application/use-cases/add-money-to-wallet.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
19 changes: 3 additions & 16 deletions src/application/use-cases/add-money-to-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AddMoneyToWalletInput, void>
{
constructor(
private readonly walletRepository: IWalletRepository,
private readonly validateUserExists: ValidateUserExists,
) {}
constructor(private readonly walletRepository: IWalletRepository) {}

async execute(input: AddMoneyToWalletInput): Promise<void> {
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");
Expand Down

0 comments on commit 4e35a15

Please sign in to comment.