refactor use cases
This commit is contained in:
@@ -2,10 +2,9 @@ import { describe, it, expect, vi, type Mock } from 'vitest';
|
||||
import {
|
||||
GetUnreadNotificationsUseCase,
|
||||
type GetUnreadNotificationsInput,
|
||||
type GetUnreadNotificationsResult,
|
||||
} from './GetUnreadNotificationsUseCase';
|
||||
import type { INotificationRepository } from '../../domain/repositories/INotificationRepository';
|
||||
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
|
||||
import type { Logger } from '@core/shared/application';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import { Notification } from '../../domain/entities/Notification';
|
||||
@@ -14,14 +13,9 @@ interface NotificationRepositoryMock {
|
||||
findUnreadByRecipientId: Mock;
|
||||
}
|
||||
|
||||
interface OutputPortMock extends UseCaseOutputPort<GetUnreadNotificationsResult> {
|
||||
present: Mock;
|
||||
}
|
||||
|
||||
describe('GetUnreadNotificationsUseCase', () => {
|
||||
let notificationRepository: NotificationRepositoryMock;
|
||||
let logger: Logger;
|
||||
let output: OutputPortMock;
|
||||
let useCase: GetUnreadNotificationsUseCase;
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -36,13 +30,8 @@ describe('GetUnreadNotificationsUseCase', () => {
|
||||
error: vi.fn(),
|
||||
} as unknown as Logger;
|
||||
|
||||
output = {
|
||||
present: vi.fn(),
|
||||
} as unknown as OutputPortMock;
|
||||
|
||||
useCase = new GetUnreadNotificationsUseCase(
|
||||
notificationRepository as unknown as INotificationRepository,
|
||||
output,
|
||||
logger,
|
||||
);
|
||||
});
|
||||
@@ -69,10 +58,10 @@ describe('GetUnreadNotificationsUseCase', () => {
|
||||
expect(notificationRepository.findUnreadByRecipientId).toHaveBeenCalledWith(recipientId);
|
||||
expect(result).toBeInstanceOf(Result);
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(output.present).toHaveBeenCalledWith({
|
||||
notifications,
|
||||
totalCount: 1,
|
||||
});
|
||||
|
||||
const successResult = result.unwrap();
|
||||
expect(successResult.notifications).toEqual(notifications);
|
||||
expect(successResult.totalCount).toBe(1);
|
||||
});
|
||||
|
||||
it('handles repository errors by logging and returning error result', async () => {
|
||||
@@ -89,6 +78,5 @@ describe('GetUnreadNotificationsUseCase', () => {
|
||||
expect(err.code).toBe('REPOSITORY_ERROR');
|
||||
expect(err.details.message).toBe('DB error');
|
||||
expect((logger.error as unknown as Mock)).toHaveBeenCalled();
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user