refactor use cases
This commit is contained in:
@@ -7,7 +7,6 @@ import {
|
||||
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
|
||||
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
|
||||
import type { Logger } from '@core/shared/application';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
import { Race } from '../../domain/entities/Race';
|
||||
import { League } from '../../domain/entities/League';
|
||||
|
||||
@@ -46,22 +45,14 @@ describe('GetAllRacesPageDataUseCase', () => {
|
||||
error: vi.fn(),
|
||||
};
|
||||
|
||||
let output: UseCaseOutputPort<GetAllRacesPageDataResult> & { present: ReturnType<typeof vi.fn> };
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
output = {
|
||||
present: vi.fn(),
|
||||
} as unknown as UseCaseOutputPort<GetAllRacesPageDataResult> & { present: ReturnType<typeof vi.fn> };
|
||||
});
|
||||
});
|
||||
|
||||
it('should present races and filters data', async () => {
|
||||
const useCase = new GetAllRacesPageDataUseCase(
|
||||
mockRaceRepo,
|
||||
const useCase = new GetAllRacesPageDataUseCase(mockRaceRepo,
|
||||
mockLeagueRepo,
|
||||
mockLogger,
|
||||
output,
|
||||
);
|
||||
mockLogger);
|
||||
|
||||
const race1 = Race.create({
|
||||
id: 'race1',
|
||||
@@ -105,11 +96,7 @@ describe('GetAllRacesPageDataUseCase', () => {
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
|
||||
const presented = output.present.mock.calls[0]?.[0] as GetAllRacesPageDataResult;
|
||||
|
||||
expect(presented.races).toEqual([
|
||||
const presented = expect(presented.races).toEqual([
|
||||
{
|
||||
id: 'race2',
|
||||
track: 'Track B',
|
||||
@@ -148,12 +135,9 @@ describe('GetAllRacesPageDataUseCase', () => {
|
||||
});
|
||||
|
||||
it('should present empty result when no races or leagues', async () => {
|
||||
const useCase = new GetAllRacesPageDataUseCase(
|
||||
mockRaceRepo,
|
||||
const useCase = new GetAllRacesPageDataUseCase(mockRaceRepo,
|
||||
mockLeagueRepo,
|
||||
mockLogger,
|
||||
output,
|
||||
);
|
||||
mockLogger);
|
||||
|
||||
mockRaceFindAll.mockResolvedValue([]);
|
||||
mockLeagueFindAll.mockResolvedValue([]);
|
||||
@@ -162,11 +146,7 @@ describe('GetAllRacesPageDataUseCase', () => {
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
|
||||
const presented = output.present.mock.calls[0]?.[0] as GetAllRacesPageDataResult;
|
||||
|
||||
expect(presented.races).toEqual([]);
|
||||
const presented = expect(presented.races).toEqual([]);
|
||||
expect(presented.filters).toEqual({
|
||||
statuses: [
|
||||
{ value: 'all', label: 'All Statuses' },
|
||||
@@ -180,12 +160,9 @@ describe('GetAllRacesPageDataUseCase', () => {
|
||||
});
|
||||
|
||||
it('should return error when repository throws and not present data', async () => {
|
||||
const useCase = new GetAllRacesPageDataUseCase(
|
||||
mockRaceRepo,
|
||||
const useCase = new GetAllRacesPageDataUseCase(mockRaceRepo,
|
||||
mockLeagueRepo,
|
||||
mockLogger,
|
||||
output,
|
||||
);
|
||||
mockLogger);
|
||||
|
||||
const error = new Error('Repository error');
|
||||
mockRaceFindAll.mockRejectedValue(error);
|
||||
@@ -196,6 +173,5 @@ describe('GetAllRacesPageDataUseCase', () => {
|
||||
const err = result.unwrapErr();
|
||||
expect(err.code).toBe('REPOSITORY_ERROR');
|
||||
expect(err.details.message).toBe('Repository error');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user