refactor
This commit is contained in:
@@ -1,29 +1,52 @@
|
||||
import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { GetAllRacesPageDataUseCase } from './GetAllRacesPageDataUseCase';
|
||||
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
|
||||
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
|
||||
import type { Logger } from '@core/shared/application';
|
||||
|
||||
describe('GetAllRacesPageDataUseCase', () => {
|
||||
let mockRaceRepo: { findAll: Mock };
|
||||
let mockLeagueRepo: { findAll: Mock };
|
||||
let mockLogger: Logger;
|
||||
const mockRaceFindAll = vi.fn();
|
||||
const mockRaceRepo: IRaceRepository = {
|
||||
findById: vi.fn(),
|
||||
findAll: mockRaceFindAll,
|
||||
findByLeagueId: vi.fn(),
|
||||
findUpcomingByLeagueId: vi.fn(),
|
||||
findCompletedByLeagueId: vi.fn(),
|
||||
findByStatus: vi.fn(),
|
||||
findByDateRange: vi.fn(),
|
||||
create: vi.fn(),
|
||||
update: vi.fn(),
|
||||
delete: vi.fn(),
|
||||
exists: vi.fn(),
|
||||
};
|
||||
|
||||
const mockLeagueFindAll = vi.fn();
|
||||
const mockLeagueRepo: ILeagueRepository = {
|
||||
findById: vi.fn(),
|
||||
findAll: mockLeagueFindAll,
|
||||
findByOwnerId: vi.fn(),
|
||||
create: vi.fn(),
|
||||
update: vi.fn(),
|
||||
delete: vi.fn(),
|
||||
exists: vi.fn(),
|
||||
searchByName: vi.fn(),
|
||||
};
|
||||
|
||||
const mockLogger: Logger = {
|
||||
debug: vi.fn(),
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
mockRaceRepo = { findAll: vi.fn() };
|
||||
mockLeagueRepo = { findAll: vi.fn() };
|
||||
mockLogger = {
|
||||
debug: vi.fn(),
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
};
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should return races and filters data', async () => {
|
||||
const useCase = new GetAllRacesPageDataUseCase(
|
||||
mockRaceRepo as unknown as IRaceRepository,
|
||||
mockLeagueRepo as unknown as ILeagueRepository,
|
||||
mockRaceRepo,
|
||||
mockLeagueRepo,
|
||||
mockLogger,
|
||||
);
|
||||
|
||||
@@ -48,8 +71,8 @@ describe('GetAllRacesPageDataUseCase', () => {
|
||||
const league1 = { id: 'league1', name: 'League One' };
|
||||
const league2 = { id: 'league2', name: 'League Two' };
|
||||
|
||||
mockRaceRepo.findAll.mockResolvedValue([race1, race2]);
|
||||
mockLeagueRepo.findAll.mockResolvedValue([league1, league2]);
|
||||
mockRaceFindAll.mockResolvedValue([race1, race2]);
|
||||
mockLeagueFindAll.mockResolvedValue([league1, league2]);
|
||||
|
||||
const result = await useCase.execute();
|
||||
|
||||
@@ -95,13 +118,13 @@ describe('GetAllRacesPageDataUseCase', () => {
|
||||
|
||||
it('should return empty result when no races or leagues', async () => {
|
||||
const useCase = new GetAllRacesPageDataUseCase(
|
||||
mockRaceRepo as unknown as IRaceRepository,
|
||||
mockLeagueRepo as unknown as ILeagueRepository,
|
||||
mockRaceRepo,
|
||||
mockLeagueRepo,
|
||||
mockLogger,
|
||||
);
|
||||
|
||||
mockRaceRepo.findAll.mockResolvedValue([]);
|
||||
mockLeagueRepo.findAll.mockResolvedValue([]);
|
||||
mockRaceFindAll.mockResolvedValue([]);
|
||||
mockLeagueFindAll.mockResolvedValue([]);
|
||||
|
||||
const result = await useCase.execute();
|
||||
|
||||
@@ -123,17 +146,18 @@ describe('GetAllRacesPageDataUseCase', () => {
|
||||
|
||||
it('should return error when repository throws', async () => {
|
||||
const useCase = new GetAllRacesPageDataUseCase(
|
||||
mockRaceRepo as unknown as IRaceRepository,
|
||||
mockLeagueRepo as unknown as ILeagueRepository,
|
||||
mockRaceRepo,
|
||||
mockLeagueRepo,
|
||||
mockLogger,
|
||||
);
|
||||
|
||||
const error = new Error('Repository error');
|
||||
mockRaceRepo.findAll.mockRejectedValue(error);
|
||||
mockRaceFindAll.mockRejectedValue(error);
|
||||
|
||||
const result = await useCase.execute();
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr().message).toBe('Repository error');
|
||||
expect(result.unwrapErr().code).toBe('REPOSITORY_ERROR');
|
||||
expect(result.unwrapErr().details.message).toBe('Repository error');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user