Files
gridpilot.gg/core/notifications/application/use-cases/MarkNotificationReadUseCase.test.ts
2025-12-20 12:55:07 +01:00

84 lines
2.5 KiB
TypeScript

import { describe, it, expect, vi, type Mock } from 'vitest';
import { MarkNotificationReadUseCase } from './MarkNotificationReadUseCase';
import type { INotificationRepository } from '../../domain/repositories/INotificationRepository';
import type { Logger } from '@core/shared/application';
import { Notification } from '../../domain/entities/Notification';
import { NotificationDomainError } from '../../domain/errors/NotificationDomainError';
interface NotificationRepositoryMock {
findById: Mock;
update: Mock;
markAllAsReadByRecipientId: Mock;
}
describe('MarkNotificationReadUseCase', () => {
let notificationRepository: NotificationRepositoryMock;
let logger: Logger;
let useCase: MarkNotificationReadUseCase;
beforeEach(() => {
notificationRepository = {
findById: vi.fn(),
update: vi.fn(),
markAllAsReadByRecipientId: vi.fn(),
} as unknown as INotificationRepository as any;
logger = {
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
} as unknown as Logger;
useCase = new MarkNotificationReadUseCase(
notificationRepository as unknown as INotificationRepository,
logger,
);
});
it('throws when notification is not found', async () => {
notificationRepository.findById.mockResolvedValue(null);
await expect(
useCase.execute({ notificationId: 'n1', recipientId: 'driver-1' }),
).rejects.toThrow(NotificationDomainError);
expect((logger.warn as unknown as Mock)).toHaveBeenCalled();
});
it('throws when recipientId does not match', async () => {
const notification = Notification.create({
id: 'n1',
recipientId: 'driver-2',
type: 'info',
title: 'Test',
body: 'Body',
channel: 'in_app',
});
notificationRepository.findById.mockResolvedValue(notification);
await expect(
useCase.execute({ notificationId: 'n1', recipientId: 'driver-1' }),
).rejects.toThrow(NotificationDomainError);
});
it('marks notification as read when unread', async () => {
const notification = Notification.create({
id: 'n1',
recipientId: 'driver-1',
type: 'info',
title: 'Test',
body: 'Body',
channel: 'in_app',
});
notificationRepository.findById.mockResolvedValue(notification);
await useCase.execute({ notificationId: 'n1', recipientId: 'driver-1' });
expect(notificationRepository.update).toHaveBeenCalled();
expect((logger.info as unknown as Mock)).toHaveBeenCalled();
});
});