UNPKG

@ideal-photography/shared

Version:

Shared MongoDB and utility logic for Ideal Photography PWAs: users, products, services, bookings, orders/cart, galleries, reviews, notifications, campaigns, settings, audit logs, minimart items/orders, and push notification subscriptions.

207 lines (175 loc) 7.75 kB
import { jest } from '@jest/globals'; // Simple unit tests for channel dispatching logic describe('Email/Push Channel Dispatching', () => { beforeEach(() => { jest.clearAllMocks(); }); test('validates alertMeta structure for email channel', () => { const alertMeta = { title: 'Test Alert', message: 'Test message', type: 'system', recipients: { users: ['507f1f77bcf86cd799439011'] }, channels: { inApp: true, push: false, email: true }, emailTemplate: 'welcome', emailData: { name: 'Test User' } }; // Test that required fields are present for email expect(alertMeta.channels.email).toBe(true); expect(alertMeta.emailTemplate).toBeDefined(); expect(alertMeta.emailData).toBeDefined(); expect(alertMeta.recipients.users).toHaveLength(1); }); test('validates alertMeta structure for push channel', () => { const alertMeta = { title: 'Test Alert', message: 'Test message', type: 'system', recipients: { users: ['507f1f77bcf86cd799439011'] }, channels: { inApp: true, push: true, email: false } }; // Test that required fields are present for push expect(alertMeta.channels.push).toBe(true); expect(alertMeta.recipients.users).toHaveLength(1); expect(alertMeta.title).toBeDefined(); expect(alertMeta.message).toBeDefined(); }); test('validates alertMeta structure for in-app channel', () => { const alertMeta = { title: 'Test Alert', message: 'Test message', type: 'system', recipients: { users: ['507f1f77bcf86cd799439011'] }, channels: { inApp: true, push: false, email: false } }; // Test that required fields are present for in-app expect(alertMeta.channels.inApp).toBe(true); expect(alertMeta.recipients.users).toHaveLength(1); expect(alertMeta.title).toBeDefined(); expect(alertMeta.message).toBeDefined(); expect(alertMeta.type).toBeDefined(); }); test('handles multi-channel configuration', () => { const alertMeta = { title: 'Multi-Channel Alert', message: 'Multi-channel message', type: 'booking', recipients: { users: ['507f1f77bcf86cd799439011', '507f1f77bcf86cd799439012'] }, channels: { inApp: true, push: true, email: true }, emailTemplate: 'booking-confirmation', emailData: { bookingId: 'BK123' } }; // Test all channels enabled expect(alertMeta.channels.inApp).toBe(true); expect(alertMeta.channels.push).toBe(true); expect(alertMeta.channels.email).toBe(true); // Test multiple recipients expect(alertMeta.recipients.users).toHaveLength(2); // Test email template data expect(alertMeta.emailTemplate).toBe('booking-confirmation'); expect(alertMeta.emailData.bookingId).toBe('BK123'); }); test('handles channel overrides correctly', () => { const baseAlertMeta = { title: 'Base Alert', message: 'Base message', type: 'system', recipients: { users: ['507f1f77bcf86cd799439011'] }, channels: { inApp: true, push: true, email: true } }; // Test overriding channels const overriddenChannels = { ...baseAlertMeta.channels, email: false, push: false }; expect(overriddenChannels.inApp).toBe(true); expect(overriddenChannels.push).toBe(false); expect(overriddenChannels.email).toBe(false); }); test('validates priority levels', () => { const priorities = ['low', 'normal', 'high', 'urgent']; priorities.forEach(priority => { const alertMeta = { title: 'Priority Test', message: 'Priority message', type: 'system', recipients: { users: ['507f1f77bcf86cd799439011'] }, channels: { inApp: true, push: false, email: false }, priority }; expect(alertMeta.priority).toBe(priority); }); }); test('validates notification types', () => { const types = ['system', 'booking', 'payment', 'verification', 'reminder']; types.forEach(type => { const alertMeta = { title: 'Type Test', message: 'Type message', type, recipients: { users: ['507f1f77bcf86cd799439011'] }, channels: { inApp: true, push: false, email: false } }; expect(alertMeta.type).toBe(type); }); }); test('handles email template fallbacks', () => { const alertMetaWithTemplate = { title: 'Template Test', message: 'Template message', type: 'booking', recipients: { users: ['507f1f77bcf86cd799439011'] }, channels: { inApp: true, push: false, email: true }, emailTemplate: 'booking-confirmation', emailData: { bookingId: 'BK123' } }; // Test with explicit template expect(alertMetaWithTemplate.emailTemplate).toBe('booking-confirmation'); // Test fallback to type when template not specified const alertMetaWithoutTemplate = { title: 'No Template Test', message: 'No template message', type: 'booking', recipients: { users: ['507f1f77bcf86cd799439011'] }, channels: { inApp: true, push: false, email: true } }; const fallbackTemplate = alertMetaWithoutTemplate.emailTemplate || alertMetaWithoutTemplate.type; expect(fallbackTemplate).toBe('booking'); }); test('validates recipient structure', () => { const alertMeta = { title: 'Recipient Test', message: 'Recipient message', type: 'system', recipients: { users: ['507f1f77bcf86cd799439011'] }, channels: { inApp: true, push: false, email: false } }; // Test user recipients expect(alertMeta.recipients.users).toBeDefined(); expect(Array.isArray(alertMeta.recipients.users)).toBe(true); expect(alertMeta.recipients.users[0]).toMatch(/^[0-9a-fA-F]{24}$/); // Valid ObjectId format // Test role recipients (future feature) const roleAlertMeta = { ...alertMeta, recipients: { roles: ['admin', 'moderator'] } }; expect(roleAlertMeta.recipients.roles).toBeDefined(); expect(Array.isArray(roleAlertMeta.recipients.roles)).toBe(true); }); test('validates trigger and metadata', () => { const alertMeta = { title: 'Trigger Test', message: 'Trigger message', type: 'system', recipients: { users: ['507f1f77bcf86cd799439011'] }, channels: { inApp: true, push: false, email: false } }; const opts = { trigger: 'user.registered', priority: 'normal' }; // Test trigger format expect(opts.trigger).toMatch(/^[a-z]+\.[a-z]+(\.[a-z0-9]+)*$/); // Test metadata structure const metadata = { ...alertMeta, ...opts }; expect(metadata.trigger).toBe('user.registered'); expect(metadata.priority).toBe('normal'); }); });