refactor
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
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 } from '@core/shared/application';
|
||||
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
|
||||
describe('GetAnalyticsMetricsUseCase', () => {
|
||||
let pageViewRepository: {
|
||||
@@ -15,6 +16,7 @@ describe('GetAnalyticsMetricsUseCase', () => {
|
||||
getBounceRate: Mock;
|
||||
};
|
||||
let logger: Logger;
|
||||
let output: UseCaseOutputPort<Result<any, any>> & { present: Mock };
|
||||
let useCase: GetAnalyticsMetricsUseCase;
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -36,42 +38,36 @@ describe('GetAnalyticsMetricsUseCase', () => {
|
||||
error: vi.fn(),
|
||||
} as unknown as Logger;
|
||||
|
||||
output = {
|
||||
present: vi.fn(),
|
||||
};
|
||||
|
||||
useCase = new GetAnalyticsMetricsUseCase(
|
||||
pageViewRepository as unknown as IPageViewRepository,
|
||||
output,
|
||||
logger,
|
||||
);
|
||||
});
|
||||
|
||||
it('returns default metrics and logs retrieval when no input is provided', async () => {
|
||||
const result = await useCase.execute();
|
||||
|
||||
expect(result).toEqual({
|
||||
pageViews: 0,
|
||||
uniqueVisitors: 0,
|
||||
averageSessionDuration: 0,
|
||||
bounceRate: 0,
|
||||
});
|
||||
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 logs error when execute throws', async () => {
|
||||
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'),
|
||||
};
|
||||
|
||||
const erroringUseCase = new GetAnalyticsMetricsUseCase(
|
||||
pageViewRepository as unknown as IPageViewRepository,
|
||||
logger,
|
||||
);
|
||||
|
||||
// Simulate an error by temporarily spying on logger.info to throw
|
||||
(logger.info as unknown as Mock).mockImplementation(() => {
|
||||
throw new Error('Logging failed');
|
||||
});
|
||||
|
||||
await expect(erroringUseCase.execute(input)).rejects.toThrow('Logging failed');
|
||||
await useCase.execute(input);
|
||||
|
||||
expect((logger.error as unknown as Mock)).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user