// Mock dependencies jest.mock('../../../../../services/email/core/EmailClient', () => { return jest.fn().mockImplementation(() => ({ initialize: jest.fn().mockResolvedValue(), sendEmail: jest.fn().mockResolvedValue({ success: true, messageId: 'msg-123' }), })); }); jest.mock('../../../../../services/email/core/TemplateManager', () => { return jest.fn().mockImplementation(() => ({ initialize: jest.fn().mockResolvedValue(), renderTemplate: jest.fn().mockResolvedValue('Test'), })); }); jest.mock('../../../../../utils/logger', () => ({ info: jest.fn(), error: jest.fn(), warn: jest.fn(), })); const RentalReminderEmailService = require('../../../../../services/email/domain/RentalReminderEmailService'); describe('RentalReminderEmailService', () => { let service; const originalEnv = process.env; beforeEach(() => { jest.clearAllMocks(); process.env = { ...originalEnv, FRONTEND_URL: 'http://localhost:3000' }; service = new RentalReminderEmailService(); }); afterEach(() => { process.env = originalEnv; }); describe('initialize', () => { it('should initialize only once', async () => { await service.initialize(); await service.initialize(); expect(service.emailClient.initialize).toHaveBeenCalledTimes(1); expect(service.templateManager.initialize).toHaveBeenCalledTimes(1); }); }); describe('sendConditionCheckReminder', () => { const userEmail = 'john@example.com'; const notification = { title: 'Condition Check Required', message: 'Please complete the condition check for your rental.', metadata: { deadline: '2024-01-16T10:00:00Z', }, }; const rental = { item: { name: 'Power Drill' }, }; it('should send condition check reminder with correct variables', async () => { const result = await service.sendConditionCheckReminder(userEmail, notification, rental); expect(result.success).toBe(true); expect(service.templateManager.renderTemplate).toHaveBeenCalledWith( 'conditionCheckReminderToUser', expect.objectContaining({ title: 'Condition Check Required', message: 'Please complete the condition check for your rental.', itemName: 'Power Drill', }) ); expect(service.emailClient.sendEmail).toHaveBeenCalledWith( 'john@example.com', 'Village Share: Condition Check Required', expect.any(String) ); }); it('should use default item name when rental item is missing', async () => { const rentalNoItem = {}; await service.sendConditionCheckReminder(userEmail, notification, rentalNoItem); expect(service.templateManager.renderTemplate).toHaveBeenCalledWith( 'conditionCheckReminderToUser', expect.objectContaining({ itemName: 'Unknown Item' }) ); }); it('should use default deadline when metadata is missing', async () => { const notificationNoDeadline = { ...notification, metadata: {}, }; await service.sendConditionCheckReminder(userEmail, notificationNoDeadline, rental); expect(service.templateManager.renderTemplate).toHaveBeenCalledWith( 'conditionCheckReminderToUser', expect.objectContaining({ deadline: 'Not specified' }) ); }); it('should handle errors gracefully', async () => { service.templateManager.renderTemplate.mockRejectedValueOnce(new Error('Template error')); const result = await service.sendConditionCheckReminder(userEmail, notification, rental); expect(result.success).toBe(false); expect(result.error).toBe('Template error'); }); }); });