Files
gridpilot.gg/core/racing/application/use-cases/GetTotalDriversUseCase.test.ts
2026-01-16 19:38:55 +01:00

53 lines
1.5 KiB
TypeScript

import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
import {
GetTotalDriversUseCase,
GetTotalDriversInput,
GetTotalDriversErrorCode,
} from './GetTotalDriversUseCase';
import { DriverRepository } from '../../domain/repositories/DriverRepository';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
describe('GetTotalDriversUseCase', () => {
let useCase: GetTotalDriversUseCase;
let driverRepository: {
findAll: Mock;
};
beforeEach(() => {
driverRepository = {
findAll: vi.fn(),
};
useCase = new GetTotalDriversUseCase(driverRepository as unknown as DriverRepository);
});
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);
});
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);
});
});