/*
Copyright (c) 2018 Uber Technologies, Inc.

This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
import React from 'react';
import { mount } from 'enzyme';
import { StyledTab } from '../index.js';
import { default as Tab } from '../tab.js';
describe('Tab', function () {
  test('basic rendering', function () {
    var props = {
      active: true,
      children: 'title'
    };
    var renderedTab;
    var wrapper = mount(React.createElement(Tab, props));
    renderedTab = wrapper.find(StyledTab).first();
    expect(renderedTab.props()).toMatchSnapshot('styled tab has correct props');
  });
  test('component overrides', function () {
    var overrides = {
      // eslint-disable-next-line react/display-name
      Tab: function Tab(_ref) {
        var children = _ref.children;
        return React.createElement("span", null, children);
      }
    }; // $FlowFixMe

    var wrapper = mount(React.createElement(Tab, {
      overrides: overrides
    }, "Title"));
    var tab = wrapper.find(overrides.Tab);
    expect(tab).toHaveLength(1);
  });
  test('onSelect and onClick are called on a click event', function () {
    var props = {
      onSelect: jest.fn(),
      onClick: jest.fn()
    };
    var wrapper = mount(React.createElement(Tab, props, "Title"));
    var tab = wrapper.find(StyledTab);
    tab.simulate('click');
    expect(props.onSelect).toHaveBeenCalledTimes(1);
    expect(props.onClick).toHaveBeenCalledTimes(1);
  });
  test('onSelect and onKeydown are called on a Enter keydown event', function () {
    var props = {
      onSelect: jest.fn(),
      onKeyDown: jest.fn()
    };
    var event = {
      key: 'Enter'
    };
    var wrapper = mount(React.createElement(Tab, props, "Title"));
    var tab = wrapper.find(StyledTab);
    tab.simulate('keydown', event);
    expect(props.onSelect).toHaveBeenCalledTimes(1);
    expect(props.onKeyDown).toHaveBeenCalledTimes(1);
  });
  test('onSelect and onKeydown are called on a Space keydown event', function () {
    var props = {
      onSelect: jest.fn(),
      onKeyDown: jest.fn()
    };
    var event = {
      which: 32
    };
    var wrapper = mount(React.createElement(Tab, props, "Title"));
    var tab = wrapper.find(StyledTab);
    tab.simulate('keydown', event);
    expect(props.onSelect).toHaveBeenCalledTimes(1);
    expect(props.onKeyDown).toHaveBeenCalledTimes(1);
  });
  test('onClick is not called when the tab is disabled', function () {
    var props = {
      onClick: jest.fn(),
      disabled: true
    };
    var wrapper = mount(React.createElement(Tab, props, "Title"));
    var tab = wrapper.find(StyledTab);
    tab.simulate('click');
    expect(props.onClick).not.toHaveBeenCalledTimes(1);
  });
  test('onSelect nor onKeydown is not called on key presses when the tab is disabled', function () {
    var props = {
      onSelect: jest.fn(),
      onKeyDown: jest.fn(),
      disabled: true
    };
    var wrapper = mount(React.createElement(Tab, props, "Title"));
    var tab = wrapper.find(StyledTab);
    tab.simulate('keydown', {
      key: 'Enter'
    });
    tab.simulate('keydown', {
      which: 32
    });
    expect(props.onSelect).not.toHaveBeenCalledTimes(1);
    expect(props.onKeyDown).not.toHaveBeenCalledTimes(1);
  });
});