refactor use cases
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
import { describe, it, expect, vi, type Mock } from 'vitest';
|
||||
import { GetAnalyticsMetricsUseCase, type GetAnalyticsMetricsInput, type GetAnalyticsMetricsOutput } from './GetAnalyticsMetricsUseCase';
|
||||
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
|
||||
import { GetAnalyticsMetricsUseCase, type GetAnalyticsMetricsInput } from './GetAnalyticsMetricsUseCase';
|
||||
import type { Logger } from '@core/shared/application';
|
||||
|
||||
describe('GetAnalyticsMetricsUseCase', () => {
|
||||
let logger: Logger;
|
||||
let output: UseCaseOutputPort<GetAnalyticsMetricsOutput> & { present: Mock };
|
||||
let useCase: GetAnalyticsMetricsUseCase;
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -15,21 +14,15 @@ describe('GetAnalyticsMetricsUseCase', () => {
|
||||
error: vi.fn(),
|
||||
} as unknown as Logger;
|
||||
|
||||
output = {
|
||||
present: vi.fn(),
|
||||
};
|
||||
|
||||
useCase = new GetAnalyticsMetricsUseCase(
|
||||
logger,
|
||||
output,
|
||||
);
|
||||
useCase = new GetAnalyticsMetricsUseCase(logger);
|
||||
});
|
||||
|
||||
it('presents default metrics and logs retrieval when no input is provided', async () => {
|
||||
it('returns default metrics when no input is provided', async () => {
|
||||
const result = await useCase.execute();
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(output.present).toHaveBeenCalledWith({
|
||||
const data = result.unwrap();
|
||||
expect(data).toEqual({
|
||||
pageViews: 0,
|
||||
uniqueVisitors: 0,
|
||||
averageSessionDuration: 0,
|
||||
@@ -38,7 +31,21 @@ describe('GetAnalyticsMetricsUseCase', () => {
|
||||
expect((logger.info as unknown as Mock)).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('uses provided date range and presents error when execute throws', async () => {
|
||||
it('uses provided date range and returns metrics', async () => {
|
||||
const input: GetAnalyticsMetricsInput = {
|
||||
startDate: new Date('2024-01-01'),
|
||||
endDate: new Date('2024-01-31'),
|
||||
};
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
const data = result.unwrap();
|
||||
expect(data.pageViews).toBe(0);
|
||||
expect((logger.info as unknown as Mock)).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns error when execute throws', async () => {
|
||||
const input: GetAnalyticsMetricsInput = {
|
||||
startDate: new Date('2024-01-01'),
|
||||
endDate: new Date('2024-01-31'),
|
||||
|
||||
Reference in New Issue
Block a user