function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }

function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }

/*
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 { StatefulSelectContainer } from '../index.js';
import { STATE_CHANGE_TYPE } from '../constants.js';
describe('StatefulSelectContainer', function () {
  var wrapper;
  var props = {};
  beforeEach(function () {
    props = {
      children: jest.fn(function () {
        return React.createElement("div", null, "test");
      }),
      initialState: {
        value: [{
          id: 'id',
          label: 'label'
        }]
      },
      stateReducer: jest.fn(),
      overrides: {},
      onChange: jest.fn()
    };
    wrapper = mount(React.createElement(StatefulSelectContainer, props));
  });
  afterEach(function () {
    wrapper && wrapper.unmount();
  });
  test('renders correctly', function () {
    expect(wrapper).toMatchSnapshot('StatefulSelectContainer has correct render');
  });
  test('provides props to children render func', function () {
    var actualProps = props.children.mock.calls[0][0];
    expect(actualProps).toMatchObject({
      value: props.initialState.value,
      overrides: props.overrides,
      onChange: wrapper.instance().onChange
    });
  });
  test('sets initial state correctly', function () {
    expect(wrapper.state()).toMatchObject(props.initialState);
  });
  test('calls stateReducer on every change', function () {
    var newValue = {
      id: 'id2',
      label: 'label2'
    };
    var params = {
      value: _toConsumableArray(props.initialState.value).concat([newValue]),
      option: newValue,
      type: STATE_CHANGE_TYPE.select
    };
    var newStateValue = [newValue];
    props.stateReducer.mockImplementation(function () {
      return {
        value: newStateValue
      };
    });
    wrapper.instance().onChange(params);
    expect(props.stateReducer).toHaveBeenCalledWith(params.type, {
      value: params.value
    }, props.initialState);
    expect(wrapper.state().value).toEqual(newStateValue);
  });
  test('calls onChange handler with correct params', function () {
    var newValue = {
      id: 'id2',
      label: 'label2'
    };
    var params = {
      value: _toConsumableArray(props.initialState.value).concat([newValue]),
      option: newValue,
      type: STATE_CHANGE_TYPE.select
    };
    props.stateReducer.mockImplementation(function (type, nextState) {
      return nextState;
    });
    wrapper.instance().onChange(params);
    expect(props.onChange).toHaveBeenCalledWith(params);
    expect(wrapper.state().value).toEqual(params.value);
  });
});