74 lines
2.2 KiB
TypeScript
74 lines
2.2 KiB
TypeScript
import { describe, it, expect, vi, type Mock } from 'vitest';
|
|
import { GetAnalyticsMetricsUseCase, type GetAnalyticsMetricsInput } from './GetAnalyticsMetricsUseCase';
|
|
import type { IPageViewRepository } from '../../domain/repositories/IPageViewRepository';
|
|
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
|
|
import { Result } from '@core/shared/application/Result';
|
|
|
|
describe('GetAnalyticsMetricsUseCase', () => {
|
|
let pageViewRepository: {
|
|
save: Mock;
|
|
findById: Mock;
|
|
findByEntityId: Mock;
|
|
findBySessionId: Mock;
|
|
countByEntityId: Mock;
|
|
getUniqueVisitorsCount: Mock;
|
|
getAverageSessionDuration: Mock;
|
|
getBounceRate: Mock;
|
|
};
|
|
let logger: Logger;
|
|
let output: UseCaseOutputPort<Result<any, any>> & { present: Mock };
|
|
let useCase: GetAnalyticsMetricsUseCase;
|
|
|
|
beforeEach(() => {
|
|
pageViewRepository = {
|
|
save: vi.fn(),
|
|
findById: vi.fn(),
|
|
findByEntityId: vi.fn(),
|
|
findBySessionId: vi.fn(),
|
|
countByEntityId: vi.fn(),
|
|
getUniqueVisitorsCount: vi.fn(),
|
|
getAverageSessionDuration: vi.fn(),
|
|
getBounceRate: vi.fn(),
|
|
} as unknown as IPageViewRepository as any;
|
|
|
|
logger = {
|
|
debug: vi.fn(),
|
|
info: vi.fn(),
|
|
warn: vi.fn(),
|
|
error: vi.fn(),
|
|
} as unknown as Logger;
|
|
|
|
output = {
|
|
present: vi.fn(),
|
|
};
|
|
|
|
useCase = new GetAnalyticsMetricsUseCase(
|
|
pageViewRepository as unknown as IPageViewRepository,
|
|
output,
|
|
logger,
|
|
);
|
|
});
|
|
|
|
it('presents default metrics and logs retrieval when no input is provided', async () => {
|
|
await useCase.execute();
|
|
|
|
expect((logger.info as unknown as Mock)).toHaveBeenCalled();
|
|
});
|
|
|
|
it('uses provided date range and presents error when execute throws', async () => {
|
|
const input: GetAnalyticsMetricsInput = {
|
|
startDate: new Date('2024-01-01'),
|
|
endDate: new Date('2024-01-31'),
|
|
};
|
|
|
|
// Simulate an error by temporarily spying on logger.info to throw
|
|
(logger.info as unknown as Mock).mockImplementation(() => {
|
|
throw new Error('Logging failed');
|
|
});
|
|
|
|
await useCase.execute(input);
|
|
|
|
expect((logger.error as unknown as Mock)).toHaveBeenCalled();
|
|
});
|
|
});
|