/*
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 { BaseInput } from '../index.js';
test('BaseInput - basic functionality', function () {
  var props = {
    placeholder: 'Placeholder',
    onFocus: jest.fn(),
    onBlur: jest.fn(),
    onChange: jest.fn(),
    onKeyDown: jest.fn(),
    onKeyPress: jest.fn(),
    onKeyUp: jest.fn(),
    overrides: {
      Before: jest.fn().mockImplementation(function () {
        return React.createElement("span", null);
      }),
      After: jest.fn().mockImplementation(function () {
        return React.createElement("span", null);
      })
    }
  }; // $FlowFixMe

  var wrapper = mount(React.createElement(BaseInput, props));
  expect(wrapper).toHaveState('isFocused', false); // Renders input, before and after

  var renderedInput = wrapper.find('input').first();
  expect(renderedInput).toExist();
  expect(renderedInput.props()).toMatchSnapshot('Base input has correct props');
  expect(renderedInput.props().onFocus).toEqual(wrapper.instance().onFocus);
  expect(renderedInput.props().onBlur).toEqual(wrapper.instance().onBlur);
  var renderedBefore = wrapper.find(props.overrides.Before);
  expect(renderedBefore).toHaveLength(1);
  expect(renderedBefore.props()).toMatchSnapshot('Before gets correct props');
  var renderedAfter = wrapper.find(props.overrides.After);
  expect(renderedAfter).toHaveLength(1);
  expect(renderedAfter.props()).toMatchSnapshot('After gets correct props'); // onFocus handler from props is called

  renderedInput.simulate('focus');
  expect(props.onFocus).toBeCalled();
  expect(wrapper).toHaveState('isFocused', true); // onBlur handler from props is called

  renderedInput.simulate('blur');
  expect(props.onBlur).toBeCalled();
  expect(wrapper).toHaveState('isFocused', false); // onChange handler from props is called

  renderedInput.simulate('change');
  expect(props.onChange).toBeCalled(); // onKeyDown handler from props is called

  renderedInput.simulate('keyDown', {
    keyCode: 40
  });
  expect(props.onKeyDown).toBeCalled(); // onKeyPress handler from props is called

  renderedInput.simulate('keyPress', {
    keyCode: 40
  });
  expect(props.onKeyPress).toBeCalled(); // onKeyUp handler from props is called

  renderedInput.simulate('keyUp', {
    keyCode: 40
  });
  expect(props.onKeyUp).toBeCalled(); // Correct props passed when error state

  wrapper.setProps({
    error: true
  });
  var updatedBefore = wrapper.find(props.overrides.Before);
  expect(updatedBefore.props()).toMatchSnapshot('Before gets correct error prop');
  var updatedAfter = wrapper.find(props.overrides.After);
  expect(updatedAfter.props()).toMatchSnapshot('After gets correct error prop');
});
test('BaseInput - should not take default value prop', function () {
  // $FlowFixMe
  var wrapper = mount(React.createElement(BaseInput, null)); // Guard against passing default value prop

  expect(wrapper.prop('value')).not.toBeDefined();
});
test('BaseInput - autoFocus sets the initial focus state', function () {
  var props = {
    autoFocus: true,
    onFocus: jest.fn(),
    onChange: jest.fn()
  }; // $FlowFixMe

  var wrapper = mount(React.createElement(BaseInput, props)); // Is focused when mount

  expect(wrapper).toHaveState('isFocused', true);
});
test('BaseInput - 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 = mount(React.createElement(BaseInput, props)); // Is focused when mount

  expect(wrapper).toHaveState('isFocused', true); // ref's focus method is called

  expect(focus).toBeCalled();
});