Skip to content

Commit

Permalink
test: ensure that a wallet is created successful
Browse files Browse the repository at this point in the history
  • Loading branch information
patricks-js committed Nov 23, 2024
1 parent 8189f90 commit c97ca79
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
50 changes: 50 additions & 0 deletions src/application/use-cases/create-wallet.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Currencies } from "@/domain/entities/wallet";
import type { IWalletRepository } from "@/domain/repositories/wallet-repository";
import { InMemoryWalletRepository } from "@/infra/repositories/in-memory/in-memory-wallet-repository";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { CreateWalletUseCase } from "./create-wallet";

describe("Use Case -> Create Wallet", () => {
let walletRepository: IWalletRepository;
let sut: CreateWalletUseCase;

beforeEach(() => {
walletRepository = new InMemoryWalletRepository();
sut = new CreateWalletUseCase(walletRepository);
});

afterEach(() => {
vi.resetAllMocks();
});

it("should create a wallet to a user", async () => {
walletRepository.findByUserId = vi.fn().mockResolvedValue(null);

const { wallet } = await sut.execute({
currency: Currencies.BRL,
userId: "existent_id",
});

expect(wallet).toBeDefined();
expect(wallet).toEqual(
expect.objectContaining({
id: expect.any(String),
}),
);
});

it("should throw an error if wallet already exists for user", async () => {
walletRepository.findByUserId = vi.fn().mockResolvedValueOnce({
id: "id",
userId: "existent_id",
balance: "0.00",
currency: Currencies.BRL,
createdAt: new Date(),
updatedAt: new Date(),
});

await expect(
sut.execute({ currency: Currencies.BRL, userId: "existent_id" }),
).rejects.toThrow("Wallet already exists.");
});
});
13 changes: 1 addition & 12 deletions src/application/use-cases/create-wallet.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { ResourceNotFoundError } from "@/application/errors/resource-not-found";
import type { ValidateUserExists } from "@/application/use-cases/validate-user-exists";
import type { CreateWalletParams, Wallet } from "@/domain/entities/wallet";
import type { UseCase } from "@/domain/interfaces/use-case";
import type { IWalletRepository } from "@/domain/repositories/wallet-repository";
Expand All @@ -13,18 +11,9 @@ export type CreateWalletOutput = {
export class CreateWalletUseCase
implements UseCase<CreateWalletInput, CreateWalletOutput>
{
constructor(
private readonly walletRepository: IWalletRepository,
private readonly validateUserExists: ValidateUserExists,
) {}
constructor(private readonly walletRepository: IWalletRepository) {}

async execute(input: CreateWalletParams): Promise<CreateWalletOutput> {
const userExists = await this.validateUserExists.execute({
field: "id",
value: input.userId,
});
if (!userExists) throw new ResourceNotFoundError("User");

const walletExists = await this.walletRepository.findByUserId(input.userId);
if (walletExists) throw new Error("Wallet already exists.");

Expand Down

0 comments on commit c97ca79

Please sign in to comment.