view data fixes
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { LeagueRosterAdminPageQuery } from './LeagueRosterAdminPageQuery';
|
||||
import { LeagueService } from '@/lib/services/leagues/LeagueService';
|
||||
import { Result } from '@/lib/contracts/Result';
|
||||
import { LeagueRosterAdminViewDataBuilder } from '@/lib/builders/view-data/LeagueRosterAdminViewDataBuilder';
|
||||
import { mapToPresentationError } from '@/lib/contracts/page-queries/PresentationError';
|
||||
|
||||
// Mock dependencies
|
||||
vi.mock('@/lib/services/leagues/LeagueService', () => ({
|
||||
LeagueService: vi.fn(class {
|
||||
getRosterAdminData = vi.fn();
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/builders/view-data/LeagueRosterAdminViewDataBuilder', () => ({
|
||||
LeagueRosterAdminViewDataBuilder: {
|
||||
build: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/contracts/page-queries/PresentationError', () => ({
|
||||
mapToPresentationError: vi.fn(),
|
||||
}));
|
||||
|
||||
describe('LeagueRosterAdminPageQuery', () => {
|
||||
let query: LeagueRosterAdminPageQuery;
|
||||
let mockServiceInstance: any;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
query = new LeagueRosterAdminPageQuery();
|
||||
mockServiceInstance = {
|
||||
getRosterAdminData: vi.fn(),
|
||||
};
|
||||
(LeagueService as any).mockImplementation(function () {
|
||||
return mockServiceInstance;
|
||||
});
|
||||
});
|
||||
|
||||
it('should return view data when service succeeds', async () => {
|
||||
const leagueId = 'league-123';
|
||||
const apiDto = { members: [], joinRequests: [] };
|
||||
const viewData = { members: [], joinRequests: [] };
|
||||
|
||||
mockServiceInstance.getRosterAdminData.mockResolvedValue(Result.ok(apiDto));
|
||||
(LeagueRosterAdminViewDataBuilder.build as any).mockReturnValue(viewData);
|
||||
|
||||
const result = await query.execute(leagueId);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toEqual(viewData);
|
||||
expect(LeagueService).toHaveBeenCalled();
|
||||
expect(mockServiceInstance.getRosterAdminData).toHaveBeenCalledWith(leagueId);
|
||||
expect(LeagueRosterAdminViewDataBuilder.build).toHaveBeenCalledWith(apiDto);
|
||||
});
|
||||
|
||||
it('should return mapped presentation error when service fails', async () => {
|
||||
const leagueId = 'league-123';
|
||||
const serviceError = { type: 'notFound' };
|
||||
const presentationError = 'notFound';
|
||||
|
||||
mockServiceInstance.getRosterAdminData.mockResolvedValue(Result.err(serviceError));
|
||||
(mapToPresentationError as any).mockReturnValue(presentationError);
|
||||
|
||||
const result = await query.execute(leagueId);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.getError()).toBe(presentationError);
|
||||
expect(mapToPresentationError).toHaveBeenCalledWith(serviceError);
|
||||
});
|
||||
|
||||
it('should provide a static execute method', async () => {
|
||||
const leagueId = 'league-123';
|
||||
const apiDto = { members: [], joinRequests: [] };
|
||||
const viewData = { members: [], joinRequests: [] };
|
||||
|
||||
mockServiceInstance.getRosterAdminData.mockResolvedValue(Result.ok(apiDto));
|
||||
(LeagueRosterAdminViewDataBuilder.build as any).mockReturnValue(viewData);
|
||||
|
||||
const result = await LeagueRosterAdminPageQuery.execute(leagueId);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toEqual(viewData);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user