refactor
This commit is contained in:
@@ -2,14 +2,16 @@ import { describe, it, expect, vi, type Mock } from 'vitest';
|
||||
import { RecordPageViewUseCase, type RecordPageViewInput } from './RecordPageViewUseCase';
|
||||
import type { IPageViewRepository } from '../../domain/repositories/IPageViewRepository';
|
||||
import { PageView } from '../../domain/entities/PageView';
|
||||
import type { Logger } from '@core/shared/application';
|
||||
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
|
||||
import type { EntityType, VisitorType } from '../../domain/types/PageView';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
|
||||
describe('RecordPageViewUseCase', () => {
|
||||
let pageViewRepository: {
|
||||
save: Mock;
|
||||
};
|
||||
let logger: Logger;
|
||||
let output: UseCaseOutputPort<Result<any, any>> & { present: Mock };
|
||||
let useCase: RecordPageViewUseCase;
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -24,13 +26,18 @@ describe('RecordPageViewUseCase', () => {
|
||||
error: vi.fn(),
|
||||
} as unknown as Logger;
|
||||
|
||||
output = {
|
||||
present: vi.fn(),
|
||||
};
|
||||
|
||||
useCase = new RecordPageViewUseCase(
|
||||
pageViewRepository as unknown as IPageViewRepository,
|
||||
output,
|
||||
logger,
|
||||
);
|
||||
});
|
||||
|
||||
it('creates and saves a PageView and returns its id', async () => {
|
||||
it('creates and saves a PageView and presents its id', async () => {
|
||||
const input: RecordPageViewInput = {
|
||||
entityType: 'league' as EntityType,
|
||||
entityId: 'league-1',
|
||||
@@ -44,7 +51,7 @@ describe('RecordPageViewUseCase', () => {
|
||||
|
||||
pageViewRepository.save.mockResolvedValue(undefined);
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
await useCase.execute(input);
|
||||
|
||||
expect(pageViewRepository.save).toHaveBeenCalledTimes(1);
|
||||
const saved = (pageViewRepository.save as unknown as Mock).mock.calls[0][0] as PageView;
|
||||
@@ -53,11 +60,11 @@ describe('RecordPageViewUseCase', () => {
|
||||
expect(saved.id).toBeDefined();
|
||||
expect(saved.entityId).toBe(input.entityId);
|
||||
expect(saved.entityType).toBe(input.entityType);
|
||||
expect(result.pageViewId).toBe(saved.id);
|
||||
|
||||
expect((logger.info as unknown as Mock)).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('logs and rethrows when repository save fails', async () => {
|
||||
it('logs and presents error when repository save fails', async () => {
|
||||
const input: RecordPageViewInput = {
|
||||
entityType: 'league' as EntityType,
|
||||
entityId: 'league-1',
|
||||
@@ -68,7 +75,8 @@ describe('RecordPageViewUseCase', () => {
|
||||
const error = new Error('DB error');
|
||||
pageViewRepository.save.mockRejectedValue(error);
|
||||
|
||||
await expect(useCase.execute(input)).rejects.toThrow('DB error');
|
||||
await useCase.execute(input);
|
||||
|
||||
expect((logger.error as unknown as Mock)).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user