refactor use cases
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { describe, it, expect, vi, type Mock } from 'vitest';
|
||||
import { RecordPageViewUseCase, type RecordPageViewInput, type RecordPageViewOutput } from './RecordPageViewUseCase';
|
||||
import { RecordPageViewUseCase, type RecordPageViewInput } from './RecordPageViewUseCase';
|
||||
import { PageView } from '../../domain/entities/PageView';
|
||||
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
|
||||
import type { Logger } from '@core/shared/application';
|
||||
import type { EntityType, VisitorType } from '../../domain/types/PageView';
|
||||
|
||||
describe('RecordPageViewUseCase', () => {
|
||||
@@ -9,7 +9,6 @@ describe('RecordPageViewUseCase', () => {
|
||||
save: Mock;
|
||||
};
|
||||
let logger: Logger;
|
||||
let output: UseCaseOutputPort<RecordPageViewOutput> & { present: Mock };
|
||||
let useCase: RecordPageViewUseCase;
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -26,18 +25,13 @@ describe('RecordPageViewUseCase', () => {
|
||||
error: vi.fn(),
|
||||
} as unknown as Logger;
|
||||
|
||||
output = {
|
||||
present: vi.fn(),
|
||||
};
|
||||
|
||||
useCase = new RecordPageViewUseCase(
|
||||
pageViewRepository as unknown as PageViewRepository,
|
||||
logger,
|
||||
output,
|
||||
);
|
||||
});
|
||||
|
||||
it('creates and saves a PageView and presents its id', async () => {
|
||||
it('creates and saves a PageView and returns its id', async () => {
|
||||
const input: RecordPageViewInput = {
|
||||
entityType: 'league' as EntityType,
|
||||
entityId: 'league-1',
|
||||
@@ -54,6 +48,7 @@ describe('RecordPageViewUseCase', () => {
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
const data = result.unwrap();
|
||||
expect(pageViewRepository.save).toHaveBeenCalledTimes(1);
|
||||
const saved = (pageViewRepository.save as unknown as Mock).mock.calls?.[0]?.[0] as PageView;
|
||||
|
||||
@@ -62,13 +57,11 @@ describe('RecordPageViewUseCase', () => {
|
||||
expect(saved.entityId).toBe(input.entityId);
|
||||
expect(saved.entityType).toBe(input.entityType);
|
||||
|
||||
expect(output.present).toHaveBeenCalledWith({
|
||||
pageViewId: saved.id,
|
||||
});
|
||||
expect(data.pageViewId).toBe(saved.id);
|
||||
expect((logger.info as unknown as Mock)).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('logs and presents error when repository save fails', async () => {
|
||||
it('logs and returns error when repository save fails', async () => {
|
||||
const input: RecordPageViewInput = {
|
||||
entityType: 'league' as EntityType,
|
||||
entityId: 'league-1',
|
||||
|
||||
Reference in New Issue
Block a user