"use strict";

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

var _enzyme = require("enzyme");

var _textarea = _interopRequireDefault(require("../textarea"));

var _styledComponents = require("../styled-components");

var _input = require("../../input");

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('Textarea', function () {
  test('Basic functionality', function () {
    var props = {
      value: 'textarea value',
      placeholder: 'Placeholder',
      onFocus: jest.fn(),
      onBlur: jest.fn(),
      onChange: jest.fn()
    };
    var wrapper = (0, _enzyme.mount)(_react.default.createElement(_textarea.default, props));
    var baseInput = wrapper.find(_input.BaseInput);
    expect(baseInput).toExist();
    expect(baseInput.instance().state.isFocused).toEqual(false);
    expect(baseInput.props()).toMatchSnapshot('textarea renders BaseInput with correct props passed'); // Renders Textarea

    var renderedTextarea = wrapper.find('textarea').first();
    expect(renderedTextarea).toExist();
    expect(renderedTextarea.props()).toMatchSnapshot('Textarea has correct props');
    expect(renderedTextarea.props().onFocus).toEqual(baseInput.instance().onFocus);
    expect(renderedTextarea.props().onFocus).not.toEqual(props.onFocus);
    expect(renderedTextarea.props().onBlur).toEqual(baseInput.instance().onBlur);
    expect(renderedTextarea.props().onBlur).not.toEqual(props.onBlur); // onFocus handler from props is called

    renderedTextarea.simulate('focus');
    expect(props.onFocus).toBeCalled();
    expect(baseInput.instance().state.isFocused).toEqual(true); // onBlur handler from props is called

    renderedTextarea.simulate('blur');
    expect(props.onBlur).toBeCalled();
    expect(baseInput.instance().state.isFocused).toEqual(false); // onChange handler from props is called

    renderedTextarea.simulate('change');
    expect(props.onChange).toBeCalled();
  });
  test('autoFocus sets the initial focus state', function () {
    var props = {
      autoFocus: true,
      onChange: jest.fn()
    }; // $FlowFixMe

    var wrapper = (0, _enzyme.mount)(_react.default.createElement(_textarea.default, props));
    var baseInput = wrapper.find(_input.BaseInput); // Is focused when mount

    expect(baseInput.instance().state.isFocused).toEqual(true);
  });
  test('inputRef from props', function () {
    var focus = jest.fn();
    var props = {
      autoFocus: true,
      onFocus: jest.fn(),
      onChange: jest.fn(),
      inputRef: {
        current: {
          focus: focus
        }
      }
    }; // $FlowFixMe

    var wrapper = (0, _enzyme.mount)(_react.default.createElement(_textarea.default, props));
    var baseInput = wrapper.find(_input.BaseInput); // Is focused when mount

    expect(baseInput.instance().state.isFocused).toEqual(true); // ref's focus method is called

    expect(focus).toBeCalled();
  });
  test('With component overrides', function () {
    var props = {
      value: 'textarea value',
      placeholder: 'Placeholder',
      onFocus: jest.fn(),
      onBlur: jest.fn(),
      onChange: jest.fn(),
      overrides: {
        InputContainer: {
          component: function CustomContainer(props) {
            return _react.default.createElement("span", {
              id: "test-container"
            }, props.children);
          }
        },
        Input: function CustomTextarea(props) {
          return _react.default.createElement("span", {
            id: "test-input"
          }, _react.default.createElement(_styledComponents.Textarea, props));
        }
      }
    }; // $FlowFixMe

    var wrapper = (0, _enzyme.mount)(_react.default.createElement(_textarea.default, props));
    var baseInput = wrapper.find(_input.BaseInput);
    expect(baseInput.props()).toMatchSnapshot('components overrides get passed to the BaseInput');
    var customContainer = wrapper.find('span#test-container');
    expect(customContainer).toExist();
    var customInput = wrapper.find('span#test-input');
    expect(customInput).toExist();
    var customTextarea = wrapper.find('span#test-input textarea');
    expect(customTextarea).toExist();
    customTextarea.simulate('focus');
    expect(baseInput.instance().state.isFocused).toEqual(true);
    expect(props.onFocus).toHaveBeenCalled();
  });
});