refactor use cases
This commit is contained in:
@@ -1,9 +1,14 @@
|
||||
import { describe, it, expect, vi, type Mock } from 'vitest';
|
||||
import { MarkNotificationReadUseCase } from './MarkNotificationReadUseCase';
|
||||
import {
|
||||
MarkNotificationReadUseCase,
|
||||
type MarkNotificationReadCommand,
|
||||
type MarkNotificationReadResult,
|
||||
} from './MarkNotificationReadUseCase';
|
||||
import type { INotificationRepository } from '../../domain/repositories/INotificationRepository';
|
||||
import type { Logger } from '@core/shared/application';
|
||||
import type { Logger, UseCaseOutputPort } 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';
|
||||
import { NotificationDomainError } from '../../domain/errors/NotificationDomainError';
|
||||
|
||||
interface NotificationRepositoryMock {
|
||||
findById: Mock;
|
||||
@@ -11,9 +16,14 @@ interface NotificationRepositoryMock {
|
||||
markAllAsReadByRecipientId: Mock;
|
||||
}
|
||||
|
||||
interface OutputPortMock extends UseCaseOutputPort<MarkNotificationReadResult> {
|
||||
present: Mock;
|
||||
}
|
||||
|
||||
describe('MarkNotificationReadUseCase', () => {
|
||||
let notificationRepository: NotificationRepositoryMock;
|
||||
let logger: Logger;
|
||||
let output: OutputPortMock;
|
||||
let useCase: MarkNotificationReadUseCase;
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -30,27 +40,39 @@ describe('MarkNotificationReadUseCase', () => {
|
||||
error: vi.fn(),
|
||||
} as unknown as Logger;
|
||||
|
||||
output = {
|
||||
present: vi.fn(),
|
||||
} as unknown as OutputPortMock;
|
||||
|
||||
useCase = new MarkNotificationReadUseCase(
|
||||
notificationRepository as unknown as INotificationRepository,
|
||||
output,
|
||||
logger,
|
||||
);
|
||||
});
|
||||
|
||||
it('throws when notification is not found', async () => {
|
||||
it('returns NOTIFICATION_NOT_FOUND when notification is not found', async () => {
|
||||
notificationRepository.findById.mockResolvedValue(null);
|
||||
|
||||
await expect(
|
||||
useCase.execute({ notificationId: 'n1', recipientId: 'driver-1' }),
|
||||
).rejects.toThrow(NotificationDomainError);
|
||||
const command: MarkNotificationReadCommand = {
|
||||
notificationId: 'n1',
|
||||
recipientId: 'driver-1',
|
||||
};
|
||||
|
||||
expect((logger.warn as unknown as Mock)).toHaveBeenCalled();
|
||||
const result = await useCase.execute(command);
|
||||
|
||||
expect(result).toBeInstanceOf(Result);
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<'NOTIFICATION_NOT_FOUND', { message: string }>;
|
||||
expect(err.code).toBe('NOTIFICATION_NOT_FOUND');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('throws when recipientId does not match', async () => {
|
||||
it('returns RECIPIENT_MISMATCH when recipientId does not match', async () => {
|
||||
const notification = Notification.create({
|
||||
id: 'n1',
|
||||
recipientId: 'driver-2',
|
||||
type: 'info',
|
||||
type: 'system_announcement',
|
||||
title: 'Test',
|
||||
body: 'Body',
|
||||
channel: 'in_app',
|
||||
@@ -58,16 +80,24 @@ describe('MarkNotificationReadUseCase', () => {
|
||||
|
||||
notificationRepository.findById.mockResolvedValue(notification);
|
||||
|
||||
await expect(
|
||||
useCase.execute({ notificationId: 'n1', recipientId: 'driver-1' }),
|
||||
).rejects.toThrow(NotificationDomainError);
|
||||
const command: MarkNotificationReadCommand = {
|
||||
notificationId: 'n1',
|
||||
recipientId: 'driver-1',
|
||||
};
|
||||
|
||||
const result = await useCase.execute(command);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<'RECIPIENT_MISMATCH', { message: string }>;
|
||||
expect(err.code).toBe('RECIPIENT_MISMATCH');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('marks notification as read when unread', async () => {
|
||||
it('marks notification as read when unread and presents result', async () => {
|
||||
const notification = Notification.create({
|
||||
id: 'n1',
|
||||
recipientId: 'driver-1',
|
||||
type: 'info',
|
||||
type: 'system_announcement',
|
||||
title: 'Test',
|
||||
body: 'Body',
|
||||
channel: 'in_app',
|
||||
@@ -75,9 +105,19 @@ describe('MarkNotificationReadUseCase', () => {
|
||||
|
||||
notificationRepository.findById.mockResolvedValue(notification);
|
||||
|
||||
await useCase.execute({ notificationId: 'n1', recipientId: 'driver-1' });
|
||||
const command: MarkNotificationReadCommand = {
|
||||
notificationId: 'n1',
|
||||
recipientId: 'driver-1',
|
||||
};
|
||||
|
||||
const result = await useCase.execute(command);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(notificationRepository.update).toHaveBeenCalled();
|
||||
expect((logger.info as unknown as Mock)).toHaveBeenCalled();
|
||||
expect(output.present).toHaveBeenCalledWith({
|
||||
notificationId: 'n1',
|
||||
recipientId: 'driver-1',
|
||||
wasAlreadyRead: false,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user