"use strict";

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

var _reactDom = _interopRequireDefault(require("react-dom"));

var _testUtils = _interopRequireDefault(require("react-dom/test-utils"));

var _chai = require("chai");

var _lodash = _interopRequireDefault(require("lodash.assign"));

var _button = _interopRequireDefault(require("../../button"));

var _iconSettings = _interopRequireDefault(require("../../icon-settings"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/* eslint-disable react/no-render-return-value */
var Simulate = _testUtils.default.Simulate,
    findRenderedDOMComponentWithTag = _testUtils.default.findRenderedDOMComponentWithTag,
    findRenderedDOMComponentWithClass = _testUtils.default.findRenderedDOMComponentWithClass;
var mockCallback = sinon.spy();
describe('SLDSButton: ', function () {
  var body;
  var defaultProps = {
    label: 'Neutral',
    onClick: mockCallback,
    variant: 'neutral'
  };

  var renderButton = function renderButton(inst) {
    body = document.createElement('div');
    document.body.appendChild(body);
    return _reactDom.default.render(_react.default.createElement(_iconSettings.default, {
      iconPath: "/assets/icons"
    }, inst), body);
  };

  function removeButton() {
    _reactDom.default.unmountComponentAtNode(body);

    document.body.removeChild(body);
  }

  var createButton = function createButton(props) {
    return _react.default.createElement(_button.default, (0, _lodash.default)({}, defaultProps, props));
  };

  var getButton = function getButton(props) {
    return renderButton(createButton(props));
  };

  describe('Basic Button Props Render', function () {
    var cmp;
    var btn;
    beforeEach(function () {
      cmp = getButton({
        id: 'custom-id',
        text: 'Brand',
        theme: 'brand',
        style: {
          background: 'rgb(18, 49, 35)'
        }
      });
      btn = findRenderedDOMComponentWithClass(cmp, 'slds-button');
    });
    afterEach(function () {
      removeButton();
    });
    it('renders correct label', function () {
      (0, _chai.expect)(btn.textContent).to.equal('Neutral');
    });
    it('renders correct variant styles', function () {
      (0, _chai.expect)(btn.className).to.include('slds-button_neutral');
    });
    it('renders custom id', function () {
      (0, _chai.expect)(btn.getAttribute('id')).to.equal('custom-id');
    });
    it('renders custom styles', function () {
      btn.style.backgroundColor.should.equal('rgb(18, 49, 35)');
    });
  });
  describe('Icon with Label Button Props Render', function () {
    var cmp;
    var btn;
    var svg;
    beforeEach(function () {
      cmp = getButton({
        label: 'Neutral with Icon',
        iconName: 'download',
        iconCategory: 'action',
        iconPosition: 'right',
        variant: 'neutral'
      });
      btn = findRenderedDOMComponentWithClass(cmp, 'slds-button');
      svg = findRenderedDOMComponentWithClass(cmp, 'slds-button__icon');
    });
    afterEach(function () {
      removeButton();
    });
    it('renders label', function () {
      (0, _chai.expect)(btn.textContent).to.equal('Neutral with Icon');
    });
    it('renders icon', function () {
      (0, _chai.expect)(svg.className.baseVal).to.include('slds-button__icon_right');
    });
  });
  describe('Icon Button Props render', function () {
    var cmp;
    var asstText;
    var svg;
    beforeEach(function () {
      cmp = getButton({
        assistiveText: {
          icon: 'my settings'
        },
        variant: 'icon',
        iconCategory: 'utility',
        iconName: 'settings',
        iconSize: 'small',
        iconVariant: 'bare'
      });
      asstText = findRenderedDOMComponentWithClass(cmp, 'slds-assistive-text');
      svg = findRenderedDOMComponentWithTag(cmp, 'svg');
    });
    afterEach(function () {
      removeButton();
    });
    it('renders label', function () {
      (0, _chai.expect)(asstText.textContent).to.equal('my settings');
    });
    it('renders icon styles', function () {
      (0, _chai.expect)(svg.className.baseVal).to.include('slds-button__icon');
    });
  });
  describe('(icon path) Icon Button renders assistive text', function () {
    var cmp;
    var asstText;
    beforeEach(function () {
      cmp = getButton({
        assistiveText: {
          icon: 'News'
        },
        iconSize: 'large',
        iconPath: '/assets/icons/utility-sprite/svg/symbols.svg#announcement',
        title: 'announcement'
      });
      asstText = findRenderedDOMComponentWithClass(cmp, 'slds-assistive-text');
    });
    afterEach(function () {
      removeButton();
    });
    it('renders label', function () {
      (0, _chai.expect)(asstText.textContent).to.equal('News');
    });
  });
  describe('External Path Icon Button renders', function () {
    var cmp;
    var use;
    var svgHref;
    before(function () {
      cmp = getButton({
        assistiveText: {
          icon: 'announcement'
        },
        variant: 'icon',
        iconPath: '/assets/icons/utility-sprite/svg/symbols.svg#announcement',
        iconSize: 'large',
        iconVariant: 'bare'
      });
      use = findRenderedDOMComponentWithTag(cmp, 'use');
      svgHref = use.getAttribute('xlink:href');
    });
    after(function () {
      removeButton();
    });
    it('renders svg', function () {
      (0, _chai.expect)(svgHref).to.equal('/assets/icons/utility-sprite/svg/symbols.svg#announcement');
    });
  });
  describe('Button Clickable', function () {
    var cmp;
    var btn;
    var clicked;

    function setClick() {
      clicked = true;
    }

    beforeEach(function () {
      clicked = false;
      cmp = getButton({
        label: 'Neutral',
        variant: 'neutral',
        onClick: setClick
      });
      btn = findRenderedDOMComponentWithClass(cmp, 'slds-button');
    });
    afterEach(function () {
      removeButton();
    });
    it('can be clicked', function () {
      /* eslint-disable no-unused-expressions */
      (0, _chai.expect)(clicked).to.be.false;
      Simulate.click(btn, {});
      (0, _chai.expect)(clicked).to.be.true;
      /* eslint-enable no-unused-expressions */
    });
  });
});