refactor use cases
This commit is contained in:
@@ -2,10 +2,13 @@ import { describe, it, expect, vi, type Mock } from 'vitest';
|
||||
import {
|
||||
MarkNotificationReadUseCase,
|
||||
type MarkNotificationReadCommand,
|
||||
type MarkNotificationReadResult,
|
||||
MarkAllNotificationsReadUseCase,
|
||||
type MarkAllNotificationsReadInput,
|
||||
DismissNotificationUseCase,
|
||||
type DismissNotificationCommand,
|
||||
} from './MarkNotificationReadUseCase';
|
||||
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';
|
||||
@@ -16,14 +19,9 @@ 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(() => {
|
||||
@@ -40,13 +38,8 @@ 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,
|
||||
);
|
||||
});
|
||||
@@ -65,7 +58,6 @@ describe('MarkNotificationReadUseCase', () => {
|
||||
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('returns RECIPIENT_MISMATCH when recipientId does not match', async () => {
|
||||
@@ -90,10 +82,9 @@ describe('MarkNotificationReadUseCase', () => {
|
||||
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 and presents result', async () => {
|
||||
it('marks notification as read when unread and returns success result', async () => {
|
||||
const notification = Notification.create({
|
||||
id: 'n1',
|
||||
recipientId: 'driver-1',
|
||||
@@ -114,10 +105,220 @@ describe('MarkNotificationReadUseCase', () => {
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(notificationRepository.update).toHaveBeenCalled();
|
||||
expect(output.present).toHaveBeenCalledWith({
|
||||
|
||||
const successResult = result.unwrap();
|
||||
expect(successResult.notificationId).toBe('n1');
|
||||
expect(successResult.recipientId).toBe('driver-1');
|
||||
expect(successResult.wasAlreadyRead).toBe(false);
|
||||
});
|
||||
|
||||
it('returns already read when notification is already read', async () => {
|
||||
const notification = Notification.create({
|
||||
id: 'n1',
|
||||
recipientId: 'driver-1',
|
||||
type: 'system_announcement',
|
||||
title: 'Test',
|
||||
body: 'Body',
|
||||
channel: 'in_app',
|
||||
});
|
||||
|
||||
// Mark as read
|
||||
const readNotification = notification.markAsRead();
|
||||
notificationRepository.findById.mockResolvedValue(readNotification);
|
||||
|
||||
const command: MarkNotificationReadCommand = {
|
||||
notificationId: 'n1',
|
||||
recipientId: 'driver-1',
|
||||
wasAlreadyRead: false,
|
||||
});
|
||||
};
|
||||
|
||||
const result = await useCase.execute(command);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(notificationRepository.update).not.toHaveBeenCalled();
|
||||
|
||||
const successResult = result.unwrap();
|
||||
expect(successResult.wasAlreadyRead).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('MarkAllNotificationsReadUseCase', () => {
|
||||
let notificationRepository: NotificationRepositoryMock;
|
||||
let useCase: MarkAllNotificationsReadUseCase;
|
||||
|
||||
beforeEach(() => {
|
||||
notificationRepository = {
|
||||
findById: vi.fn(),
|
||||
update: vi.fn(),
|
||||
markAllAsReadByRecipientId: vi.fn(),
|
||||
};
|
||||
|
||||
useCase = new MarkAllNotificationsReadUseCase(
|
||||
notificationRepository as unknown as INotificationRepository,
|
||||
);
|
||||
});
|
||||
|
||||
it('marks all notifications as read', async () => {
|
||||
const input: MarkAllNotificationsReadInput = {
|
||||
recipientId: 'driver-1',
|
||||
};
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(notificationRepository.markAllAsReadByRecipientId).toHaveBeenCalledWith('driver-1');
|
||||
expect(result.isOk()).toBe(true);
|
||||
|
||||
const successResult = result.unwrap();
|
||||
expect(successResult.recipientId).toBe('driver-1');
|
||||
});
|
||||
|
||||
it('handles repository errors', async () => {
|
||||
notificationRepository.markAllAsReadByRecipientId.mockRejectedValue(new Error('DB error'));
|
||||
|
||||
const input: MarkAllNotificationsReadInput = {
|
||||
recipientId: 'driver-1',
|
||||
};
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<'REPOSITORY_ERROR', { message: string }>;
|
||||
expect(err.code).toBe('REPOSITORY_ERROR');
|
||||
});
|
||||
});
|
||||
|
||||
describe('DismissNotificationUseCase', () => {
|
||||
let notificationRepository: NotificationRepositoryMock;
|
||||
let useCase: DismissNotificationUseCase;
|
||||
|
||||
beforeEach(() => {
|
||||
notificationRepository = {
|
||||
findById: vi.fn(),
|
||||
update: vi.fn(),
|
||||
markAllAsReadByRecipientId: vi.fn(),
|
||||
};
|
||||
|
||||
useCase = new DismissNotificationUseCase(
|
||||
notificationRepository as unknown as INotificationRepository,
|
||||
);
|
||||
});
|
||||
|
||||
it('returns NOTIFICATION_NOT_FOUND when notification is not found', async () => {
|
||||
notificationRepository.findById.mockResolvedValue(null);
|
||||
|
||||
const command: DismissNotificationCommand = {
|
||||
notificationId: 'n1',
|
||||
recipientId: 'driver-1',
|
||||
};
|
||||
|
||||
const result = await useCase.execute(command);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<'NOTIFICATION_NOT_FOUND', { message: string }>;
|
||||
expect(err.code).toBe('NOTIFICATION_NOT_FOUND');
|
||||
});
|
||||
|
||||
it('returns RECIPIENT_MISMATCH when recipientId does not match', async () => {
|
||||
const notification = Notification.create({
|
||||
id: 'n1',
|
||||
recipientId: 'driver-2',
|
||||
type: 'system_announcement',
|
||||
title: 'Test',
|
||||
body: 'Body',
|
||||
channel: 'in_app',
|
||||
});
|
||||
|
||||
notificationRepository.findById.mockResolvedValue(notification);
|
||||
|
||||
const command: DismissNotificationCommand = {
|
||||
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');
|
||||
});
|
||||
|
||||
it('dismisses notification and returns success result', async () => {
|
||||
const notification = Notification.create({
|
||||
id: 'n1',
|
||||
recipientId: 'driver-1',
|
||||
type: 'system_announcement',
|
||||
title: 'Test',
|
||||
body: 'Body',
|
||||
channel: 'in_app',
|
||||
});
|
||||
|
||||
notificationRepository.findById.mockResolvedValue(notification);
|
||||
|
||||
const command: DismissNotificationCommand = {
|
||||
notificationId: 'n1',
|
||||
recipientId: 'driver-1',
|
||||
};
|
||||
|
||||
const result = await useCase.execute(command);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(notificationRepository.update).toHaveBeenCalled();
|
||||
|
||||
const successResult = result.unwrap();
|
||||
expect(successResult.notificationId).toBe('n1');
|
||||
expect(successResult.recipientId).toBe('driver-1');
|
||||
expect(successResult.wasAlreadyDismissed).toBe(false);
|
||||
});
|
||||
|
||||
it('returns already dismissed when notification is already dismissed', async () => {
|
||||
const notification = Notification.create({
|
||||
id: 'n1',
|
||||
recipientId: 'driver-1',
|
||||
type: 'system_announcement',
|
||||
title: 'Test',
|
||||
body: 'Body',
|
||||
channel: 'in_app',
|
||||
});
|
||||
|
||||
// Dismiss it
|
||||
const dismissedNotification = notification.dismiss();
|
||||
notificationRepository.findById.mockResolvedValue(dismissedNotification);
|
||||
|
||||
const command: DismissNotificationCommand = {
|
||||
notificationId: 'n1',
|
||||
recipientId: 'driver-1',
|
||||
};
|
||||
|
||||
const result = await useCase.execute(command);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(notificationRepository.update).not.toHaveBeenCalled();
|
||||
|
||||
const successResult = result.unwrap();
|
||||
expect(successResult.wasAlreadyDismissed).toBe(true);
|
||||
});
|
||||
|
||||
it('returns CANNOT_DISMISS_REQUIRING_RESPONSE when notification requires response', async () => {
|
||||
const notification = Notification.create({
|
||||
id: 'n1',
|
||||
recipientId: 'driver-1',
|
||||
type: 'system_announcement',
|
||||
title: 'Test',
|
||||
body: 'Body',
|
||||
channel: 'in_app',
|
||||
requiresResponse: true,
|
||||
});
|
||||
|
||||
notificationRepository.findById.mockResolvedValue(notification);
|
||||
|
||||
const command: DismissNotificationCommand = {
|
||||
notificationId: 'n1',
|
||||
recipientId: 'driver-1',
|
||||
};
|
||||
|
||||
const result = await useCase.execute(command);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<'CANNOT_DISMISS_REQUIRING_RESPONSE', { message: string }>;
|
||||
expect(err.code).toBe('CANNOT_DISMISS_REQUIRING_RESPONSE');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user