Files
gridpilot.gg/core/identity/application/use-cases/SignupUseCase.test.ts
2025-12-20 12:55:07 +01:00

72 lines
2.1 KiB
TypeScript

import { describe, it, expect, vi, type Mock } from 'vitest';
import { SignupUseCase } from './SignupUseCase';
import { EmailAddress } from '../../domain/value-objects/EmailAddress';
import { UserId } from '../../domain/value-objects/UserId';
import { User } from '../../domain/entities/User';
import type { IAuthRepository } from '../../domain/repositories/IAuthRepository';
import type { IPasswordHashingService } from '../../domain/services/PasswordHashingService';
vi.mock('../../domain/value-objects/PasswordHash', () => ({
PasswordHash: {
fromHash: (hash: string) => ({ value: hash }),
},
}));
describe('SignupUseCase', () => {
let authRepo: {
findByEmail: Mock;
save: Mock;
};
let passwordService: {
hash: Mock;
};
let useCase: SignupUseCase;
beforeEach(() => {
authRepo = {
findByEmail: vi.fn(),
save: vi.fn(),
};
passwordService = {
hash: vi.fn(),
};
useCase = new SignupUseCase(
authRepo as unknown as IAuthRepository,
passwordService as unknown as IPasswordHashingService,
);
});
it('creates and saves a new user when email is free', async () => {
const email = 'new@example.com';
const password = 'password123';
const displayName = 'New User';
authRepo.findByEmail.mockResolvedValue(null);
passwordService.hash.mockResolvedValue('hashed-password');
const result = await useCase.execute(email, password, displayName);
expect(authRepo.findByEmail).toHaveBeenCalledWith(EmailAddress.create(email));
expect(passwordService.hash).toHaveBeenCalledWith(password);
expect(authRepo.save).toHaveBeenCalled();
expect(result).toBeInstanceOf(User);
expect(result.getDisplayName()).toBe(displayName);
});
it('throws when user already exists', async () => {
const email = 'existing@example.com';
const existingUser = User.create({
id: UserId.create(),
displayName: 'Existing User',
email,
});
authRepo.findByEmail.mockResolvedValue(existingUser);
await expect(useCase.execute(email, 'password', 'Existing User')).rejects.toThrow('User already exists');
});
});