All files / src/services notificationService.ts

100% Statements 11/11
100% Branches 10/10
100% Functions 2/2
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61        4x                                       4x   2x 1x 1x     1x                           4x   2x 1x 1x     1x                
// src/services/notificationService.ts
import toast, { ToastOptions } from '../lib/toast';
import { logger } from './logger.client';
 
const commonToastOptions: ToastOptions = {
  style: {
    borderRadius: '8px',
    background: 'var(--toast-bg, #333)', // Uses CSS variables for theming
    color: 'var(--toast-color, #fff)',
    boxShadow: '0 4px 12px rgba(0, 0, 0, 0.15)',
  },
};
 
// Define a minimal interface for the toaster to allow mocking
export interface Toaster {
  success: (message: string, options?: ToastOptions) => string;
  error: (message: string, options?: ToastOptions) => string;
}
 
/**
 * Displays a success toast notification.
 * @param message The message to display.
 * @param toaster Optional toaster instance (for testing). Defaults to the imported toast library.
 */
export const notifySuccess = (message: string, toaster: Toaster = toast) => {
  // Defensive check: Ensure the toaster instance is valid
  if (!toaster || typeof toaster.success !== 'function') {
    logger.warn({ message }, '[NotificationService] toast.success is not available');
    return;
  }
 
  toaster.success(message, {
    ...commonToastOptions,
    iconTheme: {
      primary: '#10B981', // Emerald-500
      secondary: '#fff',
    },
  });
};
 
/**
 * Displays an error toast notification.
 * @param message The message to display.
 * @param toaster Optional toaster instance (for testing). Defaults to the imported toast library.
 */
export const notifyError = (message: string, toaster: Toaster = toast) => {
  // Defensive check
  if (!toaster || typeof toaster.error !== 'function') {
    logger.warn({ message }, '[NotificationService] toast.error is not available');
    return;
  }
 
  toaster.error(message, {
    ...commonToastOptions,
    iconTheme: {
      primary: '#EF4444', // Red-500
      secondary: '#fff',
    },
  });
};