view models

This commit is contained in:
2025-12-18 01:20:23 +01:00
parent 7c449af311
commit cc2553876a
216 changed files with 485 additions and 10179 deletions

View File

@@ -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);
});
});
});