refactor use cases
This commit is contained in:
@@ -8,7 +8,6 @@ import {
|
||||
import type { ISeasonRepository } from '../../domain/repositories/ISeasonRepository';
|
||||
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
|
||||
import { Season } from '../../domain/entities/season/Season';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
describe('ManageSeasonLifecycleUseCase', () => {
|
||||
@@ -20,8 +19,6 @@ describe('ManageSeasonLifecycleUseCase', () => {
|
||||
findById: Mock;
|
||||
update: Mock;
|
||||
};
|
||||
let output: UseCaseOutputPort<ManageSeasonLifecycleResult> & {
|
||||
present: Mock;
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -32,16 +29,9 @@ describe('ManageSeasonLifecycleUseCase', () => {
|
||||
findById: vi.fn(),
|
||||
update: vi.fn(),
|
||||
};
|
||||
output = {
|
||||
present: vi.fn(),
|
||||
} as unknown as UseCaseOutputPort<ManageSeasonLifecycleResult> & {
|
||||
present: Mock;
|
||||
};
|
||||
useCase = new ManageSeasonLifecycleUseCase(
|
||||
leagueRepository as unknown as ILeagueRepository,
|
||||
seasonRepository as unknown as ISeasonRepository,
|
||||
output,
|
||||
);
|
||||
useCase = new ManageSeasonLifecycleUseCase(leagueRepository as unknown as ILeagueRepository,
|
||||
seasonRepository as unknown as ISeasonRepository);
|
||||
});
|
||||
|
||||
it('applies activate → complete → archive transitions and persists state', async () => {
|
||||
@@ -70,15 +60,11 @@ describe('ManageSeasonLifecycleUseCase', () => {
|
||||
const activated = await useCase.execute(activateInput);
|
||||
expect(activated.isOk()).toBe(true);
|
||||
expect(activated.unwrap()).toBeUndefined();
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
const [firstCall] = output.present.mock.calls;
|
||||
const [firstArg] = firstCall as [ManageSeasonLifecycleResult];
|
||||
const [firstCall] = const [firstArg] = firstCall as [ManageSeasonLifecycleResult];
|
||||
let presented = firstArg;
|
||||
expect(presented.season.status.toString()).toBe('active');
|
||||
|
||||
(output.present as Mock).mockClear();
|
||||
|
||||
const completeInput: ManageSeasonLifecycleInput = {
|
||||
(const completeInput: ManageSeasonLifecycleInput = {
|
||||
leagueId: 'league-1',
|
||||
seasonId: currentSeason.id,
|
||||
transition: 'complete',
|
||||
@@ -87,16 +73,12 @@ describe('ManageSeasonLifecycleUseCase', () => {
|
||||
const completed = await useCase.execute(completeInput);
|
||||
expect(completed.isOk()).toBe(true);
|
||||
expect(completed.unwrap()).toBeUndefined();
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
{
|
||||
const [[arg]] = output.present.mock.calls as [[ManageSeasonLifecycleResult]];
|
||||
presented = arg;
|
||||
const [[arg]] = presented = arg;
|
||||
}
|
||||
expect(presented.season.status.toString()).toBe('completed');
|
||||
|
||||
(output.present as Mock).mockClear();
|
||||
|
||||
const archiveInput: ManageSeasonLifecycleInput = {
|
||||
(const archiveInput: ManageSeasonLifecycleInput = {
|
||||
leagueId: 'league-1',
|
||||
seasonId: currentSeason.id,
|
||||
transition: 'archive',
|
||||
@@ -105,10 +87,8 @@ describe('ManageSeasonLifecycleUseCase', () => {
|
||||
const archived = await useCase.execute(archiveInput);
|
||||
expect(archived.isOk()).toBe(true);
|
||||
expect(archived.unwrap()).toBeUndefined();
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
{
|
||||
const presentedRaw = output.present.mock.calls[0]?.[0];
|
||||
expect(presentedRaw).toBeDefined();
|
||||
const presentedRaw = expect(presentedRaw).toBeDefined();
|
||||
presented = presentedRaw as ManageSeasonLifecycleResult;
|
||||
}
|
||||
expect(presented.season.status.toString()).toBe('archived');
|
||||
@@ -140,8 +120,7 @@ describe('ManageSeasonLifecycleUseCase', () => {
|
||||
{ message: string }
|
||||
>;
|
||||
expect(error.code).toEqual('INVALID_TRANSITION');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('returns error when league not found', async () => {
|
||||
leagueRepository.findById.mockResolvedValue(null);
|
||||
@@ -160,8 +139,7 @@ describe('ManageSeasonLifecycleUseCase', () => {
|
||||
>;
|
||||
expect(error.code).toEqual('LEAGUE_NOT_FOUND');
|
||||
expect(error.details).toEqual({ message: 'League not found: league-1' });
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('returns error when season not found', async () => {
|
||||
const league = { id: 'league-1' };
|
||||
@@ -184,6 +162,5 @@ describe('ManageSeasonLifecycleUseCase', () => {
|
||||
expect(error.details).toEqual({
|
||||
message: 'Season season-1 does not belong to league league-1',
|
||||
});
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user