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

109 lines
3.4 KiB
TypeScript

import { describe, it, expect, jest, beforeEach } from "bun:test";
import AccountService from "../src/domain/account/service/AccountService";
import { AccountEntity } from "../src/domain/account/entity/AccountEntity";
import {
AccountNotFoundError,
AccountAlreadyExistsError,
BadPasswordError,
} from "../src/domain/account/errors/AccountErrors";
describe("AccountService", () => {
let accountService: AccountService;
let mockAccountRepository: {
findByEmail: ReturnType<typeof jest.fn>;
save: ReturnType<typeof jest.fn>;
findById: ReturnType<typeof jest.fn>;
};
beforeEach(() => {
mockAccountRepository = {
findByEmail: jest.fn(() => Promise.resolve(null)),
save: jest.fn(() => Promise.resolve("123")),
findById: jest.fn(() => Promise.resolve(null)),
};
accountService = new AccountService(mockAccountRepository as any);
});
describe("createAccount", () => {
it("should create a new account successfully", async () => {
const email = "test@example.com";
const password = "password123";
mockAccountRepository.findByEmail.mockImplementation(() =>
Promise.resolve(null),
);
mockAccountRepository.save.mockImplementation(() =>
Promise.resolve("123"),
);
const result = await accountService.register(email, password);
expect(result).toBeInstanceOf(AccountEntity);
expect(result.email).toBe(email);
expect(mockAccountRepository.findByEmail).toHaveBeenCalledWith(email);
expect(mockAccountRepository.save).toHaveBeenCalledWith(
expect.any(AccountEntity),
);
});
it("should throw error if account already exists", async () => {
const email = "test@example.com";
const password = "password123";
const existingAccount = AccountEntity.create(email, password);
mockAccountRepository.findByEmail.mockImplementation(() =>
Promise.resolve(existingAccount),
);
await expect(accountService.register(email, password)).rejects.toThrow(
AccountAlreadyExistsError,
);
});
});
describe("login", () => {
it("should login successfully with correct credentials", async () => {
const email = "test@example.com";
const password = "password123";
const account = AccountEntity.create(email, password);
mockAccountRepository.findByEmail.mockImplementation(() =>
Promise.resolve(account),
);
const result = await accountService.login(email, password);
expect(result).toBe(account);
expect(mockAccountRepository.findByEmail).toHaveBeenCalledWith(email);
});
it("should throw error if account not found", async () => {
const email = "test@example.com";
const password = "password123";
mockAccountRepository.findByEmail.mockImplementation(() =>
Promise.resolve(null),
);
await expect(accountService.login(email, password)).rejects.toThrow(
AccountNotFoundError,
);
});
it("should throw error if password is incorrect", async () => {
const email = "test@example.com";
const password = "password123";
const wrongPassword = "wrongpassword";
const account = AccountEntity.create(email, password);
mockAccountRepository.findByEmail.mockImplementation(() =>
Promise.resolve(account),
);
await expect(accountService.login(email, wrongPassword)).rejects.toThrow(
BadPasswordError,
);
});
});
});