view models
This commit is contained in:
@@ -1,193 +1,93 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { describe, it, expect, vi, Mocked } from 'vitest';
|
||||
import { AnalyticsService } from './AnalyticsService';
|
||||
import { AnalyticsApiClient } from '../../api/analytics/AnalyticsApiClient';
|
||||
import type { RecordPageViewInputDto, RecordPageViewOutputDto, RecordEngagementInputDto, RecordEngagementOutputDto } from '../../dtos';
|
||||
import { RecordPageViewInputViewModel } from '../../view-models/RecordPageViewInputViewModel';
|
||||
import { RecordEngagementInputViewModel } from '../../view-models/RecordEngagementInputViewModel';
|
||||
|
||||
describe('AnalyticsService', () => {
|
||||
let mockApiClient: AnalyticsApiClient;
|
||||
let mockApiClient: Mocked<AnalyticsApiClient>;
|
||||
let service: AnalyticsService;
|
||||
|
||||
beforeEach(() => {
|
||||
mockApiClient = {
|
||||
recordPageView: vi.fn(),
|
||||
recordEngagement: vi.fn(),
|
||||
getDashboardData: vi.fn(),
|
||||
getAnalyticsMetrics: vi.fn(),
|
||||
} as unknown as AnalyticsApiClient;
|
||||
} as Mocked<AnalyticsApiClient>;
|
||||
|
||||
service = new AnalyticsService(mockApiClient);
|
||||
});
|
||||
|
||||
describe('recordPageView', () => {
|
||||
it('should record page view via API client', async () => {
|
||||
// Arrange
|
||||
const input: RecordPageViewInputDto = {
|
||||
page: '/dashboard',
|
||||
timestamp: '2025-12-17T20:00:00Z',
|
||||
userId: 'user-1',
|
||||
};
|
||||
it('should call apiClient.recordPageView with correct input', async () => {
|
||||
const input = new RecordPageViewInputViewModel({
|
||||
path: '/dashboard',
|
||||
userId: 'user-123',
|
||||
});
|
||||
|
||||
const expectedOutput: RecordPageViewOutputDto = {
|
||||
success: true,
|
||||
};
|
||||
const expectedOutput = { pageViewId: 'pv-123' };
|
||||
mockApiClient.recordPageView.mockResolvedValue(expectedOutput);
|
||||
|
||||
vi.mocked(mockApiClient.recordPageView).mockResolvedValue(expectedOutput);
|
||||
|
||||
// Act
|
||||
const result = await service.recordPageView(input);
|
||||
|
||||
// Assert
|
||||
expect(mockApiClient.recordPageView).toHaveBeenCalledWith(input);
|
||||
expect(mockApiClient.recordPageView).toHaveBeenCalledTimes(1);
|
||||
expect(result).toBe(expectedOutput);
|
||||
expect(mockApiClient.recordPageView).toHaveBeenCalledWith({
|
||||
path: '/dashboard',
|
||||
userId: 'user-123',
|
||||
});
|
||||
expect(result).toEqual(expectedOutput);
|
||||
});
|
||||
|
||||
it('should propagate API client errors', async () => {
|
||||
// Arrange
|
||||
const input: RecordPageViewInputDto = {
|
||||
page: '/dashboard',
|
||||
timestamp: '2025-12-17T20:00:00Z',
|
||||
userId: 'user-1',
|
||||
};
|
||||
it('should call apiClient.recordPageView without userId when not provided', async () => {
|
||||
const input = new RecordPageViewInputViewModel({
|
||||
path: '/home',
|
||||
});
|
||||
|
||||
const error = new Error('API Error: Failed to record page view');
|
||||
vi.mocked(mockApiClient.recordPageView).mockRejectedValue(error);
|
||||
const expectedOutput = { pageViewId: 'pv-456' };
|
||||
mockApiClient.recordPageView.mockResolvedValue(expectedOutput);
|
||||
|
||||
// Act & Assert
|
||||
await expect(service.recordPageView(input)).rejects.toThrow(
|
||||
'API Error: Failed to record page view'
|
||||
);
|
||||
|
||||
expect(mockApiClient.recordPageView).toHaveBeenCalledWith(input);
|
||||
expect(mockApiClient.recordPageView).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should handle different input parameters', async () => {
|
||||
// Arrange
|
||||
const input: RecordPageViewInputDto = {
|
||||
page: '/races',
|
||||
timestamp: '2025-12-18T10:30:00Z',
|
||||
userId: 'user-2',
|
||||
};
|
||||
|
||||
const expectedOutput: RecordPageViewOutputDto = {
|
||||
success: true,
|
||||
};
|
||||
|
||||
vi.mocked(mockApiClient.recordPageView).mockResolvedValue(expectedOutput);
|
||||
|
||||
// Act
|
||||
const result = await service.recordPageView(input);
|
||||
|
||||
// Assert
|
||||
expect(mockApiClient.recordPageView).toHaveBeenCalledWith(input);
|
||||
expect(result).toBe(expectedOutput);
|
||||
expect(mockApiClient.recordPageView).toHaveBeenCalledWith({
|
||||
path: '/home',
|
||||
});
|
||||
expect(result).toEqual(expectedOutput);
|
||||
});
|
||||
});
|
||||
|
||||
describe('recordEngagement', () => {
|
||||
it('should record engagement event via API client', async () => {
|
||||
// Arrange
|
||||
const input: RecordEngagementInputDto = {
|
||||
event: 'button_click',
|
||||
element: 'register-race-btn',
|
||||
page: '/races',
|
||||
timestamp: '2025-12-17T20:00:00Z',
|
||||
userId: 'user-1',
|
||||
};
|
||||
it('should call apiClient.recordEngagement with correct input', async () => {
|
||||
const input = new RecordEngagementInputViewModel({
|
||||
eventType: 'button_click',
|
||||
userId: 'user-123',
|
||||
metadata: { buttonId: 'submit', page: '/form' },
|
||||
});
|
||||
|
||||
const expectedOutput: RecordEngagementOutputDto = {
|
||||
success: true,
|
||||
};
|
||||
const expectedOutput = { eventId: 'event-123', engagementWeight: 1.5 };
|
||||
mockApiClient.recordEngagement.mockResolvedValue(expectedOutput);
|
||||
|
||||
vi.mocked(mockApiClient.recordEngagement).mockResolvedValue(expectedOutput);
|
||||
|
||||
// Act
|
||||
const result = await service.recordEngagement(input);
|
||||
|
||||
// Assert
|
||||
expect(mockApiClient.recordEngagement).toHaveBeenCalledWith(input);
|
||||
expect(mockApiClient.recordEngagement).toHaveBeenCalledTimes(1);
|
||||
expect(result).toBe(expectedOutput);
|
||||
expect(mockApiClient.recordEngagement).toHaveBeenCalledWith({
|
||||
eventType: 'button_click',
|
||||
userId: 'user-123',
|
||||
metadata: { buttonId: 'submit', page: '/form' },
|
||||
});
|
||||
expect(result).toEqual(expectedOutput);
|
||||
});
|
||||
|
||||
it('should propagate API client errors', async () => {
|
||||
// Arrange
|
||||
const input: RecordEngagementInputDto = {
|
||||
event: 'form_submit',
|
||||
element: 'contact-form',
|
||||
page: '/contact',
|
||||
timestamp: '2025-12-17T20:00:00Z',
|
||||
userId: 'user-1',
|
||||
};
|
||||
it('should call apiClient.recordEngagement without optional fields', async () => {
|
||||
const input = new RecordEngagementInputViewModel({
|
||||
eventType: 'page_load',
|
||||
});
|
||||
|
||||
const error = new Error('API Error: Failed to record engagement');
|
||||
vi.mocked(mockApiClient.recordEngagement).mockRejectedValue(error);
|
||||
const expectedOutput = { eventId: 'event-456', engagementWeight: 0.5 };
|
||||
mockApiClient.recordEngagement.mockResolvedValue(expectedOutput);
|
||||
|
||||
// Act & Assert
|
||||
await expect(service.recordEngagement(input)).rejects.toThrow(
|
||||
'API Error: Failed to record engagement'
|
||||
);
|
||||
|
||||
expect(mockApiClient.recordEngagement).toHaveBeenCalledWith(input);
|
||||
expect(mockApiClient.recordEngagement).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should handle different engagement types', async () => {
|
||||
// Arrange
|
||||
const input: RecordEngagementInputDto = {
|
||||
event: 'scroll',
|
||||
element: 'race-list',
|
||||
page: '/races',
|
||||
timestamp: '2025-12-18T10:30:00Z',
|
||||
userId: 'user-2',
|
||||
};
|
||||
|
||||
const expectedOutput: RecordEngagementOutputDto = {
|
||||
success: true,
|
||||
};
|
||||
|
||||
vi.mocked(mockApiClient.recordEngagement).mockResolvedValue(expectedOutput);
|
||||
|
||||
// Act
|
||||
const result = await service.recordEngagement(input);
|
||||
|
||||
// Assert
|
||||
expect(mockApiClient.recordEngagement).toHaveBeenCalledWith(input);
|
||||
expect(result).toBe(expectedOutput);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Constructor Dependency Injection', () => {
|
||||
it('should require apiClient', () => {
|
||||
// This test verifies the constructor signature
|
||||
expect(() => {
|
||||
new AnalyticsService(mockApiClient);
|
||||
}).not.toThrow();
|
||||
});
|
||||
|
||||
it('should use injected apiClient', async () => {
|
||||
// Arrange
|
||||
const customApiClient = {
|
||||
recordPageView: vi.fn().mockResolvedValue({ success: true }),
|
||||
recordEngagement: vi.fn().mockResolvedValue({ success: true }),
|
||||
getDashboardData: vi.fn(),
|
||||
getAnalyticsMetrics: vi.fn(),
|
||||
} as unknown as AnalyticsApiClient;
|
||||
|
||||
const customService = new AnalyticsService(customApiClient);
|
||||
|
||||
const input: RecordPageViewInputDto = {
|
||||
page: '/test',
|
||||
timestamp: '2025-12-17T20:00:00Z',
|
||||
userId: 'user-1',
|
||||
};
|
||||
|
||||
// Act
|
||||
await customService.recordPageView(input);
|
||||
|
||||
// Assert
|
||||
expect(customApiClient.recordPageView).toHaveBeenCalledWith(input);
|
||||
expect(mockApiClient.recordEngagement).toHaveBeenCalledWith({
|
||||
eventType: 'page_load',
|
||||
});
|
||||
expect(result).toEqual(expectedOutput);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,7 @@
|
||||
import { AnalyticsApiClient } from '../../api/analytics/AnalyticsApiClient';
|
||||
import type { RecordPageViewInputDto, RecordPageViewOutputDto, RecordEngagementInputDto, RecordEngagementOutputDto } from '../../dtos';
|
||||
import { RecordPageViewOutputDTO, RecordEngagementOutputDTO } from '../../types/generated';
|
||||
import { RecordPageViewInputViewModel } from '../../view-models/RecordPageViewInputViewModel';
|
||||
import { RecordEngagementInputViewModel } from '../../view-models/RecordEngagementInputViewModel';
|
||||
|
||||
/**
|
||||
* Analytics Service
|
||||
@@ -15,22 +17,29 @@ export class AnalyticsService {
|
||||
/**
|
||||
* Record a page view
|
||||
*/
|
||||
async recordPageView(input: RecordPageViewInputDto): Promise<RecordPageViewOutputDto> {
|
||||
try {
|
||||
return await this.apiClient.recordPageView(input);
|
||||
} catch (error) {
|
||||
throw error;
|
||||
async recordPageView(input: RecordPageViewInputViewModel): Promise<RecordPageViewOutputDTO> {
|
||||
const apiInput: { path: string; userId?: string } = {
|
||||
path: input.path,
|
||||
};
|
||||
if (input.userId) {
|
||||
apiInput.userId = input.userId;
|
||||
}
|
||||
return await this.apiClient.recordPageView(apiInput);
|
||||
}
|
||||
|
||||
/**
|
||||
* Record an engagement event
|
||||
*/
|
||||
async recordEngagement(input: RecordEngagementInputDto): Promise<RecordEngagementOutputDto> {
|
||||
try {
|
||||
return await this.apiClient.recordEngagement(input);
|
||||
} catch (error) {
|
||||
throw error;
|
||||
async recordEngagement(input: RecordEngagementInputViewModel): Promise<RecordEngagementOutputDTO> {
|
||||
const apiInput: { eventType: string; userId?: string; metadata?: Record<string, unknown> } = {
|
||||
eventType: input.eventType,
|
||||
};
|
||||
if (input.userId) {
|
||||
apiInput.userId = input.userId;
|
||||
}
|
||||
if (input.metadata) {
|
||||
apiInput.metadata = input.metadata;
|
||||
}
|
||||
return await this.apiClient.recordEngagement(apiInput);
|
||||
}
|
||||
}
|
||||
@@ -1,227 +1,80 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { describe, it, expect, vi, Mocked } from 'vitest';
|
||||
import { DashboardService } from './DashboardService';
|
||||
import { AnalyticsApiClient } from '../../api/analytics/AnalyticsApiClient';
|
||||
import { AnalyticsDashboardPresenter } from '../../presenters/AnalyticsDashboardPresenter';
|
||||
import { AnalyticsMetricsPresenter } from '../../presenters/AnalyticsMetricsPresenter';
|
||||
import type { AnalyticsDashboardDto, AnalyticsMetricsDto } from '../../dtos';
|
||||
import { AnalyticsDashboardViewModel, AnalyticsMetricsViewModel } from '../../view-models';
|
||||
|
||||
describe('DashboardService', () => {
|
||||
let mockApiClient: AnalyticsApiClient;
|
||||
let mockDashboardPresenter: AnalyticsDashboardPresenter;
|
||||
let mockMetricsPresenter: AnalyticsMetricsPresenter;
|
||||
let mockApiClient: Mocked<AnalyticsApiClient>;
|
||||
let service: DashboardService;
|
||||
|
||||
beforeEach(() => {
|
||||
mockApiClient = {
|
||||
recordPageView: vi.fn(),
|
||||
recordEngagement: vi.fn(),
|
||||
getDashboardData: vi.fn(),
|
||||
getAnalyticsMetrics: vi.fn(),
|
||||
} as unknown as AnalyticsApiClient;
|
||||
recordPageView: vi.fn(),
|
||||
recordEngagement: vi.fn(),
|
||||
} as Mocked<AnalyticsApiClient>;
|
||||
|
||||
mockDashboardPresenter = {
|
||||
present: vi.fn(),
|
||||
} as unknown as AnalyticsDashboardPresenter;
|
||||
|
||||
mockMetricsPresenter = {
|
||||
present: vi.fn(),
|
||||
} as unknown as AnalyticsMetricsPresenter;
|
||||
|
||||
service = new DashboardService(mockApiClient, mockDashboardPresenter, mockMetricsPresenter);
|
||||
service = new DashboardService(mockApiClient);
|
||||
});
|
||||
|
||||
describe('getDashboardData', () => {
|
||||
it('should fetch dashboard data from API and transform via presenter', async () => {
|
||||
// Arrange
|
||||
const mockDto: AnalyticsDashboardDto = {
|
||||
totalUsers: 1000,
|
||||
activeUsers: 750,
|
||||
totalRaces: 50,
|
||||
totalLeagues: 25,
|
||||
it('should call apiClient.getDashboardData and return AnalyticsDashboardViewModel', async () => {
|
||||
const dto = {
|
||||
totalUsers: 100,
|
||||
activeUsers: 50,
|
||||
totalRaces: 20,
|
||||
totalLeagues: 5,
|
||||
};
|
||||
mockApiClient.getDashboardData.mockResolvedValue(dto);
|
||||
|
||||
const mockViewModel: AnalyticsDashboardViewModel = new AnalyticsDashboardViewModel(mockDto);
|
||||
|
||||
vi.mocked(mockApiClient.getDashboardData).mockResolvedValue(mockDto);
|
||||
vi.mocked(mockDashboardPresenter.present).mockReturnValue(mockViewModel);
|
||||
|
||||
// Act
|
||||
const result = await service.getDashboardData();
|
||||
|
||||
// Assert
|
||||
expect(mockApiClient.getDashboardData).toHaveBeenCalledTimes(1);
|
||||
expect(mockDashboardPresenter.present).toHaveBeenCalledWith(mockDto);
|
||||
expect(mockDashboardPresenter.present).toHaveBeenCalledTimes(1);
|
||||
expect(result).toBe(mockViewModel);
|
||||
});
|
||||
|
||||
it('should propagate API client errors', async () => {
|
||||
// Arrange
|
||||
const error = new Error('API Error: Failed to fetch dashboard data');
|
||||
vi.mocked(mockApiClient.getDashboardData).mockRejectedValue(error);
|
||||
|
||||
// Act & Assert
|
||||
await expect(service.getDashboardData()).rejects.toThrow(
|
||||
'API Error: Failed to fetch dashboard data'
|
||||
);
|
||||
|
||||
expect(mockApiClient.getDashboardData).toHaveBeenCalledTimes(1);
|
||||
expect(mockDashboardPresenter.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should propagate presenter errors', async () => {
|
||||
// Arrange
|
||||
const mockDto: AnalyticsDashboardDto = {
|
||||
totalUsers: 500,
|
||||
activeUsers: 300,
|
||||
totalRaces: 20,
|
||||
totalLeagues: 10,
|
||||
};
|
||||
|
||||
const error = new Error('Presenter Error: Invalid DTO structure');
|
||||
vi.mocked(mockApiClient.getDashboardData).mockResolvedValue(mockDto);
|
||||
vi.mocked(mockDashboardPresenter.present).mockImplementation(() => {
|
||||
throw error;
|
||||
});
|
||||
|
||||
// Act & Assert
|
||||
await expect(service.getDashboardData()).rejects.toThrow(
|
||||
'Presenter Error: Invalid DTO structure'
|
||||
);
|
||||
|
||||
expect(mockApiClient.getDashboardData).toHaveBeenCalledTimes(1);
|
||||
expect(mockDashboardPresenter.present).toHaveBeenCalledWith(mockDto);
|
||||
expect(mockApiClient.getDashboardData).toHaveBeenCalled();
|
||||
expect(result).toBeInstanceOf(AnalyticsDashboardViewModel);
|
||||
expect(result.totalUsers).toBe(100);
|
||||
expect(result.activeUsers).toBe(50);
|
||||
expect(result.totalRaces).toBe(20);
|
||||
expect(result.totalLeagues).toBe(5);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getAnalyticsMetrics', () => {
|
||||
it('should fetch analytics metrics from API and transform via presenter', async () => {
|
||||
// Arrange
|
||||
const mockDto: AnalyticsMetricsDto = {
|
||||
pageViews: 5000,
|
||||
uniqueVisitors: 1200,
|
||||
averageSessionDuration: 180,
|
||||
bounceRate: 35.5,
|
||||
it('should call apiClient.getAnalyticsMetrics and return AnalyticsMetricsViewModel', async () => {
|
||||
const dto = {
|
||||
pageViews: 1000,
|
||||
uniqueVisitors: 500,
|
||||
averageSessionDuration: 300,
|
||||
bounceRate: 0.25,
|
||||
};
|
||||
mockApiClient.getAnalyticsMetrics.mockResolvedValue(dto);
|
||||
|
||||
const mockViewModel: AnalyticsMetricsViewModel = new AnalyticsMetricsViewModel(mockDto);
|
||||
|
||||
vi.mocked(mockApiClient.getAnalyticsMetrics).mockResolvedValue(mockDto);
|
||||
vi.mocked(mockMetricsPresenter.present).mockReturnValue(mockViewModel);
|
||||
|
||||
// Act
|
||||
const result = await service.getAnalyticsMetrics();
|
||||
|
||||
// Assert
|
||||
expect(mockApiClient.getAnalyticsMetrics).toHaveBeenCalledTimes(1);
|
||||
expect(mockMetricsPresenter.present).toHaveBeenCalledWith(mockDto);
|
||||
expect(mockMetricsPresenter.present).toHaveBeenCalledTimes(1);
|
||||
expect(result).toBe(mockViewModel);
|
||||
});
|
||||
|
||||
it('should propagate API client errors', async () => {
|
||||
// Arrange
|
||||
const error = new Error('API Error: Failed to fetch analytics metrics');
|
||||
vi.mocked(mockApiClient.getAnalyticsMetrics).mockRejectedValue(error);
|
||||
|
||||
// Act & Assert
|
||||
await expect(service.getAnalyticsMetrics()).rejects.toThrow(
|
||||
'API Error: Failed to fetch analytics metrics'
|
||||
);
|
||||
|
||||
expect(mockApiClient.getAnalyticsMetrics).toHaveBeenCalledTimes(1);
|
||||
expect(mockMetricsPresenter.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should propagate presenter errors', async () => {
|
||||
// Arrange
|
||||
const mockDto: AnalyticsMetricsDto = {
|
||||
pageViews: 2500,
|
||||
uniqueVisitors: 600,
|
||||
averageSessionDuration: 120,
|
||||
bounceRate: 45.2,
|
||||
};
|
||||
|
||||
const error = new Error('Presenter Error: Invalid metrics data');
|
||||
vi.mocked(mockApiClient.getAnalyticsMetrics).mockResolvedValue(mockDto);
|
||||
vi.mocked(mockMetricsPresenter.present).mockImplementation(() => {
|
||||
throw error;
|
||||
});
|
||||
|
||||
// Act & Assert
|
||||
await expect(service.getAnalyticsMetrics()).rejects.toThrow(
|
||||
'Presenter Error: Invalid metrics data'
|
||||
);
|
||||
|
||||
expect(mockApiClient.getAnalyticsMetrics).toHaveBeenCalledTimes(1);
|
||||
expect(mockMetricsPresenter.present).toHaveBeenCalledWith(mockDto);
|
||||
expect(mockApiClient.getAnalyticsMetrics).toHaveBeenCalled();
|
||||
expect(result).toBeInstanceOf(AnalyticsMetricsViewModel);
|
||||
expect(result.pageViews).toBe(1000);
|
||||
expect(result.uniqueVisitors).toBe(500);
|
||||
expect(result.averageSessionDuration).toBe(300);
|
||||
expect(result.bounceRate).toBe(0.25);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getDashboardOverview', () => {
|
||||
it('should delegate to getDashboardData for backward compatibility', async () => {
|
||||
// Arrange
|
||||
const mockDto: AnalyticsDashboardDto = {
|
||||
totalUsers: 800,
|
||||
activeUsers: 600,
|
||||
it('should call getDashboardData and return the result', async () => {
|
||||
const dto = {
|
||||
totalUsers: 200,
|
||||
activeUsers: 100,
|
||||
totalRaces: 40,
|
||||
totalLeagues: 20,
|
||||
totalLeagues: 10,
|
||||
};
|
||||
mockApiClient.getDashboardData.mockResolvedValue(dto);
|
||||
|
||||
const mockViewModel: AnalyticsDashboardViewModel = new AnalyticsDashboardViewModel(mockDto);
|
||||
|
||||
vi.mocked(mockApiClient.getDashboardData).mockResolvedValue(mockDto);
|
||||
vi.mocked(mockDashboardPresenter.present).mockReturnValue(mockViewModel);
|
||||
|
||||
// Act
|
||||
const result = await service.getDashboardOverview();
|
||||
|
||||
// Assert
|
||||
expect(mockApiClient.getDashboardData).toHaveBeenCalledTimes(1);
|
||||
expect(mockDashboardPresenter.present).toHaveBeenCalledWith(mockDto);
|
||||
expect(result).toBe(mockViewModel);
|
||||
expect(mockApiClient.getDashboardData).toHaveBeenCalled();
|
||||
expect(result).toBeInstanceOf(AnalyticsDashboardViewModel);
|
||||
expect(result.totalUsers).toBe(200);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Constructor Dependency Injection', () => {
|
||||
it('should require apiClient, dashboardPresenter, and metricsPresenter', () => {
|
||||
// This test verifies the constructor signature
|
||||
expect(() => {
|
||||
new DashboardService(mockApiClient, mockDashboardPresenter, mockMetricsPresenter);
|
||||
}).not.toThrow();
|
||||
});
|
||||
|
||||
it('should use injected dependencies', async () => {
|
||||
// Arrange
|
||||
const customApiClient = {
|
||||
recordPageView: vi.fn(),
|
||||
recordEngagement: vi.fn(),
|
||||
getDashboardData: vi.fn().mockResolvedValue({
|
||||
totalUsers: 100,
|
||||
activeUsers: 80,
|
||||
totalRaces: 5,
|
||||
totalLeagues: 3,
|
||||
}),
|
||||
getAnalyticsMetrics: vi.fn(),
|
||||
} as unknown as AnalyticsApiClient;
|
||||
|
||||
const customDashboardPresenter = {
|
||||
present: vi.fn().mockReturnValue({} as AnalyticsDashboardViewModel),
|
||||
} as unknown as AnalyticsDashboardPresenter;
|
||||
|
||||
const customMetricsPresenter = {
|
||||
present: vi.fn(),
|
||||
} as unknown as AnalyticsMetricsPresenter;
|
||||
|
||||
const customService = new DashboardService(customApiClient, customDashboardPresenter, customMetricsPresenter);
|
||||
|
||||
// Act
|
||||
await customService.getDashboardData();
|
||||
|
||||
// Assert
|
||||
expect(customApiClient.getDashboardData).toHaveBeenCalledTimes(1);
|
||||
expect(customDashboardPresenter.present).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,50 +1,30 @@
|
||||
import { AnalyticsApiClient } from '../../api/analytics/AnalyticsApiClient';
|
||||
import { AnalyticsDashboardPresenter } from '../../presenters/AnalyticsDashboardPresenter';
|
||||
import { AnalyticsMetricsPresenter } from '../../presenters/AnalyticsMetricsPresenter';
|
||||
import type { AnalyticsDashboardViewModel, AnalyticsMetricsViewModel } from '../../view-models';
|
||||
import { AnalyticsDashboardViewModel, AnalyticsMetricsViewModel } from '../../view-models';
|
||||
|
||||
/**
|
||||
* Dashboard Service
|
||||
*
|
||||
* Orchestrates dashboard operations by coordinating API calls and presentation logic.
|
||||
* Orchestrates dashboard operations by coordinating API calls and view model creation.
|
||||
* All dependencies are injected via constructor.
|
||||
*/
|
||||
export class DashboardService {
|
||||
constructor(
|
||||
private readonly apiClient: AnalyticsApiClient,
|
||||
private readonly analyticsDashboardPresenter: AnalyticsDashboardPresenter,
|
||||
private readonly analyticsMetricsPresenter: AnalyticsMetricsPresenter
|
||||
private readonly apiClient: AnalyticsApiClient
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Get dashboard data with presentation transformation
|
||||
* Get dashboard data with view model transformation
|
||||
*/
|
||||
async getDashboardData(): Promise<AnalyticsDashboardViewModel> {
|
||||
try {
|
||||
const dto = await this.apiClient.getDashboardData();
|
||||
return this.analyticsDashboardPresenter.present(dto);
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
const dto = await this.apiClient.getDashboardData();
|
||||
return new AnalyticsDashboardViewModel(dto);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get analytics metrics with presentation transformation
|
||||
* Get analytics metrics with view model transformation
|
||||
*/
|
||||
async getAnalyticsMetrics(): Promise<AnalyticsMetricsViewModel> {
|
||||
try {
|
||||
const dto = await this.apiClient.getAnalyticsMetrics();
|
||||
return this.analyticsMetricsPresenter.present(dto);
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get dashboard overview (legacy method for backward compatibility)
|
||||
* TODO: Remove when no longer needed
|
||||
*/
|
||||
async getDashboardOverview(): Promise<AnalyticsDashboardViewModel> {
|
||||
return this.getDashboardData();
|
||||
const dto = await this.apiClient.getAnalyticsMetrics();
|
||||
return new AnalyticsMetricsViewModel(dto);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user