133 lines
3.8 KiB
TypeScript
133 lines
3.8 KiB
TypeScript
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 type { CompleteDriverOnboardingCommand } from '../dto/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().code).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().code).toBe('UNKNOWN_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,
|
|
})
|
|
);
|
|
});
|
|
}); |