124 lines
4.4 KiB
TypeScript
124 lines
4.4 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
import { SponsorshipRequestsPageQuery } from './SponsorshipRequestsPageQuery';
|
|
import { SessionGateway } from '@/lib/gateways/SessionGateway';
|
|
import { SponsorshipRequestsService } from '@/lib/services/sponsors/SponsorshipRequestsService';
|
|
import { SponsorshipRequestsPageViewDataBuilder } from '@/lib/builders/view-data/SponsorshipRequestsPageViewDataBuilder';
|
|
import { Result } from '@/lib/contracts/Result';
|
|
import { mapToPresentationError } from '@/lib/contracts/page-queries/PresentationError';
|
|
|
|
// Mock dependencies
|
|
vi.mock('@/lib/gateways/SessionGateway', () => ({
|
|
SessionGateway: vi.fn(),
|
|
}));
|
|
|
|
vi.mock('@/lib/services/sponsors/SponsorshipRequestsService', () => ({
|
|
SponsorshipRequestsService: vi.fn(),
|
|
}));
|
|
|
|
vi.mock('@/lib/builders/view-data/SponsorshipRequestsPageViewDataBuilder', () => ({
|
|
SponsorshipRequestsPageViewDataBuilder: {
|
|
build: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
vi.mock('@/lib/contracts/page-queries/PresentationError', () => ({
|
|
mapToPresentationError: vi.fn(),
|
|
}));
|
|
|
|
describe('SponsorshipRequestsPageQuery', () => {
|
|
let query: SponsorshipRequestsPageQuery;
|
|
let mockSessionGatewayInstance: any;
|
|
let mockServiceInstance: any;
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
query = new SponsorshipRequestsPageQuery();
|
|
|
|
mockSessionGatewayInstance = {
|
|
getSession: vi.fn(),
|
|
};
|
|
(SessionGateway as any).mockImplementation(function() {
|
|
return mockSessionGatewayInstance;
|
|
});
|
|
|
|
mockServiceInstance = {
|
|
getPendingRequests: vi.fn(),
|
|
};
|
|
(SponsorshipRequestsService as any).mockImplementation(function() {
|
|
return mockServiceInstance;
|
|
});
|
|
});
|
|
|
|
it('should return view data when session and service succeed', async () => {
|
|
const primaryDriverId = 'driver-123';
|
|
const session = { user: { primaryDriverId } };
|
|
const apiDto = { sections: [] };
|
|
const viewData = { sections: [] };
|
|
|
|
mockSessionGatewayInstance.getSession.mockResolvedValue(session);
|
|
mockServiceInstance.getPendingRequests.mockResolvedValue(Result.ok(apiDto));
|
|
(SponsorshipRequestsPageViewDataBuilder.build as any).mockReturnValue(viewData);
|
|
|
|
const result = await query.execute();
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toEqual(viewData);
|
|
expect(SessionGateway).toHaveBeenCalled();
|
|
expect(mockServiceInstance.getPendingRequests).toHaveBeenCalledWith({
|
|
entityType: 'driver',
|
|
entityId: primaryDriverId,
|
|
});
|
|
expect(SponsorshipRequestsPageViewDataBuilder.build).toHaveBeenCalledWith(apiDto);
|
|
});
|
|
|
|
it('should return notFound error when session has no primaryDriverId', async () => {
|
|
const session = { user: {} };
|
|
mockSessionGatewayInstance.getSession.mockResolvedValue(session);
|
|
|
|
const result = await query.execute();
|
|
|
|
expect(result.isErr()).toBe(true);
|
|
expect(result.getError()).toBe('notFound');
|
|
});
|
|
|
|
it('should return notFound error when session is null', async () => {
|
|
mockSessionGatewayInstance.getSession.mockResolvedValue(null);
|
|
|
|
const result = await query.execute();
|
|
|
|
expect(result.isErr()).toBe(true);
|
|
expect(result.getError()).toBe('notFound');
|
|
});
|
|
|
|
it('should return mapped presentation error when service fails', async () => {
|
|
const session = { user: { primaryDriverId: 'driver-123' } };
|
|
const serviceError = { type: 'serverError' };
|
|
const presentationError = 'serverError';
|
|
|
|
mockSessionGatewayInstance.getSession.mockResolvedValue(session);
|
|
mockServiceInstance.getPendingRequests.mockResolvedValue(Result.err(serviceError));
|
|
(mapToPresentationError as any).mockReturnValue(presentationError);
|
|
|
|
const result = await query.execute();
|
|
|
|
expect(result.isErr()).toBe(true);
|
|
expect(result.getError()).toBe(presentationError);
|
|
expect(mapToPresentationError).toHaveBeenCalledWith(serviceError);
|
|
});
|
|
|
|
it('should provide a static execute method', async () => {
|
|
const session = { user: { primaryDriverId: 'driver-123' } };
|
|
const apiDto = { sections: [] };
|
|
const viewData = { sections: [] };
|
|
|
|
mockSessionGatewayInstance.getSession.mockResolvedValue(session);
|
|
mockServiceInstance.getPendingRequests.mockResolvedValue(Result.ok(apiDto));
|
|
(SponsorshipRequestsPageViewDataBuilder.build as any).mockReturnValue(viewData);
|
|
|
|
const result = await SponsorshipRequestsPageQuery.execute();
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toEqual(viewData);
|
|
});
|
|
});
|