refactor
This commit is contained in:
@@ -5,6 +5,8 @@ import { AnalyticsService } from './AnalyticsService';
|
||||
import type { Response } from 'express';
|
||||
import { EntityType, VisitorType } from '@core/analytics/domain/types/PageView';
|
||||
import { EngagementAction, EngagementEntityType } from '@core/analytics/domain/types/EngagementEvent';
|
||||
import type { RecordEngagementOutputDTO } from './dtos/RecordEngagementOutputDTO';
|
||||
import type { RecordPageViewOutputDTO } from './dtos/RecordPageViewOutputDTO';
|
||||
|
||||
describe('AnalyticsController', () => {
|
||||
let controller: AnalyticsController;
|
||||
@@ -42,8 +44,8 @@ describe('AnalyticsController', () => {
|
||||
userAgent: 'Mozilla/5.0',
|
||||
country: 'US',
|
||||
};
|
||||
const presenterMock = { viewModel: { pageViewId: 'pv-123' } };
|
||||
service.recordPageView.mockResolvedValue(presenterMock as any);
|
||||
const dto: RecordPageViewOutputDTO = { pageViewId: 'pv-123' };
|
||||
service.recordPageView.mockResolvedValue(dto);
|
||||
|
||||
const mockRes: ReturnType<typeof vi.mocked<Response>> = {
|
||||
status: vi.fn().mockReturnThis(),
|
||||
@@ -54,7 +56,7 @@ describe('AnalyticsController', () => {
|
||||
|
||||
expect(service.recordPageView).toHaveBeenCalledWith(input);
|
||||
expect(mockRes.status).toHaveBeenCalledWith(201);
|
||||
expect(mockRes.json).toHaveBeenCalledWith(presenterMock.viewModel);
|
||||
expect(mockRes.json).toHaveBeenCalledWith(dto);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -69,8 +71,8 @@ describe('AnalyticsController', () => {
|
||||
actorId: 'actor-789',
|
||||
metadata: { key: 'value' },
|
||||
};
|
||||
const presenterMock = { eventId: 'event-123', engagementWeight: 10 };
|
||||
service.recordEngagement.mockResolvedValue(presenterMock as any);
|
||||
const dto: RecordEngagementOutputDTO = { eventId: 'event-123', engagementWeight: 10 };
|
||||
service.recordEngagement.mockResolvedValue(dto);
|
||||
|
||||
const mockRes: ReturnType<typeof vi.mocked<Response>> = {
|
||||
status: vi.fn().mockReturnThis(),
|
||||
@@ -81,45 +83,41 @@ describe('AnalyticsController', () => {
|
||||
|
||||
expect(service.recordEngagement).toHaveBeenCalledWith(input);
|
||||
expect(mockRes.status).toHaveBeenCalledWith(201);
|
||||
expect(mockRes.json).toHaveBeenCalledWith((presenterMock as any).viewModel);
|
||||
expect(mockRes.json).toHaveBeenCalledWith(dto);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getDashboardData', () => {
|
||||
it('should return dashboard data', async () => {
|
||||
const presenterMock = {
|
||||
viewModel: {
|
||||
totalUsers: 100,
|
||||
activeUsers: 50,
|
||||
totalRaces: 20,
|
||||
totalLeagues: 5,
|
||||
},
|
||||
const dto = {
|
||||
totalUsers: 100,
|
||||
activeUsers: 50,
|
||||
totalRaces: 20,
|
||||
totalLeagues: 5,
|
||||
};
|
||||
service.getDashboardData.mockResolvedValue(presenterMock as any);
|
||||
service.getDashboardData.mockResolvedValue(dto);
|
||||
|
||||
const result = await controller.getDashboardData();
|
||||
|
||||
expect(service.getDashboardData).toHaveBeenCalled();
|
||||
expect(result).toEqual(presenterMock.viewModel);
|
||||
expect(result).toEqual(dto);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getAnalyticsMetrics', () => {
|
||||
it('should return analytics metrics', async () => {
|
||||
const presenterMock = {
|
||||
viewModel: {
|
||||
pageViews: 1000,
|
||||
uniqueVisitors: 500,
|
||||
averageSessionDuration: 300,
|
||||
bounceRate: 0.4,
|
||||
},
|
||||
const dto = {
|
||||
pageViews: 1000,
|
||||
uniqueVisitors: 500,
|
||||
averageSessionDuration: 300,
|
||||
bounceRate: 0.4,
|
||||
};
|
||||
service.getAnalyticsMetrics.mockResolvedValue(presenterMock as any);
|
||||
service.getAnalyticsMetrics.mockResolvedValue(dto);
|
||||
|
||||
const result = await controller.getAnalyticsMetrics();
|
||||
|
||||
expect(service.getAnalyticsMetrics).toHaveBeenCalled();
|
||||
expect(result).toEqual(presenterMock.viewModel);
|
||||
expect(result).toEqual(dto);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user