Files
gridpilot.gg/core/racing/application/use-cases/GetLeagueJoinRequestsUseCase.test.ts
2026-01-16 19:46:49 +01:00

120 lines
3.6 KiB
TypeScript

import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import { beforeEach, describe, expect, it, vi, type Mock } from 'vitest';
import { Driver } from '../../domain/entities/Driver';
import { DriverRepository } from '../../domain/repositories/DriverRepository';
import { LeagueMembershipRepository } from '../../domain/repositories/LeagueMembershipRepository';
import { LeagueRepository } from '../../domain/repositories/LeagueRepository';
import {
GetLeagueJoinRequestsUseCase,
type GetLeagueJoinRequestsErrorCode,
type GetLeagueJoinRequestsInput,
} from './GetLeagueJoinRequestsUseCase';
describe('GetLeagueJoinRequestsUseCase', () => {
let useCase: GetLeagueJoinRequestsUseCase;
let leagueMembershipRepository: {
getJoinRequests: Mock;
};
let driverRepository: {
findById: Mock;
};
let leagueRepository: {
exists: Mock;
};
beforeEach(() => {
leagueMembershipRepository = {
getJoinRequests: vi.fn(),
};
driverRepository = {
findById: vi.fn(),
};
leagueRepository = {
exists: vi.fn(),
};
useCase = new GetLeagueJoinRequestsUseCase(
leagueMembershipRepository as unknown as LeagueMembershipRepository,
driverRepository as unknown as DriverRepository,
leagueRepository as unknown as LeagueRepository,
);
});
it('should return join requests with drivers when league exists', async () => {
const leagueId = 'league-1';
const input: GetLeagueJoinRequestsInput = { leagueId };
const joinRequests = [
{
id: 'req-1',
leagueId: { toString: () => leagueId },
driverId: { toString: () => 'driver-1' },
requestedAt: { toDate: () => new Date() },
message: 'msg',
},
];
const driver = Driver.create({
id: 'driver-1',
iracingId: '123',
name: 'Driver 1',
country: 'US',
});
leagueRepository.exists.mockResolvedValue(true);
leagueMembershipRepository.getJoinRequests.mockResolvedValue(joinRequests);
driverRepository.findById.mockResolvedValue(driver);
const result = await useCase.execute(input);
expect(result.isOk()).toBe(true);
const successResult = result.unwrap();
expect(successResult.joinRequests).toHaveLength(1);
expect(successResult.joinRequests[0]).toMatchObject({
id: 'req-1',
leagueId,
driverId: 'driver-1',
message: 'msg',
});
expect(successResult.joinRequests[0].driver).toBe(driver);
});
it('should return LEAGUE_NOT_FOUND error when league does not exist', async () => {
const leagueId = 'missing-league';
const input: GetLeagueJoinRequestsInput = { leagueId };
leagueRepository.exists.mockResolvedValue(false);
const result = await useCase.execute(input);
expect(result.isErr()).toBe(true);
const err = result.unwrapErr() as ApplicationErrorCode<
GetLeagueJoinRequestsErrorCode,
{ message: string }
>;
expect(err.code).toBe('LEAGUE_NOT_FOUND');
expect(err.details.message).toBe('League not found');
});
it('should return REPOSITORY_ERROR when repository throws', async () => {
const leagueId = 'league-1';
const input: GetLeagueJoinRequestsInput = { leagueId };
const error = new Error('Repository failure');
leagueRepository.exists.mockRejectedValue(error);
const result = await useCase.execute(input);
expect(result.isErr()).toBe(true);
const err = result.unwrapErr() as ApplicationErrorCode<
GetLeagueJoinRequestsErrorCode,
{ message: string }
>;
expect(err.code).toBe('REPOSITORY_ERROR');
expect(err.details.message).toBe('Repository failure');
});
});