61 lines
1.8 KiB
TypeScript
61 lines
1.8 KiB
TypeScript
import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
|
|
import {
|
|
GetTotalDriversUseCase,
|
|
GetTotalDriversInput,
|
|
GetTotalDriversErrorCode,
|
|
GetTotalDriversResult,
|
|
} from './GetTotalDriversUseCase';
|
|
import { IDriverRepository } from '../../domain/repositories/IDriverRepository';
|
|
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
|
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
|
|
|
describe('GetTotalDriversUseCase', () => {
|
|
let useCase: GetTotalDriversUseCase;
|
|
let driverRepository: {
|
|
findAll: Mock;
|
|
};
|
|
let output: UseCaseOutputPort<GetTotalDriversResult>;
|
|
beforeEach(() => {
|
|
driverRepository = {
|
|
findAll: vi.fn(),
|
|
};
|
|
output = {
|
|
present: vi.fn(),
|
|
};
|
|
|
|
useCase = new GetTotalDriversUseCase(driverRepository as unknown as IDriverRepository, output);
|
|
});
|
|
|
|
it('should return total number of drivers', async () => {
|
|
const drivers = [{ id: '1' }, { id: '2' }];
|
|
|
|
driverRepository.findAll.mockResolvedValue(drivers);
|
|
|
|
const input: GetTotalDriversInput = {};
|
|
|
|
const result = await useCase.execute(input);
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
expect(output.present).toHaveBeenCalledWith({ totalDrivers: 2 });
|
|
});
|
|
|
|
it('should return error on repository failure', async () => {
|
|
const error = new Error('Repository error');
|
|
|
|
driverRepository.findAll.mockRejectedValue(error);
|
|
|
|
const input: GetTotalDriversInput = {};
|
|
|
|
const result = await useCase.execute(input);
|
|
|
|
expect(result.isErr()).toBe(true);
|
|
|
|
const unwrappedError = result.unwrapErr() as ApplicationErrorCode<
|
|
GetTotalDriversErrorCode,
|
|
{ message: string }
|
|
>;
|
|
|
|
expect(unwrappedError.code).toBe('REPOSITORY_ERROR');
|
|
expect(unwrappedError.details.message).toBe(error.message);
|
|
});
|
|
}); |