"use strict";

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

var _enzyme = require("enzyme");

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

var _constants = require("../constants.js");

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

/*
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.
*/
describe('StatefulTabs', function () {
  var Component = function Component(props) {
    return _react.default.createElement(_index.StatefulTabs, props, _react.default.createElement(_index.TabPanel, {
      title: "Tab Link 1"
    }, "Tab 1 content"), _react.default.createElement(_index.TabPanel, {
      title: "Tab Link 2"
    }, "Tab 2 content"), _react.default.createElement(_index.TabPanel, {
      title: "Tab Link 3"
    }, "Tab 3 content"));
  };

  test('basic render', function () {
    var component = (0, _enzyme.mount)(_react.default.createElement(Component, null));
    expect(component).toMatchSnapshot('Stateful tabs render correctly');
  });
  test('basic render with initial state', function () {
    var props = {
      initialState: {
        activeKey: '1'
      }
    };
    var component = (0, _enzyme.mount)(_react.default.createElement(Component, props));
    expect(component).toMatchSnapshot('Stateful tabs render correctly with initial state');
    var tab = component.find(_index.StyledTab).at(1);
    expect(tab).toHaveProp('$active', true);
    var panel = component.find(_index.StyledTabContent).at(1);
    expect(panel).toHaveProp('$active', true);
  });
  test('onChange functionality', function () {
    var props = {
      onChange: jest.fn()
    };
    var component = (0, _enzyme.mount)(_react.default.createElement(Component, props));
    var tab = component.find(_index.StyledTab).at(1);
    tab.simulate('click');
    var newState = {
      activeKey: '1'
    };
    expect(props.onChange).toHaveBeenCalledTimes(1);
    expect(props.onChange).toHaveBeenCalledWith(newState);
  });
  test('stateReducer functionality', function () {
    var props = {
      stateReducer: jest.fn(function (type, newState) {
        return newState;
      })
    };
    var component = (0, _enzyme.mount)(_react.default.createElement(Component, props));
    var tab = component.find(_index.StyledTab).at(1);
    tab.simulate('click');
    var oldState = {
      activeKey: '0'
    };
    var newState = {
      activeKey: '1'
    };
    expect(props.stateReducer).toHaveBeenCalledTimes(1);
    expect(props.stateReducer).toHaveBeenCalledWith(_constants.STATE_CHANGE_TYPE.change, newState, oldState);
  });
  test('state changes', function () {
    var component = (0, _enzyme.mount)(_react.default.createElement(Component, null));
    expect(component.find(_index.StyledTab).at(0)).toHaveProp('$active', true);
    component.find(_index.StyledTab).at(1).simulate('click');
    expect(component.find(_index.StyledTab).at(1)).toHaveProp('$active', true); // Click the same tab again

    component.find(_index.StyledTab).at(1).simulate('click');
    expect(component.find(_index.StyledTab).at(1)).toHaveProp('$active', true);
    component.find(_index.StyledTab).at(0).simulate('click');
    expect(component.find(_index.StyledTab).at(0)).toHaveProp('$active', true);
  });
});