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,21 +45,14 @@ describe('GetAllRacesUseCase', () => {
|
||||
error: vi.fn(),
|
||||
};
|
||||
|
||||
let output: UseCaseOutputPort<GetAllRacesResult> & { present: ReturnType<typeof vi.fn> };
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
output = {
|
||||
present: vi.fn(),
|
||||
} as unknown as UseCaseOutputPort<GetAllRacesResult> & { present: ReturnType<typeof vi.fn> };
|
||||
});
|
||||
});
|
||||
|
||||
it('should present domain races and leagues data', async () => {
|
||||
const useCase = new GetAllRacesUseCase(
|
||||
mockRaceRepo,
|
||||
const useCase = new GetAllRacesUseCase(mockRaceRepo,
|
||||
mockLeagueRepo,
|
||||
mockLogger,
|
||||
);
|
||||
mockLogger);
|
||||
useCase.setOutput(output);
|
||||
|
||||
const race1 = Race.create({
|
||||
@@ -104,20 +96,15 @@ describe('GetAllRacesUseCase', () => {
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
|
||||
const presented = output.present.mock.calls[0]?.[0] as GetAllRacesResult;
|
||||
expect(presented.totalCount).toBe(2);
|
||||
const presented = expect(presented.totalCount).toBe(2);
|
||||
expect(presented.races).toEqual([race1, race2]);
|
||||
expect(presented.leagues).toEqual([league1, league2]);
|
||||
});
|
||||
|
||||
it('should present empty result when no races or leagues', async () => {
|
||||
const useCase = new GetAllRacesUseCase(
|
||||
mockRaceRepo,
|
||||
const useCase = new GetAllRacesUseCase(mockRaceRepo,
|
||||
mockLeagueRepo,
|
||||
mockLogger,
|
||||
);
|
||||
mockLogger);
|
||||
useCase.setOutput(output);
|
||||
|
||||
mockRaceFindAll.mockResolvedValue([]);
|
||||
@@ -127,20 +114,15 @@ describe('GetAllRacesUseCase', () => {
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
|
||||
const presented = output.present.mock.calls[0]?.[0] as GetAllRacesResult;
|
||||
expect(presented.totalCount).toBe(0);
|
||||
const presented = expect(presented.totalCount).toBe(0);
|
||||
expect(presented.races).toEqual([]);
|
||||
expect(presented.leagues).toEqual([]);
|
||||
});
|
||||
|
||||
it('should return error when repository throws and not present data', async () => {
|
||||
const useCase = new GetAllRacesUseCase(
|
||||
mockRaceRepo,
|
||||
const useCase = new GetAllRacesUseCase(mockRaceRepo,
|
||||
mockLeagueRepo,
|
||||
mockLogger,
|
||||
);
|
||||
mockLogger);
|
||||
|
||||
const error = new Error('Repository error');
|
||||
mockRaceFindAll.mockRejectedValue(error);
|
||||
@@ -151,6 +133,5 @@ describe('GetAllRacesUseCase', () => {
|
||||
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