import CapNotification from '@capillarytech/cap-ui-library/CapNotification';
import { showError } from '../commonMethods';

jest.mock('@capillarytech/cap-ui-library/CapNotification', () => ({
  error: jest.fn(),
}));

jest.mock('../Create/messages', () => ({
  __esModule: true,
  default: {
    validationError: { defaultMessage: 'Validation error' },
  },
}));

describe('Sms commonMethods', () => {
  describe('showError', () => {
    beforeEach(() => {
      jest.clearAllMocks();
    });

    it('should call CapNotification.error when formData is not empty, isFormValid is false, and errorData has at least one true value', () => {
      const context = {
        props: {
          intl: { formatMessage: jest.fn((msg) => msg.defaultMessage || 'Validation error') },
        },
        state: {
          formData: { message: 'test' },
          isFormValid: false,
          errorData: [{ message: true }],
        },
      };
      showError.call(context);
      expect(CapNotification.error).toHaveBeenCalledWith({
        key: 'validation-error',
        message: 'Validation error',
      });
    });

    it('should not call CapNotification.error when formData is empty', () => {
      const context = {
        props: { intl: { formatMessage: jest.fn() } },
        state: {
          formData: {},
          isFormValid: false,
          errorData: [{ message: true }],
        },
      };
      showError.call(context);
      expect(CapNotification.error).not.toHaveBeenCalled();
    });

    it('should not call CapNotification.error when isFormValid is true', () => {
      const context = {
        props: { intl: { formatMessage: jest.fn() } },
        state: {
          formData: { message: 'test' },
          isFormValid: true,
          errorData: [{ message: true }],
        },
      };
      showError.call(context);
      expect(CapNotification.error).not.toHaveBeenCalled();
    });

    it('should not call CapNotification.error when no errorData entry has a true value', () => {
      const context = {
        props: { intl: { formatMessage: jest.fn() } },
        state: {
          formData: { message: 'test' },
          isFormValid: false,
          errorData: [{ message: false, title: false }],
        },
      };
      showError.call(context);
      expect(CapNotification.error).not.toHaveBeenCalled();
    });

    it('should not call CapNotification.error when errorData is empty', () => {
      const context = {
        props: { intl: { formatMessage: jest.fn() } },
        state: {
          formData: { message: 'test' },
          isFormValid: false,
          errorData: [],
        },
      };
      showError.call(context);
      expect(CapNotification.error).not.toHaveBeenCalled();
    });

    it('should not call CapNotification.error when errorData[0] is undefined', () => {
      const context = {
        props: { intl: { formatMessage: jest.fn() } },
        state: {
          formData: { message: 'test' },
          isFormValid: false,
          errorData: [],
        },
      };
      showError.call(context);
      expect(CapNotification.error).not.toHaveBeenCalled();
    });

    it('should call CapNotification.error when errorData[0] has multiple keys and one is true', () => {
      const context = {
        props: {
          intl: { formatMessage: jest.fn((msg) => msg.defaultMessage || 'Validation error') },
        },
        state: {
          formData: { message: 'hello' },
          isFormValid: false,
          errorData: [{ message: false, title: true }],
        },
      };
      showError.call(context);
      expect(CapNotification.error).toHaveBeenCalledWith({
        key: 'validation-error',
        message: 'Validation error',
      });
    });
  });
});
