Files
gridpilot.gg/apps/website/lib/page-queries/HomePageQuery.test.ts
Marc Mintel 1b0a1f4aee
Some checks failed
Contract Testing / contract-tests (pull_request) Failing after 7m11s
Contract Testing / contract-snapshot (pull_request) Has been skipped
view data fixes
2026-01-24 23:29:55 +01:00

105 lines
3.3 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest';
import { HomePageQuery } from './HomePageQuery';
import { HomeService } from '@/lib/services/home/HomeService';
import { Result } from '@/lib/contracts/Result';
import { HomeViewDataBuilder } from '@/lib/builders/view-data/HomeViewDataBuilder';
// Mock dependencies
vi.mock('@/lib/services/home/HomeService', () => ({
HomeService: vi.fn().mockImplementation(function (this: any) {
this.getHomeData = vi.fn();
this.shouldRedirectToDashboard = vi.fn();
}),
}));
vi.mock('@/lib/builders/view-data/HomeViewDataBuilder', () => ({
HomeViewDataBuilder: {
build: vi.fn(),
},
}));
describe('HomePageQuery', () => {
let query: HomePageQuery;
let mockServiceInstance: any;
beforeEach(() => {
vi.clearAllMocks();
query = new HomePageQuery();
mockServiceInstance = {
getHomeData: vi.fn(),
shouldRedirectToDashboard: vi.fn(),
};
(HomeService as any).mockImplementation(function (this: any) {
return mockServiceInstance;
});
});
describe('execute', () => {
it('should return view data when service succeeds', async () => {
const apiDto = { some: 'data' };
const viewData = { transformed: 'data' } as any;
mockServiceInstance.getHomeData.mockResolvedValue(Result.ok(apiDto));
(HomeViewDataBuilder.build as any).mockReturnValue(viewData);
const result = await query.execute();
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toEqual(viewData);
expect(HomeService).toHaveBeenCalled();
expect(mockServiceInstance.getHomeData).toHaveBeenCalled();
expect(HomeViewDataBuilder.build).toHaveBeenCalledWith(apiDto);
});
it('should return error when service fails', async () => {
mockServiceInstance.getHomeData.mockResolvedValue(Result.err('Service Error'));
const result = await query.execute();
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe('Error');
});
it('should return error on exception', async () => {
mockServiceInstance.getHomeData.mockRejectedValue(new Error('Unexpected error'));
const result = await query.execute();
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe('Error');
});
it('should provide a static execute method', async () => {
const apiDto = { some: 'data' };
const viewData = { transformed: 'data' } as any;
mockServiceInstance.getHomeData.mockResolvedValue(Result.ok(apiDto));
(HomeViewDataBuilder.build as any).mockReturnValue(viewData);
const result = await HomePageQuery.execute();
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toEqual(viewData);
});
});
describe('shouldRedirectToDashboard', () => {
it('should return true when service returns true', async () => {
mockServiceInstance.shouldRedirectToDashboard.mockResolvedValue(true);
const result = await HomePageQuery.shouldRedirectToDashboard();
expect(result).toBe(true);
expect(mockServiceInstance.shouldRedirectToDashboard).toHaveBeenCalled();
});
it('should return false when service returns false', async () => {
mockServiceInstance.shouldRedirectToDashboard.mockResolvedValue(false);
const result = await HomePageQuery.shouldRedirectToDashboard();
expect(result).toBe(false);
});
});
});