refactor
This commit is contained in:
@@ -2,14 +2,16 @@ import { describe, it, expect, vi, type Mock } from 'vitest';
|
||||
import { RecordEngagementUseCase, type RecordEngagementInput } from './RecordEngagementUseCase';
|
||||
import type { IEngagementRepository } from '../../domain/repositories/IEngagementRepository';
|
||||
import { EngagementEvent } from '../../domain/entities/EngagementEvent';
|
||||
import type { Logger } from '@core/shared/application';
|
||||
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
|
||||
import type { EngagementAction, EngagementEntityType } from '../../domain/types/EngagementEvent';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
|
||||
describe('RecordEngagementUseCase', () => {
|
||||
let engagementRepository: {
|
||||
save: Mock;
|
||||
};
|
||||
let logger: Logger;
|
||||
let output: UseCaseOutputPort<Result<any, any>> & { present: Mock };
|
||||
let useCase: RecordEngagementUseCase;
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -24,13 +26,18 @@ describe('RecordEngagementUseCase', () => {
|
||||
error: vi.fn(),
|
||||
} as unknown as Logger;
|
||||
|
||||
output = {
|
||||
present: vi.fn(),
|
||||
};
|
||||
|
||||
useCase = new RecordEngagementUseCase(
|
||||
engagementRepository as unknown as IEngagementRepository,
|
||||
output,
|
||||
logger,
|
||||
);
|
||||
});
|
||||
|
||||
it('creates and saves an EngagementEvent and returns its id and weight', async () => {
|
||||
it('creates and saves an EngagementEvent and presents its id and weight', async () => {
|
||||
const input: RecordEngagementInput = {
|
||||
action: 'view' as EngagementAction,
|
||||
entityType: 'league' as EngagementEntityType,
|
||||
@@ -43,7 +50,7 @@ describe('RecordEngagementUseCase', () => {
|
||||
|
||||
engagementRepository.save.mockResolvedValue(undefined);
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
await useCase.execute(input);
|
||||
|
||||
expect(engagementRepository.save).toHaveBeenCalledTimes(1);
|
||||
const saved = (engagementRepository.save as unknown as Mock).mock.calls[0][0] as EngagementEvent;
|
||||
@@ -52,12 +59,11 @@ describe('RecordEngagementUseCase', () => {
|
||||
expect(saved.id).toBeDefined();
|
||||
expect(saved.entityId).toBe(input.entityId);
|
||||
expect(saved.entityType).toBe(input.entityType);
|
||||
expect(result.eventId).toBe(saved.id);
|
||||
expect(typeof result.engagementWeight).toBe('number');
|
||||
|
||||
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: RecordEngagementInput = {
|
||||
action: 'view' as EngagementAction,
|
||||
entityType: 'league' as EngagementEntityType,
|
||||
@@ -69,7 +75,8 @@ describe('RecordEngagementUseCase', () => {
|
||||
const error = new Error('DB error');
|
||||
engagementRepository.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