"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _react = _interopRequireDefault(require("react"));

var _react2 = require("@testing-library/react");

var _index = _interopRequireDefault(require("../index"));

var _configProvider = _interopRequireDefault(require("../../config-provider"));

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) { (0, _defineProperty2.default)(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; }

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.default.createElement("div", {
        onClick: onClick,
        "data-testid": "close-icon"
      }, "close icon");
    }
  });
});
describe('Message', function () {
  it('should renders with default props', function () {
    var _render = (0, _react2.render)( /*#__PURE__*/_react.default.createElement(_index.default, null, "foo")),
        container = _render.container;

    expect(container).toMatchSnapshot();
  });
  it('should render with passed theme', function () {
    (0, _react2.render)( /*#__PURE__*/_react.default.createElement(_index.default, {
      "data-testid": "root",
      theme: "danger"
    }, "foo"));
    expect(_react2.screen.getByTestId('root').className).toEqual('za-message za-message--danger');
  });
  it('should render with an icon', function () {
    (0, _react2.render)( /*#__PURE__*/_react.default.createElement(_index.default, {
      icon: /*#__PURE__*/_react.default.createElement("img", {
        alt: "message-icon",
        src: "\\\\static.zhongan.com/website/health/zarm/images/icons/state.png"
      })
    }, "foo"));
    expect(_react2.screen.getByAltText('message-icon')).toBeTruthy();
    expect(_react2.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();
    (0, _react2.render)( /*#__PURE__*/_react.default.createElement(_index.default, {
      "data-testid": "root",
      hasArrow: true,
      onClick: onClick
    }, "foo"));

    _react2.fireEvent.click(_react2.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();
    (0, _react2.render)( /*#__PURE__*/_react.default.createElement(_index.default, {
      "data-testid": "root",
      onClick: onClick
    }, "foo"));

    _react2.fireEvent.click(_react2.screen.getByTestId('root'));

    expect(onClick).not.toBeCalled();
  });
  it('should do nothing if onClick props is undefined', function () {
    (0, _react2.render)( /*#__PURE__*/_react.default.createElement(_index.default, {
      "data-testid": "root",
      hasArrow: true
    }, "foo"));

    _react2.fireEvent.click(_react2.screen.getByTestId('root'));
  });
  it('should render close icon and handle close event', function () {
    (0, _react2.render)( /*#__PURE__*/_react.default.createElement(_index.default, {
      closable: true,
      "data-testid": "root"
    }, "foo"));
    expect(_react2.screen.queryByTestId('root')).toBeTruthy();
    expect(_react2.screen.getByTestId('close-icon')).toBeTruthy();

    _react2.fireEvent.click(_react2.screen.getByTestId('close-icon'));

    expect(_react2.screen.queryByTestId('root')).toBeFalsy();
  });
  test('should not render footer if message has no arrow and is not closable', function () {
    (0, _react2.render)( /*#__PURE__*/_react.default.createElement(_index.default, {
      "data-testid": "root"
    }, "foo"));
    expect(_react2.screen.getByTestId('root').querySelector('.za-message__footer')).toBeFalsy();
  });
  test('should render footer if message has arrow', function () {
    (0, _react2.render)( /*#__PURE__*/_react.default.createElement(_index.default, {
      "data-testid": "root",
      hasArrow: true
    }, "foo"));
    expect(_react2.screen.getByTestId('root').className).toEqual('za-message za-message--primary za-message--link');
    expect(_react2.screen.getByTestId('root').querySelector('.za-message__footer')).toBeTruthy();
  });
  test('should render footer if message is closable', function () {
    (0, _react2.render)( /*#__PURE__*/_react.default.createElement(_index.default, {
      "data-testid": "root",
      closable: true
    }, "foo"));
    expect(_react2.screen.getByTestId('root').querySelector('.za-message__footer')).toBeTruthy();
  });
  test('should forward ref', function () {
    var ref = /*#__PURE__*/_react.default.createRef();

    (0, _react2.render)( /*#__PURE__*/_react.default.createElement(_index.default, {
      ref: ref
    }, "foo"));
    expect(ref.current.nodeName.toLowerCase()).toEqual('div');
  });
  test('should use prefixCls from context', function () {
    (0, _react2.render)( /*#__PURE__*/_react.default.createElement(_configProvider.default, {
      prefixCls: "zarm"
    }, /*#__PURE__*/_react.default.createElement(_index.default, {
      "data-testid": "root"
    }, "foo")));
    expect(_react2.screen.getByTestId('root').className).toEqual('zarm-message zarm-message--primary');
  });
});