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.

375 lines (336 loc) 13.7 kB
import { jest } from '@jest/globals'; // Unit tests for notification filtering logic (UI component logic) describe('Notification Filtering Logic', () => { beforeEach(() => { jest.clearAllMocks(); }); test('filters notifications by System preset', () => { const notifications = [ { _id: 'notif1', title: 'System Alert', message: 'System notification', trigger: 'user.registered', createdAt: new Date('2024-01-15') }, { _id: 'notif2', title: 'Manual Alert', message: 'Manual notification', trigger: null, createdAt: new Date('2024-01-14') }, { _id: 'notif3', title: 'Booking Alert', message: 'Booking notification', trigger: 'booking.created', createdAt: new Date('2024-01-13') } ]; // Test System preset filter (trigger not null) const systemNotifications = notifications.filter(notif => notif.trigger !== null); expect(systemNotifications).toHaveLength(2); expect(systemNotifications[0].trigger).toBe('user.registered'); expect(systemNotifications[1].trigger).toBe('booking.created'); }); test('filters notifications by Manual preset', () => { const notifications = [ { _id: 'notif1', title: 'System Alert', message: 'System notification', trigger: 'user.registered', createdAt: new Date('2024-01-15') }, { _id: 'notif2', title: 'Manual Alert', message: 'Manual notification', trigger: null, createdAt: new Date('2024-01-14') }, { _id: 'notif3', title: 'Another Manual Alert', message: 'Another manual notification', trigger: null, createdAt: new Date('2024-01-13') } ]; // Test Manual preset filter (trigger is null) const manualNotifications = notifications.filter(notif => notif.trigger === null); expect(manualNotifications).toHaveLength(2); expect(manualNotifications[0].trigger).toBeNull(); expect(manualNotifications[1].trigger).toBeNull(); }); test('filters notifications by date range', () => { const notifications = [ { _id: 'notif1', title: 'Today Alert', message: 'Today notification', trigger: 'user.registered', createdAt: new Date('2024-01-15') }, { _id: 'notif2', title: 'Yesterday Alert', message: 'Yesterday notification', trigger: 'booking.created', createdAt: new Date('2024-01-14') }, { _id: 'notif3', title: 'Week Ago Alert', message: 'Week ago notification', trigger: 'payment.failed', createdAt: new Date('2024-01-08') } ]; const startDate = new Date('2024-01-14'); const endDate = new Date('2024-01-15'); // Test date range filter const filteredNotifications = notifications.filter(notif => { return notif.createdAt >= startDate && notif.createdAt <= endDate; }); expect(filteredNotifications).toHaveLength(2); expect(filteredNotifications[0].title).toBe('Today Alert'); expect(filteredNotifications[1].title).toBe('Yesterday Alert'); }); test('filters notifications by type', () => { const notifications = [ { _id: 'notif1', title: 'System Alert', message: 'System notification', type: 'system', trigger: 'user.registered' }, { _id: 'notif2', title: 'Booking Alert', message: 'Booking notification', type: 'booking', trigger: 'booking.created' }, { _id: 'notif3', title: 'Payment Alert', message: 'Payment notification', type: 'payment', trigger: 'payment.failed' } ]; // Test type filter const bookingNotifications = notifications.filter(notif => notif.type === 'booking'); const systemNotifications = notifications.filter(notif => notif.type === 'system'); expect(bookingNotifications).toHaveLength(1); expect(bookingNotifications[0].type).toBe('booking'); expect(systemNotifications).toHaveLength(1); expect(systemNotifications[0].type).toBe('system'); }); test('filters notifications by priority', () => { const notifications = [ { _id: 'notif1', title: 'High Priority Alert', message: 'High priority notification', priority: 'high', trigger: 'payment.failed' }, { _id: 'notif2', title: 'Normal Priority Alert', message: 'Normal priority notification', priority: 'normal', trigger: 'user.registered' }, { _id: 'notif3', title: 'Urgent Priority Alert', message: 'Urgent priority notification', priority: 'urgent', trigger: 'system.error' } ]; // Test priority filter const highPriorityNotifications = notifications.filter(notif => notif.priority === 'high'); const urgentNotifications = notifications.filter(notif => notif.priority === 'urgent'); expect(highPriorityNotifications).toHaveLength(1); expect(highPriorityNotifications[0].priority).toBe('high'); expect(urgentNotifications).toHaveLength(1); expect(urgentNotifications[0].priority).toBe('urgent'); }); test('filters notifications by status', () => { const notifications = [ { _id: 'notif1', title: 'Sent Alert', message: 'Sent notification', status: 'sent', trigger: 'user.registered' }, { _id: 'notif2', title: 'Pending Alert', message: 'Pending notification', status: 'pending', trigger: 'booking.created' }, { _id: 'notif3', title: 'Failed Alert', message: 'Failed notification', status: 'failed', trigger: 'payment.failed' } ]; // Test status filter const sentNotifications = notifications.filter(notif => notif.status === 'sent'); const pendingNotifications = notifications.filter(notif => notif.status === 'pending'); expect(sentNotifications).toHaveLength(1); expect(sentNotifications[0].status).toBe('sent'); expect(pendingNotifications).toHaveLength(1); expect(pendingNotifications[0].status).toBe('pending'); }); test('combines multiple filters', () => { const notifications = [ { _id: 'notif1', title: 'System High Alert', message: 'System high notification', type: 'system', priority: 'high', status: 'sent', trigger: 'user.registered', createdAt: new Date('2024-01-15') }, { _id: 'notif2', title: 'Booking Normal Alert', message: 'Booking normal notification', type: 'booking', priority: 'normal', status: 'sent', trigger: 'booking.created', createdAt: new Date('2024-01-15') }, { _id: 'notif3', title: 'System Low Alert', message: 'System low notification', type: 'system', priority: 'low', status: 'pending', trigger: 'system.warning', createdAt: new Date('2024-01-14') } ]; // Test combined filters: system type + high priority + sent status const filteredNotifications = notifications.filter(notif => { return notif.type === 'system' && notif.priority === 'high' && notif.status === 'sent' && notif.trigger !== null; }); expect(filteredNotifications).toHaveLength(1); expect(filteredNotifications[0].title).toBe('System High Alert'); }); test('validates filter preset configurations', () => { const filterPresets = { all: { label: 'All', filter: () => true }, system: { label: 'System', filter: (notif) => notif.trigger !== null }, manual: { label: 'Manual', filter: (notif) => notif.trigger === null }, today: { label: 'Today', filter: (notif) => { const today = new Date(); const notifDate = new Date(notif.createdAt); return notifDate.toDateString() === today.toDateString(); } }, unread: { label: 'Unread', filter: (notif) => !notif.read }, high: { label: 'High Priority', filter: (notif) => notif.priority === 'high' } }; // Test that all presets have required properties Object.values(filterPresets).forEach(preset => { expect(preset.label).toBeDefined(); expect(typeof preset.filter).toBe('function'); }); // Test System preset specifically const systemPreset = filterPresets.system; const systemNotification = { trigger: 'user.registered' }; const manualNotification = { trigger: null }; expect(systemPreset.filter(systemNotification)).toBe(true); expect(systemPreset.filter(manualNotification)).toBe(false); }); test('validates notification sorting logic', () => { const notifications = [ { _id: 'notif1', title: 'Old Alert', createdAt: new Date('2024-01-10'), priority: 'normal' }, { _id: 'notif2', title: 'Recent Alert', createdAt: new Date('2024-01-15'), priority: 'high' }, { _id: 'notif3', title: 'Middle Alert', createdAt: new Date('2024-01-12'), priority: 'urgent' } ]; // Test sorting by date (newest first) const sortedByDate = [...notifications].sort((a, b) => b.createdAt - a.createdAt); expect(sortedByDate[0].title).toBe('Recent Alert'); expect(sortedByDate[1].title).toBe('Middle Alert'); expect(sortedByDate[2].title).toBe('Old Alert'); // Test sorting by priority const priorityOrder = { urgent: 3, high: 2, normal: 1, low: 0 }; const sortedByPriority = [...notifications].sort((a, b) => priorityOrder[b.priority] - priorityOrder[a.priority] ); expect(sortedByPriority[0].title).toBe('Middle Alert'); // urgent expect(sortedByPriority[1].title).toBe('Recent Alert'); // high expect(sortedByPriority[2].title).toBe('Old Alert'); // normal }); test('validates notification search functionality', () => { const notifications = [ { _id: 'notif1', title: 'Welcome Email Sent', message: 'User registration completed successfully', trigger: 'user.registered' }, { _id: 'notif2', title: 'Booking Confirmed', message: 'Your studio session is confirmed for tomorrow', trigger: 'booking.created' }, { _id: 'notif3', title: 'Payment Failed', message: 'Unable to process payment for booking', trigger: 'payment.failed' } ]; // Test search by title only const searchResults = notifications.filter(notif => notif.title.toLowerCase().includes('booking') ); expect(searchResults).toHaveLength(1); expect(searchResults[0].title).toBe('Booking Confirmed'); // Test search by message content const paymentResults = notifications.filter(notif => notif.message.toLowerCase().includes('payment') ); expect(paymentResults).toHaveLength(1); expect(paymentResults[0].title).toBe('Payment Failed'); }); });