Files
nixi-api/tests/AccountRepository.test.ts
2025-09-22 21:39:45 +00:00

131 lines
3.7 KiB
TypeScript

import { describe, it, expect, jest, beforeEach } from "bun:test";
import AccountRepository from "../src/domain/account/repository/AccoutRepository";
import { AccountEntity } from "../src/domain/account/entity/AccountEntity";
describe("AccountRepository", () => {
let accountRepository: AccountRepository;
let mockDatabase: {
ping: ReturnType<typeof jest.fn>;
fetchAll: ReturnType<typeof jest.fn>;
fetchOne: ReturnType<typeof jest.fn>;
execute: ReturnType<typeof jest.fn>;
};
beforeEach(() => {
mockDatabase = {
ping: jest.fn(() => Promise.resolve()),
fetchAll: jest.fn(() => Promise.resolve([])),
fetchOne: jest.fn(() => Promise.resolve(undefined)),
execute: jest.fn(() => Promise.resolve()),
};
accountRepository = new AccountRepository(mockDatabase as any);
});
describe("findByEmail", () => {
it("should return account when found", async () => {
const email = "test@example.com";
const mockResult = {
id: "123",
email: email,
password: "hashedPassword",
role_id: 1,
created_at: new Date(),
updated_at: new Date(),
};
mockDatabase.fetchOne.mockImplementation(() =>
Promise.resolve(mockResult),
);
const result = await accountRepository.findByEmail(email);
expect(result).toBeInstanceOf(AccountEntity);
expect(result?.email).toBe(email);
expect(mockDatabase.fetchOne).toHaveBeenCalledWith(
expect.stringContaining(
"SELECT id, email, password, role_id, created_at, updated_at",
),
[email],
);
});
it("should return null when account not found", async () => {
const email = "nonexistent@example.com";
mockDatabase.fetchOne.mockImplementation(() =>
Promise.resolve(undefined),
);
const result = await accountRepository.findByEmail(email);
expect(result).toBeNull();
});
});
describe("findById", () => {
it("should return account when found", async () => {
const id = "123";
const mockResult = {
id: id,
email: "test@example.com",
password: "hashedPassword",
role_id: 1,
created_at: new Date(),
updated_at: new Date(),
};
mockDatabase.fetchOne.mockImplementation(() =>
Promise.resolve(mockResult),
);
const result = await accountRepository.findById(id);
expect(result).toBeInstanceOf(AccountEntity);
expect(result?.id).toBe(id);
expect(mockDatabase.fetchOne).toHaveBeenCalledWith(
expect.stringContaining(
"SELECT id, email, password, role_id, created_at, updated_at",
),
[id],
);
});
it("should return null when account not found", async () => {
const id = "nonexistent";
mockDatabase.fetchOne.mockImplementation(() =>
Promise.resolve(undefined),
);
const result = await accountRepository.findById(id);
expect(result).toBeNull();
});
});
describe("save", () => {
it("should save account successfully", async () => {
const account = AccountEntity.create("test@example.com", "password123");
mockDatabase.fetchOne.mockImplementation(() =>
Promise.resolve({ id: account.id }),
);
const result = await accountRepository.save(account);
expect(result).toBe(account.id);
expect(mockDatabase.fetchOne).toHaveBeenCalledWith(
expect.stringContaining("INSERT INTO accounts"),
expect.arrayContaining([
account.id,
account.email,
expect.any(String),
account.roleId,
account.createdAt,
account.updatedAt,
]),
);
});
});
});