127 lines
3.7 KiB
TypeScript
127 lines
3.7 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 { Team } from '../../domain/entities/Team';
|
|
import type { DriverRepository } from '../../domain/repositories/DriverRepository';
|
|
import type { TeamMembershipRepository } from '../../domain/repositories/TeamMembershipRepository';
|
|
import type { TeamRepository } from '../../domain/repositories/TeamRepository';
|
|
import {
|
|
GetTeamJoinRequestsUseCase,
|
|
type GetTeamJoinRequestsErrorCode,
|
|
type GetTeamJoinRequestsInput,
|
|
type GetTeamJoinRequestsResult,
|
|
} from './GetTeamJoinRequestsUseCase';
|
|
|
|
describe('GetTeamJoinRequestsUseCase', () => {
|
|
let useCase: GetTeamJoinRequestsUseCase;
|
|
let membershipRepository: {
|
|
getJoinRequests: Mock;
|
|
};
|
|
let driverRepository: {
|
|
findById: Mock;
|
|
};
|
|
let teamRepository: {
|
|
findById: Mock;
|
|
};
|
|
beforeEach(() => {
|
|
membershipRepository = {
|
|
getJoinRequests: vi.fn(),
|
|
};
|
|
driverRepository = {
|
|
findById: vi.fn(),
|
|
};
|
|
teamRepository = {
|
|
findById: vi.fn(),
|
|
};
|
|
|
|
useCase = new GetTeamJoinRequestsUseCase(
|
|
membershipRepository as unknown as TeamMembershipRepository,
|
|
driverRepository as unknown as DriverRepository,
|
|
teamRepository as unknown as TeamRepository
|
|
);
|
|
});
|
|
|
|
it('should return join requests with drivers when team exists', async () => {
|
|
const teamId = 'team-1';
|
|
const input: GetTeamJoinRequestsInput = { teamId };
|
|
|
|
const joinRequests = [
|
|
{ id: 'req-1', teamId, driverId: 'driver-1', requestedAt: new Date(), message: 'msg' },
|
|
];
|
|
|
|
const driver = Driver.create({
|
|
id: 'driver-1',
|
|
iracingId: '123',
|
|
name: 'Driver 1',
|
|
country: 'US',
|
|
});
|
|
|
|
const team = Team.create({
|
|
id: teamId,
|
|
name: 'Team 1',
|
|
tag: 'T1',
|
|
description: 'Description',
|
|
ownerId: 'owner-1',
|
|
leagues: [],
|
|
});
|
|
|
|
teamRepository.findById.mockResolvedValue(team);
|
|
membershipRepository.getJoinRequests.mockResolvedValue(joinRequests);
|
|
driverRepository.findById.mockResolvedValue(driver);
|
|
|
|
const result = await useCase.execute(input);
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
const presented = result.unwrap() as GetTeamJoinRequestsResult;
|
|
|
|
expect(presented.team).toBe(team);
|
|
expect(presented.joinRequests).toHaveLength(1);
|
|
expect(presented.joinRequests[0]!).toMatchObject({
|
|
id: 'req-1',
|
|
teamId,
|
|
driverId: 'driver-1',
|
|
message: 'msg',
|
|
});
|
|
expect(presented.joinRequests[0]!.driver).toBe(driver);
|
|
});
|
|
|
|
it('should return TEAM_NOT_FOUND error when team does not exist', async () => {
|
|
const teamId = 'missing-team';
|
|
const input: GetTeamJoinRequestsInput = { teamId };
|
|
|
|
teamRepository.findById.mockResolvedValue(null);
|
|
|
|
const result = await useCase.execute(input);
|
|
|
|
expect(result.isErr()).toBe(true);
|
|
|
|
const err = result.unwrapErr() as ApplicationErrorCode<
|
|
GetTeamJoinRequestsErrorCode,
|
|
{ message: string }
|
|
>;
|
|
|
|
expect(err.code).toBe('TEAM_NOT_FOUND');
|
|
expect(err.details.message).toBe('Team not found');
|
|
});
|
|
|
|
it('should return REPOSITORY_ERROR when repository throws', async () => {
|
|
const teamId = 'team-1';
|
|
const input: GetTeamJoinRequestsInput = { teamId };
|
|
const error = new Error('Repository failure');
|
|
|
|
teamRepository.findById.mockRejectedValue(error);
|
|
|
|
const result = await useCase.execute(input);
|
|
|
|
expect(result.isErr()).toBe(true);
|
|
|
|
const err = result.unwrapErr() as ApplicationErrorCode<
|
|
GetTeamJoinRequestsErrorCode,
|
|
{ message: string }
|
|
>;
|
|
|
|
expect(err.code).toBe('REPOSITORY_ERROR');
|
|
expect(err.details.message).toBe('Repository failure');
|
|
});
|
|
});
|