"use strict";

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

var _reactModal = _interopRequireDefault(require("react-modal"));

var _chai = require("chai");

var _enzyme = require("enzyme");

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

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

var _appLauncher = _interopRequireDefault(require("../../app-launcher"));

var _tile = _interopRequireDefault(require("../../app-launcher/tile"));

var _expandableSection = _interopRequireDefault(require("../../app-launcher/expandable-section"));

var _search = _interopRequireDefault(require("../../input/search"));

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

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

/* eslint-disable react/no-find-dom-node */
var should = chai.should();
describe('SLDS APP LAUNCHER *******************************************', function () {
  var handles = {
    appLauncher: null,
    appLauncherIcon: null,
    modal: null
  };
  var defaultAppLauncherProps = {
    isOpen: true
  };

  var createAppLauncher = function createAppLauncher(props) {
    return _react.default.createElement(_appLauncher.default, (0, _lodash.default)({}, defaultAppLauncherProps, props), _react.default.createElement(_expandableSection.default, {
      title: "All Items"
    }, _react.default.createElement(_tile.default, {
      title: "Marketing Cloud"
    }), _react.default.createElement(_tile.default, {
      title: "Support Cloud"
    })));
  };

  function mountAppLauncher(props) {
    handles.appLauncher = (0, _enzyme.mount)(_react.default.createElement(_iconSettings.default, {
      iconPath: "/assets/icons"
    }, createAppLauncher(props)));
    handles.appLauncherIcon = handles.appLauncher.find('.slds-context-bar__icon-action'); // Wrap the modal portal in an Enzyme wrapper

    handles.modal = new _enzyme.ReactWrapper(handles.appLauncher.find(_reactModal.default).getElement().portal, true);
  }

  function cleanDom() {
    // Removes the modal container element from the bottom of the DOM, this will prevent the 'setState' errors
    // gotta be a better way to do this..
    var modalWrapper = document.documentElement.querySelectorAll('.ReactModalPortal')[0];

    if (modalWrapper) {
      modalWrapper.parentNode.removeChild(modalWrapper);
    }
  } // ///////////////////////
  // ////// T O D O ////////
  // ///////////////////////
  // APP LAUNCHER -----
  // tabs?
  // if you pass a isOpen prop, you must control the component with it (this.state.isOpen will now work)
  // (#591, waiting on #590) modal content has classes: slds-modal__content slds-app-launcher__content slds-p-around_medium


  describe('App Launcher', function () {
    var onClose;
    beforeEach(function () {
      onClose = sinon.spy();
      mountAppLauncher({
        modalClassName: 'custom-modal-class',
        modalHeaderButton: _react.default.createElement(_button.default, {
          label: "App Exchange"
        }),
        onClose: onClose,
        search: _react.default.createElement(_search.default, {
          assistiveText: {
            icon: 'Find an app'
          }
        }),
        title: 'App Launcher!',
        triggerName: 'App Name'
      });
    });
    afterEach(function () {
      cleanDom();
    });
    it('renders modal', function () {
      (0, _chai.expect)(handles.appLauncher.find('.slds-modal').length).to.eql(1);
    });
    it('renders custom modal class', function () {
      should.exist(handles.appLauncher.find('.custom-modal-class'));
    });
    it('renders modal header', function () {
      should.exist(handles.appLauncher.find('.slds-app-launcher__header'));
    });
    it('app launcher title can be set', function () {
      var appLauncherTitle = handles.appLauncher.find('h2.slds-text-heading_medium');
      (0, _chai.expect)(appLauncherTitle.length).to.eql(1);
      (0, _chai.expect)(appLauncherTitle.text()).to.eql('App Launcher!');
    });
    it('app launcher triggerName can be set', function () {
      var appLauncherTriggerName = handles.appLauncher.find('.slds-context-bar__app-name');
      (0, _chai.expect)(appLauncherTriggerName.length).to.eql(1);
      (0, _chai.expect)(appLauncherTriggerName.text()).to.eql('App Name');
    });
    it('renders search bar', function () {
      should.exist(handles.modal.find(_search.default));
      (0, _chai.expect)(handles.appLauncher.find('.slds-app-launcher__header-search').length).to.eql(1);
    });
    it('renders `modalHeaderButton`', function () {
      var headerButton = handles.appLauncher.find('header.slds-modal__header button.slds-button.slds-button_neutral');
      (0, _chai.expect)(headerButton.length).to.eql(1);
      (0, _chai.expect)(headerButton.text()).to.eql('App Exchange');
    });
    it('closing modal fires callback', function () {
      handles.appLauncher.find('button.slds-modal__close').simulate('click');
      (0, _chai.expect)(onClose.calledOnce).to.be.true; // eslint-disable-line no-unused-expressions
    });
    it('close modal callback receives original event as arg', function () {
      handles.appLauncher.find('button.slds-modal__close').simulate('click');
      (0, _chai.expect)(onClose.args.length).to.equal(1);
    });
    it('renders modal content', function () {
      (0, _chai.expect)(handles.appLauncher.find('.slds-modal__content.slds-app-launcher__content.slds-p-around_medium').length).to.equal(1);
    });
    it('app launcher can be passed children', function () {
      should.exist(handles.appLauncher.find('SLDSAppLauncherExpandableSection'));
      (0, _chai.expect)(handles.appLauncher.find('SLDSAppLauncherTile').length).to.equal(2);
    });
  });
  describe('App Launcher Icon', function () {
    var triggerOnClick;
    beforeEach(function () {
      triggerOnClick = sinon.spy();
      mountAppLauncher({
        assistiveText: {
          trigger: 'Custom Icon Assistive Text'
        },
        triggerOnClick: triggerOnClick
      });
    });
    afterEach(function () {
      cleanDom();
    });
    it('renders App Launcer icon', function () {
      should.exist(handles.appLauncherIcon);
    });
    it('renders all App Launcher dots', function () {
      (0, _chai.expect)(handles.appLauncherIcon.find('.slds-icon-waffle').containsAllMatchingElements([_react.default.createElement("span", {
        className: "slds-r1"
      }), _react.default.createElement("span", {
        className: "slds-r2"
      }), _react.default.createElement("span", {
        className: "slds-r3"
      }), _react.default.createElement("span", {
        className: "slds-r4"
      }), _react.default.createElement("span", {
        className: "slds-r5"
      }), _react.default.createElement("span", {
        className: "slds-r6"
      }), _react.default.createElement("span", {
        className: "slds-r7"
      }), _react.default.createElement("span", {
        className: "slds-r8"
      }), _react.default.createElement("span", {
        className: "slds-r9"
      })])).to.equal(true);
    });
    it('App Launcher Icon link has proper classes', function () {
      var button = handles.appLauncherIcon.find('button');
      (0, _chai.expect)(button).to.have.className('slds-icon-waffle_container');
      (0, _chai.expect)(button).to.have.className('slds-context-bar__button');
    });
    it('clicking App Launcher Icon fires callback', function () {
      handles.appLauncherIcon.find('button').simulate('click');
      (0, _chai.expect)(triggerOnClick.calledOnce).to.be.true; // eslint-disable-line no-unused-expressions
    });
    it('App Launcher Icon callback receives original event as arg', function () {
      handles.appLauncherIcon.find('button').simulate('click');
      (0, _chai.expect)(triggerOnClick.args.length).to.equal(1);
    });
    it('renders assistive text from prop', function () {
      (0, _chai.expect)(handles.appLauncherIcon.find('.slds-assistive-text').text()).to.equal('Custom Icon Assistive Text');
    });
  });
});