/**
 * Copyright IBM Corp. 2016, 2018
 *
 * This source code is licensed under the Apache-2.0 license found in the
 * LICENSE file in the root directory of this source tree.
 */
import React from 'react';
import Tab from '../Tab';
import { shallow, mount } from 'enzyme';
import { settings } from 'carbon-components';
var prefix = settings.prefix;
describe('Tab', function () {
  describe('renders as expected', function () {
    var wrapper = shallow(React.createElement(Tab, {
      label: "firstTab"
    }));
    it('adds extra classes that are passed via className', function () {
      wrapper.setProps({
        className: 'extra-class'
      });
      expect(wrapper.hasClass('extra-class')).toBe(true);
    });
    it('renders <a> with expected className', function () {
      expect(wrapper.find('a').hasClass("".concat(prefix, "--tabs__nav-link"))).toBe(true);
    });
    it('renders <li> with [role="presentation"]', function () {
      expect(wrapper.props().role).toEqual('presentation');
    });
    it('renders <a> with [role="tab"]', function () {
      expect(wrapper.find('a').props().role).toEqual('tab');
    });
    it('renders <a> with tabindex set to 0', function () {
      expect(wrapper.find('a').props().tabIndex).toEqual(0);
    });
    it('sets tabIndex on <a> if one is passed via props', function () {
      wrapper.setProps({
        tabIndex: 2
      });
      expect(wrapper.find('a').props().tabIndex).toEqual(2);
    });
    it('uses label to set children on <a> when passed via props', function () {
      expect(wrapper.find('a').props().children).toEqual('firstTab');
    });
    it('sets href as # by default', function () {
      expect(wrapper.find('a').props().href).toEqual('#');
    });
    it('sets new href value when passed in via props', function () {
      wrapper.setProps({
        href: '#other-content'
      });
      expect(wrapper.find('a').props().href).toEqual('#other-content');
    });
    it("should not have [className=\"".concat(prefix, "--tabs__nav-item--selected\"] by default"), function () {
      expect(wrapper.hasClass("".concat(prefix, "--tabs__nav-item--selected"))).toBe(false);
    });
    it('has aria-disabled that matches disabled', function () {
      var getDisabledRegion = function getDisabledRegion() {
        return wrapper.find('[aria-disabled]');
      };

      expect(getDisabledRegion().length).toEqual(0);
      wrapper.setProps({
        disabled: true
      });
      expect(getDisabledRegion().prop('aria-disabled')).toEqual(true);
    });
    it("adds [className=\"".concat(prefix, "--tabs__nav-item--selected\"] when selected prop is true"), function () {
      wrapper.setProps({
        selected: true
      });
      expect(wrapper.hasClass("".concat(prefix, "--tabs__nav-item--selected"))).toBe(true);
    });
  });
  describe('events', function () {
    describe('click', function () {
      var onClick = jest.fn();
      var handleTabClick = jest.fn();
      var wrapper = shallow(React.createElement(Tab, {
        label: "firstTab"
      }));
      it('invokes handleTabClick from onClick prop', function () {
        wrapper.setProps({
          handleTabClick: handleTabClick
        });
        wrapper.simulate('click');
        expect(handleTabClick).toBeCalled();
      });
      it('invokes onClick when a function is passed to onClick prop', function () {
        wrapper.setProps({
          onClick: onClick
        });
        wrapper.simulate('click');
        expect(onClick).toBeCalled();
      });
    });
    describe('keydown', function () {
      var onKeyDown = jest.fn();
      var handleTabKeyDown = jest.fn();
      var wrapper = shallow(React.createElement(Tab, {
        label: "firstTab"
      }));
      wrapper.setProps({
        onKeyDown: onKeyDown,
        handleTabKeyDown: handleTabKeyDown
      });
      it('invokes onKeyDown when a function is passed to onKeyDown prop', function () {
        wrapper.simulate('keyDown', {
          which: 38
        });
        expect(onKeyDown).toBeCalled();
      });
      it('invokes handleTabAnchorFocus when onKeyDown occurs for appropriate events', function () {
        wrapper.simulate('keyDown', {
          which: 37
        });
        expect(onKeyDown).toBeCalled();
      });
    });
  });
  describe('custom render label', function () {
    var wrapper = mount(React.createElement(Tab, {
      renderAnchor: function renderAnchor() {
        return React.createElement("a", {
          id: "custom-label",
          href: "#other-content"
        }, "Content");
      }
    }));
    expect(wrapper.find('#custom-label').props().href).toEqual('#other-content');
  });
});