refactor racing use cases
This commit is contained in:
@@ -1,25 +1,60 @@
|
||||
import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
|
||||
import { GetLeagueSeasonsUseCase } from './GetLeagueSeasonsUseCase';
|
||||
import { ISeasonRepository } from '../../domain/repositories/ISeasonRepository';
|
||||
import { describe, it, expect, beforeEach, vi, type Mock } from 'vitest';
|
||||
import {
|
||||
GetLeagueSeasonsUseCase,
|
||||
type GetLeagueSeasonsInput,
|
||||
type GetLeagueSeasonsResult,
|
||||
type GetLeagueSeasonsErrorCode,
|
||||
} from './GetLeagueSeasonsUseCase';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type { ISeasonRepository } from '../../domain/repositories/ISeasonRepository';
|
||||
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
|
||||
import { Season } from '../../domain/entities/Season';
|
||||
import { League } from '../../domain/entities/League';
|
||||
|
||||
describe('GetLeagueSeasonsUseCase', () => {
|
||||
let useCase: GetLeagueSeasonsUseCase;
|
||||
let seasonRepository: {
|
||||
findByLeagueId: Mock;
|
||||
};
|
||||
let leagueRepository: {
|
||||
findById: Mock;
|
||||
};
|
||||
let output: UseCaseOutputPort<GetLeagueSeasonsResult> & {
|
||||
present: Mock;
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
seasonRepository = {
|
||||
findByLeagueId: vi.fn(),
|
||||
} as unknown as ISeasonRepository as any;
|
||||
|
||||
leagueRepository = {
|
||||
findById: vi.fn(),
|
||||
} as unknown as ILeagueRepository as any;
|
||||
|
||||
output = {
|
||||
present: vi.fn(),
|
||||
} as unknown as UseCaseOutputPort<GetLeagueSeasonsResult> & {
|
||||
present: Mock;
|
||||
};
|
||||
|
||||
useCase = new GetLeagueSeasonsUseCase(
|
||||
seasonRepository as unknown as ISeasonRepository,
|
||||
leagueRepository as unknown as ILeagueRepository,
|
||||
output,
|
||||
);
|
||||
});
|
||||
|
||||
it('should return seasons mapped to view model', async () => {
|
||||
it('should present seasons with correct isParallelActive flags on success', async () => {
|
||||
const leagueId = 'league-1';
|
||||
const league = League.create({
|
||||
id: leagueId,
|
||||
name: 'Test League',
|
||||
description: 'A test league',
|
||||
ownerId: 'owner-1',
|
||||
});
|
||||
|
||||
const seasons = [
|
||||
Season.create({
|
||||
id: 'season-1',
|
||||
@@ -39,37 +74,38 @@ describe('GetLeagueSeasonsUseCase', () => {
|
||||
}),
|
||||
];
|
||||
|
||||
leagueRepository.findById.mockResolvedValue(league);
|
||||
seasonRepository.findByLeagueId.mockResolvedValue(seasons);
|
||||
|
||||
const result = await useCase.execute({ leagueId });
|
||||
const result = await useCase.execute({ leagueId } satisfies GetLeagueSeasonsInput);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toEqual({
|
||||
seasons: [
|
||||
{
|
||||
seasonId: 'season-1',
|
||||
name: 'Season 1',
|
||||
status: 'active',
|
||||
startDate: new Date('2023-01-01'),
|
||||
endDate: new Date('2023-12-31'),
|
||||
isPrimary: false,
|
||||
isParallelActive: false, // only one active
|
||||
},
|
||||
{
|
||||
seasonId: 'season-2',
|
||||
name: 'Season 2',
|
||||
status: 'planned',
|
||||
startDate: undefined,
|
||||
endDate: undefined,
|
||||
isPrimary: false,
|
||||
isParallelActive: false,
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
const presented = output.present.mock.calls[0][0] as GetLeagueSeasonsResult;
|
||||
|
||||
expect(presented.league).toBe(league);
|
||||
expect(presented.seasons).toHaveLength(2);
|
||||
|
||||
expect(presented.seasons[0]!.season).toBe(seasons[0]);
|
||||
expect(presented.seasons[0]!.isPrimary).toBe(false);
|
||||
expect(presented.seasons[0]!.isParallelActive).toBe(false);
|
||||
|
||||
expect(presented.seasons[1]!.season).toBe(seasons[1]);
|
||||
expect(presented.seasons[1]!.isPrimary).toBe(false);
|
||||
expect(presented.seasons[1]!.isParallelActive).toBe(false);
|
||||
});
|
||||
|
||||
it('should set isParallelActive true for active seasons when multiple active', async () => {
|
||||
const leagueId = 'league-1';
|
||||
const league = League.create({
|
||||
id: leagueId,
|
||||
name: 'Test League',
|
||||
description: 'A test league',
|
||||
ownerId: 'owner-1',
|
||||
});
|
||||
|
||||
const seasons = [
|
||||
Season.create({
|
||||
id: 'season-1',
|
||||
@@ -87,27 +123,56 @@ describe('GetLeagueSeasonsUseCase', () => {
|
||||
}),
|
||||
];
|
||||
|
||||
leagueRepository.findById.mockResolvedValue(league);
|
||||
seasonRepository.findByLeagueId.mockResolvedValue(seasons);
|
||||
|
||||
const result = await useCase.execute({ leagueId });
|
||||
const result = await useCase.execute({ leagueId } satisfies GetLeagueSeasonsInput);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
const viewModel = result.unwrap();
|
||||
expect(viewModel.seasons).toHaveLength(2);
|
||||
expect(viewModel.seasons[0]!.isParallelActive).toBe(true);
|
||||
expect(viewModel.seasons[1]!.isParallelActive).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
const presented = output.present.mock.calls[0][0] as GetLeagueSeasonsResult;
|
||||
|
||||
expect(presented.seasons).toHaveLength(2);
|
||||
expect(presented.seasons[0]!.isParallelActive).toBe(true);
|
||||
expect(presented.seasons[1]!.isParallelActive).toBe(true);
|
||||
});
|
||||
|
||||
it('should return error when repository fails', async () => {
|
||||
const leagueId = 'league-1';
|
||||
seasonRepository.findByLeagueId.mockRejectedValue(new Error('DB error'));
|
||||
it('should return LEAGUE_NOT_FOUND error when league does not exist', async () => {
|
||||
const leagueId = 'missing-league';
|
||||
|
||||
const result = await useCase.execute({ leagueId });
|
||||
leagueRepository.findById.mockResolvedValue(null);
|
||||
|
||||
const result = await useCase.execute({ leagueId } satisfies GetLeagueSeasonsInput);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr()).toEqual({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
message: 'Failed to fetch seasons',
|
||||
});
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<
|
||||
GetLeagueSeasonsErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
|
||||
expect(err.code).toBe('LEAGUE_NOT_FOUND');
|
||||
expect(err.details.message).toBe('League not found');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should return REPOSITORY_ERROR when repository throws', async () => {
|
||||
const leagueId = 'league-1';
|
||||
const errorMessage = 'DB error';
|
||||
|
||||
leagueRepository.findById.mockRejectedValue(new Error(errorMessage));
|
||||
|
||||
const result = await useCase.execute({ leagueId } satisfies GetLeagueSeasonsInput);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<
|
||||
GetLeagueSeasonsErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
|
||||
expect(err.code).toBe('REPOSITORY_ERROR');
|
||||
expect(err.details.message).toBe(errorMessage);
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user