"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.shallow)(_react.default.createElement(_index2.ButtonGroup, props, _react.default.createElement(_index.Button, null), _react.default.createElement(_index.Button, null), _react.default.createElement(_index.Button, null)));
}

describe('ButtonGroup', function () {
  it('propagates kind to children', function () {
    var kind = 'tertiary';
    var wrapper = buildSimpleWrapper({
      kind: kind
    });
    wrapper.children().forEach(function (child) {
      expect(child).toHaveProp('kind', kind);
    });
  });
  it('propagates shape to children', function () {
    var shape = 'round';
    var wrapper = buildSimpleWrapper({
      shape: shape
    });
    wrapper.children().forEach(function (child) {
      expect(child).toHaveProp('shape', shape);
    });
  });
  it('propagates size to children', function () {
    var size = 'compact';
    var wrapper = buildSimpleWrapper({
      size: size
    });
    wrapper.children().forEach(function (child) {
      expect(child).toHaveProp('size', size);
    });
  });
  it('propagates disabled to children', function () {
    var wrapper = buildSimpleWrapper({
      disabled: true
    });
    wrapper.children().forEach(function (child) {
      expect(child).toHaveProp('disabled', true);
    });
  });
  it('click event on child element triggers parent handler', function () {
    var handler = jest.fn();
    var wrapper = buildSimpleWrapper({
      onClick: handler
    });
    wrapper.children().forEach(function (child, index) {
      child.simulate('click');
      expect(handler).toHaveBeenCalledTimes(index + 1);
    });
  });
  it('does not clobber click handler on child element', function () {
    var parentHandler = jest.fn();
    var childHandler = jest.fn();
    var wrapper = (0, _enzyme.shallow)(_react.default.createElement(_index2.ButtonGroup, {
      onClick: parentHandler
    }, _react.default.createElement("button", {
      onClick: childHandler
    }), _react.default.createElement("button", null)));
    var childWithHandler = wrapper.children().first();
    var childWithoutHandler = wrapper.children().last();
    childWithHandler.simulate('click');
    expect(parentHandler).toHaveBeenCalledTimes(1);
    expect(childHandler).toHaveBeenCalledTimes(1);
    childWithoutHandler.simulate('click');
    expect(parentHandler).toHaveBeenCalledTimes(2);
    expect(childHandler).toHaveBeenCalledTimes(1);
  });
  it('does not clobber disabled prop on child element', function () {
    var wrapper = (0, _enzyme.shallow)(_react.default.createElement(_index2.ButtonGroup, null, _react.default.createElement("button", {
      disabled: true
    }), _react.default.createElement("button", null)));
    var child = wrapper.children().first();
    expect(child).toHaveProp('disabled', true);
  });
  it('if disabled, click events do not call provided handler', function () {
    var handler = jest.fn();
    var wrapper = buildSimpleWrapper({
      disabled: true,
      onClick: handler
    });
    var child = wrapper.children().first();
    child.simulate('click');
    expect(handler).toHaveBeenCalledTimes(0);
  });
  it('sets no children as selected if selected prop is null value', function () {
    var wrapper = buildSimpleWrapper({
      selected: null
    });
    wrapper.children().forEach(function (child) {
      expect(child).toHaveProp('isSelected', false);
    });
  });
  it('sets no children as selected if selected prop is empty array', function () {
    var wrapper = buildSimpleWrapper({
      selected: []
    });
    wrapper.children().forEach(function (child) {
      expect(child).toHaveProp('isSelected', false);
    });
  });
  it('sets appropriate child as selected if selected prop is a number', function () {
    var selectedIndex = 2;
    var wrapper = buildSimpleWrapper({
      selected: selectedIndex
    });
    wrapper.children().forEach(function (child, index) {
      expect(child).toHaveProp('isSelected', selectedIndex === index);
    });
  });
  it('sets appropriate child as selected if selected prop is zero', function () {
    var selectedIndex = 0;
    var wrapper = buildSimpleWrapper({
      selected: selectedIndex
    });
    wrapper.children().forEach(function (child, index) {
      expect(child).toHaveProp('isSelected', selectedIndex === index);
    });
  });
  it('sets appropriate child as selected if selected prop is an array', function () {
    var selectedIndices = [0, 2];
    var wrapper = buildSimpleWrapper({
      selected: selectedIndices
    });
    wrapper.children().forEach(function (child, index) {
      expect(child).toHaveProp('isSelected', selectedIndices.includes(index));
    });
  });
});