refactor use cases
This commit is contained in:
@@ -7,7 +7,6 @@ import {
|
||||
} from './GetLeagueStatsUseCase';
|
||||
import { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
|
||||
import { IRaceRepository } from '../../domain/repositories/IRaceRepository';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
describe('GetLeagueStatsUseCase', () => {
|
||||
@@ -19,8 +18,6 @@ describe('GetLeagueStatsUseCase', () => {
|
||||
findByLeagueId: Mock;
|
||||
};
|
||||
let getDriverRating: Mock;
|
||||
let output: UseCaseOutputPort<GetLeagueStatsResult> & { present: Mock };
|
||||
|
||||
beforeEach(() => {
|
||||
leagueMembershipRepository = {
|
||||
getLeagueMembers: vi.fn(),
|
||||
@@ -29,16 +26,9 @@ describe('GetLeagueStatsUseCase', () => {
|
||||
findByLeagueId: vi.fn(),
|
||||
};
|
||||
getDriverRating = vi.fn();
|
||||
output = {
|
||||
present: vi.fn(),
|
||||
} as unknown as UseCaseOutputPort<GetLeagueStatsResult> & { present: Mock };
|
||||
|
||||
useCase = new GetLeagueStatsUseCase(
|
||||
leagueMembershipRepository as unknown as ILeagueMembershipRepository,
|
||||
useCase = new GetLeagueStatsUseCase(leagueMembershipRepository as unknown as ILeagueMembershipRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
getDriverRating,
|
||||
output,
|
||||
);
|
||||
getDriverRating);
|
||||
});
|
||||
|
||||
it('should return league stats with average rating', async () => {
|
||||
@@ -63,11 +53,7 @@ describe('GetLeagueStatsUseCase', () => {
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
|
||||
const presented = (output.present as Mock).mock.calls[0]?.[0] as GetLeagueStatsResult;
|
||||
|
||||
expect(presented.leagueId).toBe(input.leagueId);
|
||||
const presented = (expect(presented.leagueId).toBe(input.leagueId);
|
||||
expect(presented.driverCount).toBe(3);
|
||||
expect(presented.raceCount).toBe(2);
|
||||
expect(presented.averageRating).toBe(1550); // (1500 + 1600) / 2
|
||||
@@ -86,11 +72,7 @@ describe('GetLeagueStatsUseCase', () => {
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
|
||||
const presented = (output.present as Mock).mock.calls[0]?.[0] as GetLeagueStatsResult;
|
||||
|
||||
expect(presented.leagueId).toBe(input.leagueId);
|
||||
const presented = (expect(presented.leagueId).toBe(input.leagueId);
|
||||
expect(presented.driverCount).toBe(1);
|
||||
expect(presented.raceCount).toBe(1);
|
||||
expect(presented.averageRating).toBe(0);
|
||||
@@ -110,8 +92,7 @@ describe('GetLeagueStatsUseCase', () => {
|
||||
>;
|
||||
expect(error.code).toBe('LEAGUE_NOT_FOUND');
|
||||
expect(error.details.message).toBe('League not found');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('should return error when repository fails', async () => {
|
||||
const input: GetLeagueStatsInput = { leagueId: 'league-1' };
|
||||
@@ -126,6 +107,5 @@ describe('GetLeagueStatsUseCase', () => {
|
||||
>;
|
||||
expect(error.code).toBe('REPOSITORY_ERROR');
|
||||
expect(error.details.message).toBe('DB error');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user