view models
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import { describe, it, expect, vi, Mocked } from 'vitest';
|
||||
import { AnalyticsService } from './AnalyticsService';
|
||||
import { AnalyticsApiClient } from '../../api/analytics/AnalyticsApiClient';
|
||||
import { RecordPageViewInputViewModel } from '../../view-models/RecordPageViewInputViewModel';
|
||||
import { RecordEngagementInputViewModel } from '../../view-models/RecordEngagementInputViewModel';
|
||||
import { RecordPageViewOutputViewModel } from '../../view-models/RecordPageViewOutputViewModel';
|
||||
import { RecordEngagementOutputViewModel } from '../../view-models/RecordEngagementOutputViewModel';
|
||||
|
||||
describe('AnalyticsService', () => {
|
||||
let mockApiClient: Mocked<AnalyticsApiClient>;
|
||||
@@ -19,10 +19,10 @@ describe('AnalyticsService', () => {
|
||||
|
||||
describe('recordPageView', () => {
|
||||
it('should call apiClient.recordPageView with correct input', async () => {
|
||||
const input = new RecordPageViewInputViewModel({
|
||||
const input = {
|
||||
path: '/dashboard',
|
||||
userId: 'user-123',
|
||||
});
|
||||
};
|
||||
|
||||
const expectedOutput = { pageViewId: 'pv-123' };
|
||||
mockApiClient.recordPageView.mockResolvedValue(expectedOutput);
|
||||
@@ -33,13 +33,14 @@ describe('AnalyticsService', () => {
|
||||
path: '/dashboard',
|
||||
userId: 'user-123',
|
||||
});
|
||||
expect(result).toEqual(expectedOutput);
|
||||
expect(result).toBeInstanceOf(RecordPageViewOutputViewModel);
|
||||
expect(result.pageViewId).toEqual('pv-123');
|
||||
});
|
||||
|
||||
it('should call apiClient.recordPageView without userId when not provided', async () => {
|
||||
const input = new RecordPageViewInputViewModel({
|
||||
const input = {
|
||||
path: '/home',
|
||||
});
|
||||
};
|
||||
|
||||
const expectedOutput = { pageViewId: 'pv-456' };
|
||||
mockApiClient.recordPageView.mockResolvedValue(expectedOutput);
|
||||
@@ -49,17 +50,18 @@ describe('AnalyticsService', () => {
|
||||
expect(mockApiClient.recordPageView).toHaveBeenCalledWith({
|
||||
path: '/home',
|
||||
});
|
||||
expect(result).toEqual(expectedOutput);
|
||||
expect(result).toBeInstanceOf(RecordPageViewOutputViewModel);
|
||||
expect(result.pageViewId).toEqual('pv-456');
|
||||
});
|
||||
});
|
||||
|
||||
describe('recordEngagement', () => {
|
||||
it('should call apiClient.recordEngagement with correct input', async () => {
|
||||
const input = new RecordEngagementInputViewModel({
|
||||
const input = {
|
||||
eventType: 'button_click',
|
||||
userId: 'user-123',
|
||||
metadata: { buttonId: 'submit', page: '/form' },
|
||||
});
|
||||
};
|
||||
|
||||
const expectedOutput = { eventId: 'event-123', engagementWeight: 1.5 };
|
||||
mockApiClient.recordEngagement.mockResolvedValue(expectedOutput);
|
||||
@@ -71,13 +73,15 @@ describe('AnalyticsService', () => {
|
||||
userId: 'user-123',
|
||||
metadata: { buttonId: 'submit', page: '/form' },
|
||||
});
|
||||
expect(result).toEqual(expectedOutput);
|
||||
expect(result).toBeInstanceOf(RecordEngagementOutputViewModel);
|
||||
expect(result.eventId).toEqual('event-123');
|
||||
expect(result.engagementWeight).toEqual(1.5);
|
||||
});
|
||||
|
||||
it('should call apiClient.recordEngagement without optional fields', async () => {
|
||||
const input = new RecordEngagementInputViewModel({
|
||||
const input = {
|
||||
eventType: 'page_load',
|
||||
});
|
||||
};
|
||||
|
||||
const expectedOutput = { eventId: 'event-456', engagementWeight: 0.5 };
|
||||
mockApiClient.recordEngagement.mockResolvedValue(expectedOutput);
|
||||
@@ -87,7 +91,9 @@ describe('AnalyticsService', () => {
|
||||
expect(mockApiClient.recordEngagement).toHaveBeenCalledWith({
|
||||
eventType: 'page_load',
|
||||
});
|
||||
expect(result).toEqual(expectedOutput);
|
||||
expect(result).toBeInstanceOf(RecordEngagementOutputViewModel);
|
||||
expect(result.eventId).toEqual('event-456');
|
||||
expect(result.engagementWeight).toEqual(0.5);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user