"use strict";

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

var _enzyme = require("enzyme");

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

var _index2 = require("../index.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.
*/
function buildSimpleWrapper() {
  var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  return (0, _enzyme.mount)(_react.default.createElement(_index2.StatefulButtonGroup, props, _react.default.createElement(_index.Button, null), _react.default.createElement(_index.Button, null), _react.default.createElement(_index.Button, null)));
}

function clickChildAtIndex(wrapper, index) {
  var childAtIndex = wrapper.find('div').childAt(index);
  childAtIndex.simulate('click');
}

function expectSelectedValueAtIndex(wrapper, index, value) {
  var childAtIndex = wrapper.find('div').childAt(index);
  expect(childAtIndex).toHaveProp('isSelected', value);
}

describe('StatefulButtonGroup', function () {
  it('renders with default props', function () {
    var wrapper = buildSimpleWrapper();
    expect(wrapper).toExist();
    expect(wrapper.find('div').first()).toExist();
  });
  describe('default mode', function () {
    it('does not update state when clicked', function () {
      var wrapper = buildSimpleWrapper();
      var index = 0;
      clickChildAtIndex(wrapper, index);
      expectSelectedValueAtIndex(wrapper, index, false);
    });
  });
  describe('radio mode', function () {
    it('sets button selected when initially clicked', function () {
      var wrapper = buildSimpleWrapper({
        mode: _index2.MODE.radio
      });
      var index = 0;
      clickChildAtIndex(wrapper, index);
      expectSelectedValueAtIndex(wrapper, index, true);
    });
    it('sets button unselected on second click', function () {
      var wrapper = buildSimpleWrapper({
        mode: _index2.MODE.radio
      });
      var index = 1;
      clickChildAtIndex(wrapper, index);
      expectSelectedValueAtIndex(wrapper, index, true);
      clickChildAtIndex(wrapper, index);
      expectSelectedValueAtIndex(wrapper, index, false);
    });
    it('sets button unselected when another child is clicked', function () {
      var wrapper = buildSimpleWrapper({
        mode: _index2.MODE.radio
      });
      var first = 0;
      var last = 2;
      clickChildAtIndex(wrapper, first);
      expectSelectedValueAtIndex(wrapper, first, true);
      clickChildAtIndex(wrapper, last);
      expectSelectedValueAtIndex(wrapper, first, false);
      expectSelectedValueAtIndex(wrapper, last, true);
    });
    it('sets initial selected', function () {
      var wrapper = buildSimpleWrapper({
        mode: _index2.MODE.radio,
        initialState: {
          selected: 0
        }
      });
      expectSelectedValueAtIndex(wrapper, 0, true);
      expectSelectedValueAtIndex(wrapper, 1, false);
      expectSelectedValueAtIndex(wrapper, 2, false);
    });
    it('handles provided state reducer', function () {
      var wrapper = buildSimpleWrapper({
        mode: _index2.MODE.radio,
        // always sets the first child selected
        stateReducer: function stateReducer() {
          return {
            selected: [0]
          };
        }
      });
      expectSelectedValueAtIndex(wrapper, 0, false);
      clickChildAtIndex(wrapper, 2);
      expectSelectedValueAtIndex(wrapper, 0, true);
    });
  });
  describe('checkbox mode', function () {
    it('sets button selected when initially clicked', function () {
      var wrapper = buildSimpleWrapper({
        mode: _index2.MODE.checkbox
      });
      var index = 0;
      clickChildAtIndex(wrapper, index);
      expectSelectedValueAtIndex(wrapper, index, true);
    });
    it('sets button unselected on second click', function () {
      var wrapper = buildSimpleWrapper({
        mode: _index2.MODE.checkbox
      });
      var index = 1;
      clickChildAtIndex(wrapper, index);
      expectSelectedValueAtIndex(wrapper, index, true);
      clickChildAtIndex(wrapper, index);
      expectSelectedValueAtIndex(wrapper, index, false);
    });
    it('maintains selection when another child is clicked', function () {
      var wrapper = buildSimpleWrapper({
        mode: _index2.MODE.checkbox
      });
      var first = 0;
      var last = 2;
      clickChildAtIndex(wrapper, first);
      expectSelectedValueAtIndex(wrapper, first, true);
      clickChildAtIndex(wrapper, last);
      expectSelectedValueAtIndex(wrapper, first, true);
      expectSelectedValueAtIndex(wrapper, last, true);
    });
    it('sets initial selected', function () {
      var wrapper = buildSimpleWrapper({
        mode: _index2.MODE.checkbox,
        initialState: {
          selected: [0, 1]
        }
      });
      expectSelectedValueAtIndex(wrapper, 0, true);
      expectSelectedValueAtIndex(wrapper, 1, true);
      expectSelectedValueAtIndex(wrapper, 2, false);
    });
    it('handles provided state reducer', function () {
      var wrapper = buildSimpleWrapper({
        mode: _index2.MODE.checkbox,
        // always sets the first child selected
        stateReducer: function stateReducer() {
          return {
            selected: [0]
          };
        }
      });
      expectSelectedValueAtIndex(wrapper, 0, false);
      clickChildAtIndex(wrapper, 2);
      expectSelectedValueAtIndex(wrapper, 0, true);
    });
  });
});