105 lines
3.8 KiB
TypeScript
105 lines
3.8 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
import { LeagueProtestReviewPageQuery } from './LeagueProtestReviewPageQuery';
|
|
import { Result } from '@/lib/contracts/Result';
|
|
import { LeaguesApiClient } from '@/lib/gateways/api/leagues/LeaguesApiClient';
|
|
import { ProtestsApiClient } from '@/lib/gateways/api/protests/ProtestsApiClient';
|
|
import { ConsoleErrorReporter } from '@/lib/infrastructure/logging/ConsoleErrorReporter';
|
|
import { ConsoleLogger } from '@/lib/infrastructure/logging/ConsoleLogger';
|
|
|
|
// Mock dependencies
|
|
vi.mock('@/lib/gateways/api/leagues/LeaguesApiClient', () => ({
|
|
LeaguesApiClient: vi.fn().mockImplementation(function (this: any) { return this; }),
|
|
}));
|
|
|
|
vi.mock('@/lib/gateways/api/protests/ProtestsApiClient', () => ({
|
|
ProtestsApiClient: vi.fn().mockImplementation(function (this: any) { return this; }),
|
|
}));
|
|
|
|
vi.mock('@/lib/infrastructure/logging/ConsoleErrorReporter', () => ({
|
|
ConsoleErrorReporter: vi.fn().mockImplementation(function (this: any) { return this; }),
|
|
}));
|
|
|
|
vi.mock('@/lib/infrastructure/logging/ConsoleLogger', () => ({
|
|
ConsoleLogger: vi.fn().mockImplementation(function (this: any) { return this; }),
|
|
}));
|
|
|
|
describe('LeagueProtestReviewPageQuery', () => {
|
|
let query: LeagueProtestReviewPageQuery;
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
query = new LeagueProtestReviewPageQuery();
|
|
});
|
|
|
|
it('should return placeholder data when execution succeeds', async () => {
|
|
const input = { leagueId: 'league-123', protestId: 'protest-456' };
|
|
|
|
const result = await query.execute(input);
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
const data = result.unwrap() as any;
|
|
expect(data.protest.id).toBe('protest-456');
|
|
expect(LeaguesApiClient).toHaveBeenCalled();
|
|
expect(ProtestsApiClient).toHaveBeenCalled();
|
|
expect(ConsoleErrorReporter).toHaveBeenCalled();
|
|
expect(ConsoleLogger).toHaveBeenCalled();
|
|
});
|
|
|
|
it('should return redirect error on 403/401 errors', async () => {
|
|
const input = { leagueId: 'league-123', protestId: 'protest-456' };
|
|
|
|
(LeaguesApiClient as any).mockImplementationOnce(function () {
|
|
throw new Error('403 Forbidden');
|
|
});
|
|
|
|
const result = await query.execute(input);
|
|
|
|
expect(result.isErr()).toBe(true);
|
|
expect(result.getError()).toBe('redirect');
|
|
});
|
|
|
|
it('should return notFound error on 404 errors', async () => {
|
|
const input = { leagueId: 'league-123', protestId: 'protest-456' };
|
|
(LeaguesApiClient as any).mockImplementationOnce(function () {
|
|
throw new Error('404 Not Found');
|
|
});
|
|
|
|
const result = await query.execute(input);
|
|
|
|
expect(result.isErr()).toBe(true);
|
|
expect(result.getError()).toBe('notFound');
|
|
});
|
|
|
|
it('should return PROTEST_FETCH_FAILED on server errors', async () => {
|
|
const input = { leagueId: 'league-123', protestId: 'protest-456' };
|
|
(LeaguesApiClient as any).mockImplementationOnce(function () {
|
|
throw new Error('500 Internal Server Error');
|
|
});
|
|
|
|
const result = await query.execute(input);
|
|
|
|
expect(result.isErr()).toBe(true);
|
|
expect(result.getError()).toBe('PROTEST_FETCH_FAILED');
|
|
});
|
|
|
|
it('should return UNKNOWN_ERROR on other errors', async () => {
|
|
const input = { leagueId: 'league-123', protestId: 'protest-456' };
|
|
(LeaguesApiClient as any).mockImplementationOnce(function () {
|
|
throw new Error('Some random error');
|
|
});
|
|
|
|
const result = await query.execute(input);
|
|
|
|
expect(result.isErr()).toBe(true);
|
|
expect(result.getError()).toBe('UNKNOWN_ERROR');
|
|
});
|
|
|
|
it('should provide a static execute method', async () => {
|
|
const input = { leagueId: 'league-123', protestId: 'protest-456' };
|
|
const result = await LeagueProtestReviewPageQuery.execute(input);
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
expect((result.unwrap() as any).protest.id).toBe('protest-456');
|
|
});
|
|
});
|