105 lines
3.3 KiB
TypeScript
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);
|
|
});
|
|
});
|
|
});
|