import { getNotificationPreferences, updateNotificationPreferences, updatePreferences } from './notificationService';
import http from '@/http';

describe('notificationService', () => {
  beforeEach(() => {
    jest.clearAllMocks();
  });

  describe('getNotificationPreferences', () => {
    it('shouldFetchNotificationPreferences', async () => {
      const mockPrefs = { alertesActivees: true };
      (http.get as jest.Mock).mockResolvedValueOnce({ data: mockPrefs });

      const result = await getNotificationPreferences();

      expect(http.get).toHaveBeenCalledWith('/notification-preferences');
      expect(result).toEqual(mockPrefs);
    });
  });

  describe('updateNotificationPreferences', () => {
    it('shouldUpdateNotificationPreferences', async () => {
      const mockPrefs = { alertesActivees: false };
      (http.put as jest.Mock).mockResolvedValueOnce({ data: mockPrefs });

      const result = await updateNotificationPreferences(false);

      expect(http.put).toHaveBeenCalledWith('/notification-preferences', {
        alertesActivees: false,
      });
      expect(result).toEqual(mockPrefs);
    });
  });

  describe('updatePreferences', () => {
    it('shouldSendOnlyTheChangedPreference', async () => {
      const mockPrefs = { alertesActivees: true, newsletter: false };
      (http.put as jest.Mock).mockResolvedValueOnce({ data: mockPrefs });

      const result = await updatePreferences({ newsletter: false });

      expect(http.put).toHaveBeenCalledWith('/notification-preferences', { newsletter: false });
      expect(result).toEqual(mockPrefs);
    });
  });
});
