"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const testing_1 = require("@nestjs/testing"); const core_1 = require("@nestjs/core"); const notifications_controller_1 = require("../notifications.controller"); const notifications_service_1 = require("../services/notifications.service"); const rbac_service_1 = require("../../rbac/services/rbac.service"); describe('NotificationsController', () => { let controller; let service; const mockRequestUser = { id: 'user-123', sub: 'user-123', tenant_id: 'tenant-123', email: 'test@example.com', role: 'admin', }; const mockNotification = { id: 'notif-123', user_id: 'user-123', tenant_id: 'tenant-123', type: 'info', title: 'Test Notification', message: 'This is a test', is_read: false, created_at: new Date('2026-01-01'), }; const mockPreferences = { id: 'pref-123', user_id: 'user-123', tenant_id: 'tenant-123', email_enabled: true, push_enabled: true, in_app_enabled: true, }; beforeEach(async () => { const module = await testing_1.Test.createTestingModule({ controllers: [notifications_controller_1.NotificationsController], providers: [ { provide: notifications_service_1.NotificationsService, useValue: { findAllForUser: jest.fn(), getUnreadCount: jest.fn(), markAsRead: jest.fn(), markAllAsRead: jest.fn(), delete: jest.fn(), getPreferences: jest.fn(), updatePreferences: jest.fn(), create: jest.fn(), sendFromTemplate: jest.fn(), findAllTemplates: jest.fn(), findTemplateByCode: jest.fn(), }, }, { provide: rbac_service_1.RbacService, useValue: { userHasPermission: jest.fn().mockResolvedValue(true), userHasAnyPermission: jest.fn().mockResolvedValue(true), }, }, core_1.Reflector, ], }).compile(); controller = module.get(notifications_controller_1.NotificationsController); service = module.get(notifications_service_1.NotificationsService); }); afterEach(() => { jest.clearAllMocks(); }); describe('getMyNotifications', () => { it('should return paginated notifications', async () => { const result = { data: [mockNotification], total: 1, page: 1, limit: 20, }; service.findAllForUser.mockResolvedValue(result); const response = await controller.getMyNotifications(mockRequestUser, 1, 20, false); expect(response).toEqual(result); expect(service.findAllForUser).toHaveBeenCalledWith('user-123', 'tenant-123', { page: 1, limit: 20, unreadOnly: false }); }); it('should use default values when not provided', async () => { service.findAllForUser.mockResolvedValue({ data: [], total: 0, page: 1, limit: 20 }); await controller.getMyNotifications(mockRequestUser); expect(service.findAllForUser).toHaveBeenCalledWith('user-123', 'tenant-123', { page: 1, limit: 20, unreadOnly: false }); }); }); describe('getUnreadCount', () => { it('should return unread count', async () => { service.getUnreadCount.mockResolvedValue(5); const result = await controller.getUnreadCount(mockRequestUser); expect(result).toEqual({ count: 5 }); expect(service.getUnreadCount).toHaveBeenCalledWith('user-123', 'tenant-123'); }); }); describe('markAsRead', () => { it('should mark notification as read', async () => { service.markAsRead.mockResolvedValue({ ...mockNotification, is_read: true }); const result = await controller.markAsRead('notif-123', mockRequestUser); expect(result.is_read).toBe(true); expect(service.markAsRead).toHaveBeenCalledWith('notif-123', 'user-123', 'tenant-123'); }); }); describe('markAllAsRead', () => { it('should mark all notifications as read', async () => { service.markAllAsRead.mockResolvedValue(10); const result = await controller.markAllAsRead(mockRequestUser); expect(result.message).toContain('10'); expect(service.markAllAsRead).toHaveBeenCalledWith('user-123', 'tenant-123'); }); }); describe('delete', () => { it('should delete notification', async () => { service.delete.mockResolvedValue(undefined); const result = await controller.delete('notif-123', mockRequestUser); expect(result.message).toBe('Notificación eliminada'); expect(service.delete).toHaveBeenCalledWith('notif-123', 'user-123', 'tenant-123'); }); }); describe('getPreferences', () => { it('should return user preferences', async () => { service.getPreferences.mockResolvedValue(mockPreferences); const result = await controller.getPreferences(mockRequestUser); expect(result).toEqual(mockPreferences); expect(service.getPreferences).toHaveBeenCalledWith('user-123', 'tenant-123'); }); }); describe('updatePreferences', () => { it('should update preferences', async () => { const updateDto = { emailEnabled: false }; const updated = { ...mockPreferences, email_enabled: false }; service.updatePreferences.mockResolvedValue(updated); const result = await controller.updatePreferences(mockRequestUser, updateDto); expect(result.email_enabled).toBe(false); expect(service.updatePreferences).toHaveBeenCalledWith('user-123', 'tenant-123', updateDto); }); }); describe('sendNotification', () => { it('should send notification', async () => { const createDto = { user_id: 'user-456', type: 'info', title: 'New Notification', message: 'Hello', }; service.create.mockResolvedValue(mockNotification); const result = await controller.sendNotification(createDto, mockRequestUser); expect(result).toEqual(mockNotification); expect(service.create).toHaveBeenCalledWith(createDto, 'tenant-123'); }); }); describe('getTemplates', () => { it('should return all templates', async () => { const templates = [{ id: 'tpl-1', code: 'welcome' }]; service.findAllTemplates.mockResolvedValue(templates); const result = await controller.getTemplates(); expect(result).toEqual(templates); expect(service.findAllTemplates).toHaveBeenCalled(); }); }); describe('getTemplate', () => { it('should return template by code', async () => { const template = { id: 'tpl-1', code: 'welcome' }; service.findTemplateByCode.mockResolvedValue(template); const result = await controller.getTemplate('welcome'); expect(result).toEqual(template); expect(service.findTemplateByCode).toHaveBeenCalledWith('welcome'); }); }); }); //# sourceMappingURL=notifications.controller.spec.js.map