/*
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 { StatefulTabs, TabPanel, StyledTab, StyledTabContent } from '../index.js';
import { STATE_CHANGE_TYPE } from '../constants.js';
describe('StatefulTabs', function () {
  var Component = function Component(props) {
    return React.createElement(StatefulTabs, props, React.createElement(TabPanel, {
      title: "Tab Link 1"
    }, "Tab 1 content"), React.createElement(TabPanel, {
      title: "Tab Link 2"
    }, "Tab 2 content"), React.createElement(TabPanel, {
      title: "Tab Link 3"
    }, "Tab 3 content"));
  };

  test('basic render', function () {
    var component = mount(React.createElement(Component, null));
    expect(component).toMatchSnapshot('Stateful tabs render correctly');
  });
  test('basic render with initial state', function () {
    var props = {
      initialState: {
        activeKey: '1'
      }
    };
    var component = mount(React.createElement(Component, props));
    expect(component).toMatchSnapshot('Stateful tabs render correctly with initial state');
    var tab = component.find(StyledTab).at(1);
    expect(tab).toHaveProp('$active', true);
    var panel = component.find(StyledTabContent).at(1);
    expect(panel).toHaveProp('$active', true);
  });
  test('onChange functionality', function () {
    var props = {
      onChange: jest.fn()
    };
    var component = mount(React.createElement(Component, props));
    var tab = component.find(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 = mount(React.createElement(Component, props));
    var tab = component.find(StyledTab).at(1);
    tab.simulate('click');
    var oldState = {
      activeKey: '0'
    };
    var newState = {
      activeKey: '1'
    };
    expect(props.stateReducer).toHaveBeenCalledTimes(1);
    expect(props.stateReducer).toHaveBeenCalledWith(STATE_CHANGE_TYPE.change, newState, oldState);
  });
  test('state changes', function () {
    var component = mount(React.createElement(Component, null));
    expect(component.find(StyledTab).at(0)).toHaveProp('$active', true);
    component.find(StyledTab).at(1).simulate('click');
    expect(component.find(StyledTab).at(1)).toHaveProp('$active', true); // Click the same tab again

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