refactor
This commit is contained in:
@@ -0,0 +1,136 @@
|
||||
import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
|
||||
import { CompleteDriverOnboardingUseCase } from './CompleteDriverOnboardingUseCase';
|
||||
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
|
||||
import { Driver } from '../../domain/entities/Driver';
|
||||
import { RacingDomainValidationError } from '../../domain/errors/RacingDomainError';
|
||||
import type { CompleteDriverOnboardingCommand } from './CompleteDriverOnboardingCommand';
|
||||
|
||||
describe('CompleteDriverOnboardingUseCase', () => {
|
||||
let useCase: CompleteDriverOnboardingUseCase;
|
||||
let driverRepository: {
|
||||
findById: Mock;
|
||||
create: Mock;
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
driverRepository = {
|
||||
findById: vi.fn(),
|
||||
create: vi.fn(),
|
||||
};
|
||||
useCase = new CompleteDriverOnboardingUseCase(
|
||||
driverRepository as unknown as IDriverRepository,
|
||||
);
|
||||
});
|
||||
|
||||
it('should create driver successfully when driver does not exist', async () => {
|
||||
const command: CompleteDriverOnboardingCommand = {
|
||||
userId: 'user-1',
|
||||
firstName: 'John',
|
||||
lastName: 'Doe',
|
||||
displayName: 'John Doe',
|
||||
country: 'US',
|
||||
bio: 'Test bio',
|
||||
};
|
||||
|
||||
driverRepository.findById.mockResolvedValue(null);
|
||||
const createdDriver = Driver.create({
|
||||
id: 'user-1',
|
||||
iracingId: 'user-1',
|
||||
name: 'John Doe',
|
||||
country: 'US',
|
||||
bio: 'Test bio',
|
||||
});
|
||||
driverRepository.create.mockResolvedValue(createdDriver);
|
||||
|
||||
const result = await useCase.execute(command);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toEqual({ driverId: 'user-1' });
|
||||
expect(driverRepository.findById).toHaveBeenCalledWith('user-1');
|
||||
expect(driverRepository.create).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
id: 'user-1',
|
||||
iracingId: 'user-1',
|
||||
name: 'John Doe',
|
||||
country: 'US',
|
||||
bio: 'Test bio',
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('should return error when driver already exists', async () => {
|
||||
const command: CompleteDriverOnboardingCommand = {
|
||||
userId: 'user-1',
|
||||
firstName: 'John',
|
||||
lastName: 'Doe',
|
||||
displayName: 'John Doe',
|
||||
country: 'US',
|
||||
};
|
||||
|
||||
const existingDriver = Driver.create({
|
||||
id: 'user-1',
|
||||
iracingId: 'user-1',
|
||||
name: 'John Doe',
|
||||
country: 'US',
|
||||
});
|
||||
driverRepository.findById.mockResolvedValue(existingDriver);
|
||||
|
||||
const result = await useCase.execute(command);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr()).toBeInstanceOf(RacingDomainValidationError);
|
||||
expect(result.unwrapErr().message).toBe('Driver already exists');
|
||||
expect(driverRepository.create).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should return error when repository create throws', async () => {
|
||||
const command: CompleteDriverOnboardingCommand = {
|
||||
userId: 'user-1',
|
||||
firstName: 'John',
|
||||
lastName: 'Doe',
|
||||
displayName: 'John Doe',
|
||||
country: 'US',
|
||||
};
|
||||
|
||||
driverRepository.findById.mockResolvedValue(null);
|
||||
driverRepository.create.mockRejectedValue(new Error('DB error'));
|
||||
|
||||
const result = await useCase.execute(command);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr()).toBeInstanceOf(RacingDomainValidationError);
|
||||
expect(result.unwrapErr().message).toBe('DB error');
|
||||
});
|
||||
|
||||
it('should handle bio being undefined', async () => {
|
||||
const command: CompleteDriverOnboardingCommand = {
|
||||
userId: 'user-1',
|
||||
firstName: 'John',
|
||||
lastName: 'Doe',
|
||||
displayName: 'John Doe',
|
||||
country: 'US',
|
||||
};
|
||||
|
||||
driverRepository.findById.mockResolvedValue(null);
|
||||
const createdDriver = Driver.create({
|
||||
id: 'user-1',
|
||||
iracingId: 'user-1',
|
||||
name: 'John Doe',
|
||||
country: 'US',
|
||||
});
|
||||
driverRepository.create.mockResolvedValue(createdDriver);
|
||||
|
||||
const result = await useCase.execute(command);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(driverRepository.create).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
id: 'user-1',
|
||||
iracingId: 'user-1',
|
||||
name: 'John Doe',
|
||||
country: 'US',
|
||||
bio: undefined,
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user