refactor racing use cases
This commit is contained in:
@@ -1,29 +1,53 @@
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
import { UpdateDriverProfileUseCase } from './UpdateDriverProfileUseCase';
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import {
|
||||
UpdateDriverProfileUseCase,
|
||||
type UpdateDriverProfileInput,
|
||||
type UpdateDriverProfileResult,
|
||||
type UpdateDriverProfileErrorCode,
|
||||
} from './UpdateDriverProfileUseCase';
|
||||
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
|
||||
import type { Driver } from '../../domain/entities/Driver';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { Logger } from '@core/shared/application/Logger';
|
||||
|
||||
describe('UpdateDriverProfileUseCase', () => {
|
||||
let driverRepository: IDriverRepository;
|
||||
let output: UseCaseOutputPort<UpdateDriverProfileResult> & { present: ReturnType<typeof vi.fn> };
|
||||
let logger: Logger & { error: ReturnType<typeof vi.fn> };
|
||||
let useCase: UpdateDriverProfileUseCase;
|
||||
|
||||
beforeEach(() => {
|
||||
driverRepository = {
|
||||
findById: vi.fn(),
|
||||
update: vi.fn(),
|
||||
} as unknown as IDriverRepository;
|
||||
|
||||
output = {
|
||||
present: vi.fn(),
|
||||
} as unknown as UseCaseOutputPort<UpdateDriverProfileResult> & { present: ReturnType<typeof vi.fn> };
|
||||
|
||||
logger = {
|
||||
debug: vi.fn(),
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
} as unknown as Logger & { error: ReturnType<typeof vi.fn> };
|
||||
|
||||
useCase = new UpdateDriverProfileUseCase(driverRepository, output, logger);
|
||||
});
|
||||
|
||||
it('updates driver profile successfully', async () => {
|
||||
const mockDriver = {
|
||||
id: 'driver-1',
|
||||
update: vi.fn().mockReturnValue({}),
|
||||
update: vi.fn().mockReturnValue({ id: 'driver-1' } as Driver),
|
||||
} as unknown as Driver;
|
||||
|
||||
const mockUpdatedDriver = {
|
||||
id: 'driver-1',
|
||||
bio: 'New bio',
|
||||
country: 'US',
|
||||
} as Driver;
|
||||
(driverRepository.findById as unknown as ReturnType<typeof vi.fn>).mockResolvedValue(mockDriver);
|
||||
(driverRepository.update as unknown as ReturnType<typeof vi.fn>).mockResolvedValue(mockDriver);
|
||||
|
||||
const mockDriverRepository = {
|
||||
findById: vi.fn().mockResolvedValue(mockDriver),
|
||||
update: vi.fn().mockResolvedValue(mockUpdatedDriver),
|
||||
} as unknown as IDriverRepository;
|
||||
|
||||
const useCase = new UpdateDriverProfileUseCase(mockDriverRepository);
|
||||
|
||||
const input = {
|
||||
const input: UpdateDriverProfileInput = {
|
||||
driverId: 'driver-1',
|
||||
bio: 'New bio',
|
||||
country: 'US',
|
||||
@@ -32,49 +56,64 @@ describe('UpdateDriverProfileUseCase', () => {
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toEqual(mockUpdatedDriver);
|
||||
expect(mockDriverRepository.findById).toHaveBeenCalledWith('driver-1');
|
||||
expect(mockDriver.update).toHaveBeenCalledWith({ bio: 'New bio', country: 'US' });
|
||||
expect(mockDriverRepository.update).toHaveBeenCalledWith({});
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
|
||||
expect(driverRepository.findById).toHaveBeenCalledWith('driver-1');
|
||||
expect((mockDriver.update as unknown as ReturnType<typeof vi.fn>)).toHaveBeenCalledWith({
|
||||
bio: 'New bio',
|
||||
country: 'US',
|
||||
});
|
||||
expect(driverRepository.update).toHaveBeenCalled();
|
||||
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
expect(output.present).toHaveBeenCalledWith({ driverId: 'driver-1' });
|
||||
});
|
||||
|
||||
it('returns error when driver not found', async () => {
|
||||
const mockDriverRepository = {
|
||||
findById: vi.fn().mockResolvedValue(null),
|
||||
} as unknown as IDriverRepository;
|
||||
(driverRepository.findById as unknown as ReturnType<typeof vi.fn>).mockResolvedValue(null);
|
||||
|
||||
const useCase = new UpdateDriverProfileUseCase(mockDriverRepository);
|
||||
|
||||
const input = {
|
||||
const input: UpdateDriverProfileInput = {
|
||||
driverId: 'driver-1',
|
||||
bio: 'New bio',
|
||||
};
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
const result: Result<void, ApplicationErrorCode<UpdateDriverProfileErrorCode, { message: string }>> =
|
||||
await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr()).toEqual({ code: 'DRIVER_NOT_FOUND' });
|
||||
const error = result.unwrapErr();
|
||||
expect(error.code).toBe('DRIVER_NOT_FOUND');
|
||||
expect(error.details?.message).toContain('driver-1');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns error for invalid profile data', async () => {
|
||||
const input: UpdateDriverProfileInput = {
|
||||
driverId: 'driver-1',
|
||||
bio: '',
|
||||
};
|
||||
|
||||
const result: Result<void, ApplicationErrorCode<UpdateDriverProfileErrorCode, { message: string }>> =
|
||||
await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr();
|
||||
expect(error.code).toBe('INVALID_PROFILE_DATA');
|
||||
expect(error.details?.message).toBe('Profile data is invalid');
|
||||
expect(driverRepository.findById).not.toHaveBeenCalled();
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('updates only provided fields', async () => {
|
||||
const mockDriver = {
|
||||
id: 'driver-1',
|
||||
update: vi.fn().mockReturnValue({}),
|
||||
update: vi.fn().mockReturnValue({ id: 'driver-1' } as Driver),
|
||||
} as unknown as Driver;
|
||||
|
||||
const mockUpdatedDriver = {
|
||||
id: 'driver-1',
|
||||
country: 'US',
|
||||
} as Driver;
|
||||
(driverRepository.findById as unknown as ReturnType<typeof vi.fn>).mockResolvedValue(mockDriver);
|
||||
(driverRepository.update as unknown as ReturnType<typeof vi.fn>).mockResolvedValue(mockDriver);
|
||||
|
||||
const mockDriverRepository = {
|
||||
findById: vi.fn().mockResolvedValue(mockDriver),
|
||||
update: vi.fn().mockResolvedValue(mockUpdatedDriver),
|
||||
} as unknown as IDriverRepository;
|
||||
|
||||
const useCase = new UpdateDriverProfileUseCase(mockDriverRepository);
|
||||
|
||||
const input = {
|
||||
const input: UpdateDriverProfileInput = {
|
||||
driverId: 'driver-1',
|
||||
country: 'US',
|
||||
};
|
||||
@@ -82,6 +121,33 @@ describe('UpdateDriverProfileUseCase', () => {
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(mockDriver.update).toHaveBeenCalledWith({ country: 'US' });
|
||||
expect((mockDriver.update as unknown as ReturnType<typeof vi.fn>)).toHaveBeenCalledWith({ country: 'US' });
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('returns repository error when persistence fails', async () => {
|
||||
const mockDriver = {
|
||||
id: 'driver-1',
|
||||
update: vi.fn().mockReturnValue({ id: 'driver-1' } as Driver),
|
||||
} as unknown as Driver;
|
||||
|
||||
(driverRepository.findById as unknown as ReturnType<typeof vi.fn>).mockResolvedValue(mockDriver);
|
||||
(driverRepository.update as unknown as ReturnType<typeof vi.fn>).mockRejectedValue(new Error('db error'));
|
||||
|
||||
const input: UpdateDriverProfileInput = {
|
||||
driverId: 'driver-1',
|
||||
bio: 'Bio',
|
||||
};
|
||||
|
||||
const result: Result<void, ApplicationErrorCode<UpdateDriverProfileErrorCode, { message: string }>> =
|
||||
await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr();
|
||||
expect(error.code).toBe('REPOSITORY_ERROR');
|
||||
expect(error.details?.message).toBe('db error');
|
||||
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
expect(logger.error).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user