refactor use cases
This commit is contained in:
@@ -7,14 +7,12 @@ import {
|
||||
} 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;
|
||||
|
||||
@@ -24,10 +22,6 @@ describe('UpdateDriverProfileUseCase', () => {
|
||||
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(),
|
||||
@@ -35,7 +29,7 @@ describe('UpdateDriverProfileUseCase', () => {
|
||||
error: vi.fn(),
|
||||
} as unknown as Logger & { error: ReturnType<typeof vi.fn> };
|
||||
|
||||
useCase = new UpdateDriverProfileUseCase(driverRepository, logger, output);
|
||||
useCase = new UpdateDriverProfileUseCase(driverRepository, logger);
|
||||
});
|
||||
|
||||
it('updates driver profile successfully', async () => {
|
||||
@@ -65,9 +59,7 @@ describe('UpdateDriverProfileUseCase', () => {
|
||||
});
|
||||
expect(driverRepository.update).toHaveBeenCalled();
|
||||
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
const presentedRaw = output.present.mock.calls[0]?.[0];
|
||||
expect(presentedRaw).toBeDefined();
|
||||
const presentedRaw = expect(presentedRaw).toBeDefined();
|
||||
expect(presentedRaw).toEqual({ id: 'driver-1' });
|
||||
});
|
||||
|
||||
@@ -86,8 +78,7 @@ describe('UpdateDriverProfileUseCase', () => {
|
||||
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 = {
|
||||
@@ -103,8 +94,7 @@ describe('UpdateDriverProfileUseCase', () => {
|
||||
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 = {
|
||||
@@ -124,8 +114,7 @@ describe('UpdateDriverProfileUseCase', () => {
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
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 = {
|
||||
@@ -149,7 +138,6 @@ describe('UpdateDriverProfileUseCase', () => {
|
||||
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