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

65 lines
2.0 KiB
TypeScript

import { describe, it, expect, vi, type Mock } from 'vitest';
import { GetUnreadNotificationsUseCase } from './GetUnreadNotificationsUseCase';
import type { INotificationRepository } from '../../domain/repositories/INotificationRepository';
import type { Logger } from '@core/shared/application';
import { Notification } from '../../domain/entities/Notification';
interface NotificationRepositoryMock {
findUnreadByRecipientId: Mock;
}
describe('GetUnreadNotificationsUseCase', () => {
let notificationRepository: NotificationRepositoryMock;
let logger: Logger;
let useCase: GetUnreadNotificationsUseCase;
beforeEach(() => {
notificationRepository = {
findUnreadByRecipientId: 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 GetUnreadNotificationsUseCase(
notificationRepository as unknown as INotificationRepository,
logger,
);
});
it('returns unread notifications and total count', async () => {
const recipientId = 'driver-1';
const notifications: Notification[] = [
Notification.create({
id: 'n1',
recipientId,
type: 'info',
title: 'Test',
body: 'Body',
channel: 'in_app',
}),
];
notificationRepository.findUnreadByRecipientId.mockResolvedValue(notifications);
const result = await useCase.execute(recipientId);
expect(notificationRepository.findUnreadByRecipientId).toHaveBeenCalledWith(recipientId);
expect(result.notifications).toEqual(notifications);
expect(result.totalCount).toBe(1);
});
it('handles repository errors by logging and rethrowing', async () => {
const recipientId = 'driver-1';
const error = new Error('DB error');
notificationRepository.findUnreadByRecipientId.mockRejectedValue(error);
await expect(useCase.execute(recipientId)).rejects.toThrow('DB error');
expect((logger.error as unknown as Mock)).toHaveBeenCalled();
});
});