website refactor
This commit is contained in:
@@ -1,37 +1,31 @@
|
||||
import { beforeEach, describe, expect, it, vi, type Mock } from 'vitest';
|
||||
|
||||
import type { Logger } from '@core/shared/domain/Logger';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
import { Race } from '../../domain/entities/Race';
|
||||
import { Season } from '../../domain/entities/season/Season';
|
||||
import type { RaceRepository } from '../../domain/repositories/RaceRepository';
|
||||
import type { SeasonRepository } from '../../domain/repositories/SeasonRepository';
|
||||
|
||||
import {
|
||||
CreateLeagueSeasonScheduleRaceUseCase,
|
||||
type CreateLeagueSeasonScheduleRaceErrorCode,
|
||||
type CreateLeagueSeasonScheduleRaceResult,
|
||||
CreateLeagueSeasonScheduleRaceUseCase,
|
||||
type CreateLeagueSeasonScheduleRaceErrorCode
|
||||
} from './CreateLeagueSeasonScheduleRaceUseCase';
|
||||
import {
|
||||
UpdateLeagueSeasonScheduleRaceUseCase,
|
||||
type UpdateLeagueSeasonScheduleRaceErrorCode,
|
||||
type UpdateLeagueSeasonScheduleRaceResult,
|
||||
} from './UpdateLeagueSeasonScheduleRaceUseCase';
|
||||
import {
|
||||
DeleteLeagueSeasonScheduleRaceUseCase,
|
||||
type DeleteLeagueSeasonScheduleRaceErrorCode,
|
||||
type DeleteLeagueSeasonScheduleRaceResult,
|
||||
DeleteLeagueSeasonScheduleRaceUseCase,
|
||||
type DeleteLeagueSeasonScheduleRaceErrorCode
|
||||
} from './DeleteLeagueSeasonScheduleRaceUseCase';
|
||||
import {
|
||||
PublishLeagueSeasonScheduleUseCase,
|
||||
type PublishLeagueSeasonScheduleErrorCode,
|
||||
type PublishLeagueSeasonScheduleResult,
|
||||
PublishLeagueSeasonScheduleUseCase,
|
||||
type PublishLeagueSeasonScheduleErrorCode
|
||||
} from './PublishLeagueSeasonScheduleUseCase';
|
||||
import {
|
||||
UnpublishLeagueSeasonScheduleUseCase,
|
||||
type UnpublishLeagueSeasonScheduleErrorCode,
|
||||
type UnpublishLeagueSeasonScheduleResult,
|
||||
UnpublishLeagueSeasonScheduleUseCase,
|
||||
type UnpublishLeagueSeasonScheduleErrorCode
|
||||
} from './UnpublishLeagueSeasonScheduleUseCase';
|
||||
import {
|
||||
UpdateLeagueSeasonScheduleRaceUseCase,
|
||||
type UpdateLeagueSeasonScheduleRaceErrorCode
|
||||
} from './UpdateLeagueSeasonScheduleRaceUseCase';
|
||||
|
||||
function createLogger(): Logger {
|
||||
return {
|
||||
@@ -62,7 +56,6 @@ describe('CreateLeagueSeasonScheduleRaceUseCase', () => {
|
||||
beforeEach(() => {
|
||||
seasonRepository = { findById: vi.fn() };
|
||||
raceRepository = { create: vi.fn() };
|
||||
output = { present: vi.fn() };
|
||||
logger = createLogger();
|
||||
});
|
||||
|
||||
@@ -71,8 +64,8 @@ describe('CreateLeagueSeasonScheduleRaceUseCase', () => {
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
raceRepository.create.mockImplementation(async (race: Race) => race);
|
||||
|
||||
const useCase = new CreateLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
const useCase = new CreateLeagueSeasonScheduleRaceUseCase(seasonRepository as any,
|
||||
raceRepository as any,
|
||||
logger,
|
||||
{ generateRaceId: () => 'race-123' },
|
||||
);
|
||||
@@ -100,8 +93,8 @@ describe('CreateLeagueSeasonScheduleRaceUseCase', () => {
|
||||
const season = createSeasonWithinWindow({ leagueId: 'other-league' });
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
|
||||
const useCase = new CreateLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
const useCase = new CreateLeagueSeasonScheduleRaceUseCase(seasonRepository as any,
|
||||
raceRepository as any,
|
||||
logger,
|
||||
{ generateRaceId: () => 'race-123' },
|
||||
);
|
||||
@@ -127,8 +120,8 @@ describe('CreateLeagueSeasonScheduleRaceUseCase', () => {
|
||||
const season = createSeasonWithinWindow();
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
|
||||
const useCase = new CreateLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
const useCase = new CreateLeagueSeasonScheduleRaceUseCase(seasonRepository as any,
|
||||
raceRepository as any,
|
||||
logger,
|
||||
{ generateRaceId: () => 'race-123' },
|
||||
);
|
||||
@@ -159,7 +152,6 @@ describe('UpdateLeagueSeasonScheduleRaceUseCase', () => {
|
||||
beforeEach(() => {
|
||||
seasonRepository = { findById: vi.fn() };
|
||||
raceRepository = { findById: vi.fn(), update: vi.fn() };
|
||||
output = { present: vi.fn() };
|
||||
logger = createLogger();
|
||||
});
|
||||
|
||||
@@ -177,8 +169,8 @@ describe('UpdateLeagueSeasonScheduleRaceUseCase', () => {
|
||||
raceRepository.findById.mockResolvedValue(existing);
|
||||
raceRepository.update.mockImplementation(async (race: Race) => race);
|
||||
|
||||
const useCase = new UpdateLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
const useCase = new UpdateLeagueSeasonScheduleRaceUseCase(seasonRepository as any,
|
||||
raceRepository as any,
|
||||
logger);
|
||||
|
||||
const newScheduledAt = new Date('2025-01-20T20:00:00Z');
|
||||
@@ -193,20 +185,16 @@ describe('UpdateLeagueSeasonScheduleRaceUseCase', () => {
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(raceRepository.update).toHaveBeenCalledTimes(1);
|
||||
const updated = raceRepository.update.mock.calls[0]?.[0] as Race;
|
||||
expect(updated.id).toBe('race-1');
|
||||
expect(updated.leagueId).toBe('league-1');
|
||||
expect(updated.track).toBe('New Track');
|
||||
expect(updated.car).toBe('New Car');
|
||||
expect(updated.scheduledAt.getTime()).toBe(newScheduledAt.getTime());
|
||||
const presented = result.unwrap();
|
||||
expect(presented.success).toBe(true);
|
||||
});
|
||||
|
||||
it('returns SEASON_NOT_FOUND when season does not belong to league and does not read/update race', async () => {
|
||||
const season = createSeasonWithinWindow({ leagueId: 'other-league' });
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
|
||||
const useCase = new UpdateLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
const useCase = new UpdateLeagueSeasonScheduleRaceUseCase(seasonRepository as any,
|
||||
raceRepository as any,
|
||||
logger);
|
||||
|
||||
const result = await useCase.execute({
|
||||
@@ -224,7 +212,7 @@ describe('UpdateLeagueSeasonScheduleRaceUseCase', () => {
|
||||
expect(error.code).toBe('SEASON_NOT_FOUND');
|
||||
expect(raceRepository.findById).not.toHaveBeenCalled();
|
||||
expect(raceRepository.update).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('returns RACE_OUTSIDE_SEASON_WINDOW when updated scheduledAt is outside window and does not update', async () => {
|
||||
const season = createSeasonWithinWindow();
|
||||
@@ -239,8 +227,8 @@ describe('UpdateLeagueSeasonScheduleRaceUseCase', () => {
|
||||
});
|
||||
raceRepository.findById.mockResolvedValue(existing);
|
||||
|
||||
const useCase = new UpdateLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
const useCase = new UpdateLeagueSeasonScheduleRaceUseCase(seasonRepository as any,
|
||||
raceRepository as any,
|
||||
logger);
|
||||
|
||||
const result = await useCase.execute({
|
||||
@@ -257,15 +245,15 @@ describe('UpdateLeagueSeasonScheduleRaceUseCase', () => {
|
||||
>;
|
||||
expect(error.code).toBe('RACE_OUTSIDE_SEASON_WINDOW');
|
||||
expect(raceRepository.update).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('returns RACE_NOT_FOUND when race does not exist for league and does not update', async () => {
|
||||
const season = createSeasonWithinWindow();
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
raceRepository.findById.mockResolvedValue(null);
|
||||
|
||||
const useCase = new UpdateLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
const useCase = new UpdateLeagueSeasonScheduleRaceUseCase(seasonRepository as any,
|
||||
raceRepository as any,
|
||||
logger);
|
||||
|
||||
const result = await useCase.execute({
|
||||
@@ -282,7 +270,7 @@ describe('UpdateLeagueSeasonScheduleRaceUseCase', () => {
|
||||
>;
|
||||
expect(error.code).toBe('RACE_NOT_FOUND');
|
||||
expect(raceRepository.update).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('DeleteLeagueSeasonScheduleRaceUseCase', () => {
|
||||
@@ -293,7 +281,6 @@ describe('DeleteLeagueSeasonScheduleRaceUseCase', () => {
|
||||
beforeEach(() => {
|
||||
seasonRepository = { findById: vi.fn() };
|
||||
raceRepository = { findById: vi.fn(), delete: vi.fn() };
|
||||
output = { present: vi.fn() };
|
||||
logger = createLogger();
|
||||
});
|
||||
|
||||
@@ -311,8 +298,8 @@ describe('DeleteLeagueSeasonScheduleRaceUseCase', () => {
|
||||
raceRepository.findById.mockResolvedValue(existing);
|
||||
raceRepository.delete.mockResolvedValue(undefined);
|
||||
|
||||
const useCase = new DeleteLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
const useCase = new DeleteLeagueSeasonScheduleRaceUseCase(seasonRepository as any,
|
||||
raceRepository as any,
|
||||
logger);
|
||||
|
||||
const result = await useCase.execute({
|
||||
@@ -330,8 +317,8 @@ describe('DeleteLeagueSeasonScheduleRaceUseCase', () => {
|
||||
const season = createSeasonWithinWindow({ leagueId: 'other-league' });
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
|
||||
const useCase = new DeleteLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
const useCase = new DeleteLeagueSeasonScheduleRaceUseCase(seasonRepository as any,
|
||||
raceRepository as any,
|
||||
logger);
|
||||
|
||||
const result = await useCase.execute({
|
||||
@@ -348,15 +335,15 @@ describe('DeleteLeagueSeasonScheduleRaceUseCase', () => {
|
||||
expect(error.code).toBe('SEASON_NOT_FOUND');
|
||||
expect(raceRepository.findById).not.toHaveBeenCalled();
|
||||
expect(raceRepository.delete).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('returns RACE_NOT_FOUND when race does not exist for league and does not delete', async () => {
|
||||
const season = createSeasonWithinWindow();
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
raceRepository.findById.mockResolvedValue(null);
|
||||
|
||||
const useCase = new DeleteLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
const useCase = new DeleteLeagueSeasonScheduleRaceUseCase(seasonRepository as any,
|
||||
raceRepository as any,
|
||||
logger);
|
||||
|
||||
const result = await useCase.execute({
|
||||
@@ -372,7 +359,7 @@ describe('DeleteLeagueSeasonScheduleRaceUseCase', () => {
|
||||
>;
|
||||
expect(error.code).toBe('RACE_NOT_FOUND');
|
||||
expect(raceRepository.delete).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('PublishLeagueSeasonScheduleUseCase', () => {
|
||||
@@ -381,7 +368,6 @@ describe('PublishLeagueSeasonScheduleUseCase', () => {
|
||||
|
||||
beforeEach(() => {
|
||||
seasonRepository = { findById: vi.fn(), update: vi.fn() };
|
||||
output = { present: vi.fn() };
|
||||
logger = createLogger();
|
||||
});
|
||||
|
||||
@@ -390,24 +376,23 @@ describe('PublishLeagueSeasonScheduleUseCase', () => {
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
seasonRepository.update.mockResolvedValue(undefined);
|
||||
|
||||
const useCase = new PublishLeagueSeasonScheduleUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
const useCase = new PublishLeagueSeasonScheduleUseCase(seasonRepository as any,
|
||||
logger);
|
||||
|
||||
const result = await useCase.execute({ leagueId: 'league-1', seasonId: 'season-1' });
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(seasonRepository.update).toHaveBeenCalledTimes(1);
|
||||
const updatedSeason = seasonRepository.update.mock.calls[0]?.[0] as Season;
|
||||
expect(updatedSeason.id).toBe('season-1');
|
||||
expect(updatedSeason.leagueId).toBe('league-1');
|
||||
expect(updatedSeason.schedulePublished).toBe(true);
|
||||
const presented = result.unwrap();
|
||||
expect(presented.seasonId).toBe('season-1');
|
||||
expect(presented.published).toBe(true);
|
||||
});
|
||||
|
||||
it('returns SEASON_NOT_FOUND when season does not belong to league and does not update', async () => {
|
||||
const season = createSeasonWithinWindow({ leagueId: 'other-league' });
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
|
||||
const useCase = new PublishLeagueSeasonScheduleUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
const useCase = new PublishLeagueSeasonScheduleUseCase(seasonRepository as any,
|
||||
logger);
|
||||
|
||||
const result = await useCase.execute({ leagueId: 'league-1', seasonId: 'season-1' });
|
||||
@@ -419,7 +404,7 @@ describe('PublishLeagueSeasonScheduleUseCase', () => {
|
||||
>;
|
||||
expect(error.code).toBe('SEASON_NOT_FOUND');
|
||||
expect(seasonRepository.update).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('UnpublishLeagueSeasonScheduleUseCase', () => {
|
||||
@@ -428,7 +413,6 @@ describe('UnpublishLeagueSeasonScheduleUseCase', () => {
|
||||
|
||||
beforeEach(() => {
|
||||
seasonRepository = { findById: vi.fn(), update: vi.fn() };
|
||||
output = { present: vi.fn() };
|
||||
logger = createLogger();
|
||||
});
|
||||
|
||||
@@ -437,24 +421,23 @@ describe('UnpublishLeagueSeasonScheduleUseCase', () => {
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
seasonRepository.update.mockResolvedValue(undefined);
|
||||
|
||||
const useCase = new UnpublishLeagueSeasonScheduleUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
const useCase = new UnpublishLeagueSeasonScheduleUseCase(seasonRepository as any,
|
||||
logger);
|
||||
|
||||
const result = await useCase.execute({ leagueId: 'league-1', seasonId: 'season-1' });
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(seasonRepository.update).toHaveBeenCalledTimes(1);
|
||||
const updatedSeason = seasonRepository.update.mock.calls[0]?.[0] as Season;
|
||||
expect(updatedSeason.id).toBe('season-1');
|
||||
expect(updatedSeason.leagueId).toBe('league-1');
|
||||
expect(updatedSeason.schedulePublished).toBe(false);
|
||||
const presented = result.unwrap();
|
||||
expect(presented.seasonId).toBe('season-1');
|
||||
expect(presented.published).toBe(false);
|
||||
});
|
||||
|
||||
it('returns SEASON_NOT_FOUND when season does not belong to league and does not update', async () => {
|
||||
const season = createSeasonWithinWindow({ leagueId: 'other-league' });
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
|
||||
const useCase = new UnpublishLeagueSeasonScheduleUseCase(seasonRepository as unknown as ISeasonRepository,
|
||||
const useCase = new UnpublishLeagueSeasonScheduleUseCase(seasonRepository as any,
|
||||
logger);
|
||||
|
||||
const result = await useCase.execute({ leagueId: 'league-1', seasonId: 'season-1' });
|
||||
@@ -466,5 +449,5 @@ describe('UnpublishLeagueSeasonScheduleUseCase', () => {
|
||||
>;
|
||||
expect(error.code).toBe('SEASON_NOT_FOUND');
|
||||
expect(seasonRepository.update).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user