70 lines
2.6 KiB
TypeScript
70 lines
2.6 KiB
TypeScript
import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
|
|
import { ApproveLeagueJoinRequestUseCase } from './ApproveLeagueJoinRequestUseCase';
|
|
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
|
|
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
|
|
|
describe('ApproveLeagueJoinRequestUseCase', () => {
|
|
let mockLeagueMembershipRepo: {
|
|
getJoinRequests: Mock;
|
|
removeJoinRequest: Mock;
|
|
saveMembership: Mock;
|
|
};
|
|
|
|
beforeEach(() => {
|
|
mockLeagueMembershipRepo = {
|
|
getJoinRequests: vi.fn(),
|
|
removeJoinRequest: vi.fn(),
|
|
saveMembership: vi.fn(),
|
|
};
|
|
});
|
|
|
|
it('should approve join request and save membership', async () => {
|
|
const output = {
|
|
present: vi.fn(),
|
|
};
|
|
|
|
const useCase = new ApproveLeagueJoinRequestUseCase(
|
|
mockLeagueMembershipRepo as unknown as ILeagueMembershipRepository,
|
|
);
|
|
|
|
const leagueId = 'league-1';
|
|
const requestId = 'req-1';
|
|
const joinRequests = [{ id: requestId, leagueId, driverId: 'driver-1', requestedAt: new Date(), message: 'msg' }];
|
|
|
|
mockLeagueMembershipRepo.getJoinRequests.mockResolvedValue(joinRequests);
|
|
|
|
const result = await useCase.execute({ leagueId, requestId }, output as unknown as UseCaseOutputPort<any>);
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toBeUndefined();
|
|
expect(mockLeagueMembershipRepo.removeJoinRequest).toHaveBeenCalledWith(requestId);
|
|
expect(mockLeagueMembershipRepo.saveMembership).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
id: expect.any(String),
|
|
leagueId: expect.objectContaining({ toString: expect.any(Function) }),
|
|
driverId: expect.objectContaining({ toString: expect.any(Function) }),
|
|
role: expect.objectContaining({ toString: expect.any(Function) }),
|
|
status: expect.objectContaining({ toString: expect.any(Function) }),
|
|
joinedAt: expect.any(Date),
|
|
})
|
|
);
|
|
expect(output.present).toHaveBeenCalledWith({ success: true, message: 'Join request approved.' });
|
|
});
|
|
|
|
it('should return error if request not found', async () => {
|
|
const output = {
|
|
present: vi.fn(),
|
|
};
|
|
|
|
const useCase = new ApproveLeagueJoinRequestUseCase(
|
|
mockLeagueMembershipRepo as unknown as ILeagueMembershipRepository,
|
|
);
|
|
|
|
mockLeagueMembershipRepo.getJoinRequests.mockResolvedValue([]);
|
|
|
|
const result = await useCase.execute({ leagueId: 'league-1', requestId: 'req-1' }, output as unknown as UseCaseOutputPort<any>);
|
|
|
|
expect(result.isOk()).toBe(false);
|
|
expect(result.error!.code).toBe('JOIN_REQUEST_NOT_FOUND');
|
|
});
|
|
}); |