import _defineProperty from "@babel/runtime/helpers/defineProperty";

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

import React from 'react';
import { fireEvent, render, screen } from '@testing-library/react';
import Message from '../index';
import ConfigProvider from '../../config-provider';
jest.mock('@zarm-design/icons', function () {
  return _objectSpread(_objectSpread({}, jest.requireActual('@zarm-design/icons')), {}, {
    Close: function Close(_ref) {
      var onClick = _ref.onClick;
      return /*#__PURE__*/React.createElement("div", {
        onClick: onClick,
        "data-testid": "close-icon"
      }, "close icon");
    }
  });
});
describe('Message', function () {
  it('should renders with default props', function () {
    var _render = render( /*#__PURE__*/React.createElement(Message, null, "foo")),
        container = _render.container;

    expect(container).toMatchSnapshot();
  });
  it('should render with passed theme', function () {
    render( /*#__PURE__*/React.createElement(Message, {
      "data-testid": "root",
      theme: "danger"
    }, "foo"));
    expect(screen.getByTestId('root').className).toEqual('za-message za-message--danger');
  });
  it('should render with an icon', function () {
    render( /*#__PURE__*/React.createElement(Message, {
      icon: /*#__PURE__*/React.createElement("img", {
        alt: "message-icon",
        src: "\\\\static.zhongan.com/website/health/zarm/images/icons/state.png"
      })
    }, "foo"));
    expect(screen.getByAltText('message-icon')).toBeTruthy();
    expect(screen.getByAltText('message-icon').parentElement.className).toEqual('za-message__icon');
  });
  it('should handle click event and call the onClick function passed from props if message has arrow', function () {
    var onClick = jest.fn();
    render( /*#__PURE__*/React.createElement(Message, {
      "data-testid": "root",
      hasArrow: true,
      onClick: onClick
    }, "foo"));
    fireEvent.click(screen.getByTestId('root'));
    expect(onClick).toBeCalled();
  });
  it('should handle click event and NOT call the onClick function passed from props if message has no arrow', function () {
    var onClick = jest.fn();
    render( /*#__PURE__*/React.createElement(Message, {
      "data-testid": "root",
      onClick: onClick
    }, "foo"));
    fireEvent.click(screen.getByTestId('root'));
    expect(onClick).not.toBeCalled();
  });
  it('should do nothing if onClick props is undefined', function () {
    render( /*#__PURE__*/React.createElement(Message, {
      "data-testid": "root",
      hasArrow: true
    }, "foo"));
    fireEvent.click(screen.getByTestId('root'));
  });
  it('should render close icon and handle close event', function () {
    render( /*#__PURE__*/React.createElement(Message, {
      closable: true,
      "data-testid": "root"
    }, "foo"));
    expect(screen.queryByTestId('root')).toBeTruthy();
    expect(screen.getByTestId('close-icon')).toBeTruthy();
    fireEvent.click(screen.getByTestId('close-icon'));
    expect(screen.queryByTestId('root')).toBeFalsy();
  });
  test('should not render footer if message has no arrow and is not closable', function () {
    render( /*#__PURE__*/React.createElement(Message, {
      "data-testid": "root"
    }, "foo"));
    expect(screen.getByTestId('root').querySelector('.za-message__footer')).toBeFalsy();
  });
  test('should render footer if message has arrow', function () {
    render( /*#__PURE__*/React.createElement(Message, {
      "data-testid": "root",
      hasArrow: true
    }, "foo"));
    expect(screen.getByTestId('root').className).toEqual('za-message za-message--primary za-message--link');
    expect(screen.getByTestId('root').querySelector('.za-message__footer')).toBeTruthy();
  });
  test('should render footer if message is closable', function () {
    render( /*#__PURE__*/React.createElement(Message, {
      "data-testid": "root",
      closable: true
    }, "foo"));
    expect(screen.getByTestId('root').querySelector('.za-message__footer')).toBeTruthy();
  });
  test('should forward ref', function () {
    var ref = /*#__PURE__*/React.createRef();
    render( /*#__PURE__*/React.createElement(Message, {
      ref: ref
    }, "foo"));
    expect(ref.current.nodeName.toLowerCase()).toEqual('div');
  });
  test('should use prefixCls from context', function () {
    render( /*#__PURE__*/React.createElement(ConfigProvider, {
      prefixCls: "zarm"
    }, /*#__PURE__*/React.createElement(Message, {
      "data-testid": "root"
    }, "foo")));
    expect(screen.getByTestId('root').className).toEqual('zarm-message zarm-message--primary');
  });
});