fix issues in core
This commit is contained in:
@@ -5,6 +5,7 @@ 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';
|
||||
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
|
||||
|
||||
vi.mock('../../domain/value-objects/PasswordHash', () => ({
|
||||
PasswordHash: {
|
||||
@@ -20,6 +21,8 @@ describe('SignupUseCase', () => {
|
||||
let passwordService: {
|
||||
hash: Mock;
|
||||
};
|
||||
let logger: Logger;
|
||||
let output: UseCaseOutputPort<any> & { present: Mock };
|
||||
let useCase: SignupUseCase;
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -30,42 +33,61 @@ describe('SignupUseCase', () => {
|
||||
passwordService = {
|
||||
hash: vi.fn(),
|
||||
};
|
||||
logger = {
|
||||
debug: vi.fn(),
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
} as unknown as Logger;
|
||||
output = {
|
||||
present: vi.fn(),
|
||||
};
|
||||
|
||||
useCase = new SignupUseCase(
|
||||
authRepo as unknown as IAuthRepository,
|
||||
passwordService as unknown as IPasswordHashingService,
|
||||
logger,
|
||||
output,
|
||||
);
|
||||
});
|
||||
|
||||
it('creates and saves a new user when email is free', async () => {
|
||||
const email = 'new@example.com';
|
||||
const password = 'password123';
|
||||
const displayName = 'New User';
|
||||
const input = {
|
||||
email: 'new@example.com',
|
||||
password: 'password123',
|
||||
displayName: 'New User',
|
||||
};
|
||||
|
||||
authRepo.findByEmail.mockResolvedValue(null);
|
||||
passwordService.hash.mockResolvedValue('hashed-password');
|
||||
|
||||
const result = await useCase.execute(email, password, displayName);
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(authRepo.findByEmail).toHaveBeenCalledWith(EmailAddress.create(email));
|
||||
expect(passwordService.hash).toHaveBeenCalledWith(password);
|
||||
expect(authRepo.findByEmail).toHaveBeenCalledWith(EmailAddress.create(input.email));
|
||||
expect(passwordService.hash).toHaveBeenCalledWith(input.password);
|
||||
expect(authRepo.save).toHaveBeenCalled();
|
||||
|
||||
expect(result).toBeInstanceOf(User);
|
||||
expect(result.getDisplayName()).toBe(displayName);
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(output.present).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('throws when user already exists', async () => {
|
||||
const email = 'existing@example.com';
|
||||
const input = {
|
||||
email: 'existing@example.com',
|
||||
password: 'password123',
|
||||
displayName: 'Existing User',
|
||||
};
|
||||
|
||||
const existingUser = User.create({
|
||||
id: UserId.create(),
|
||||
displayName: 'Existing User',
|
||||
email,
|
||||
email: input.email,
|
||||
});
|
||||
|
||||
authRepo.findByEmail.mockResolvedValue(existingUser);
|
||||
|
||||
await expect(useCase.execute(email, 'password', 'Existing User')).rejects.toThrow('User already exists');
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user