"use strict";

require("core-js/modules/es.array.find");

require("core-js/modules/es.function.name");

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

var _enzyme = require("enzyme");

var _ = _interopRequireDefault(require("."));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

var noop = function noop() {};

describe('components/Input', function () {
  test('should render an input with basic props', function () {
    var onChange = jest.fn(function (e) {
      expect(e.target.value).toBe('changed');
      expect(e.target.name).toBe('');
    });
    var wrapper = (0, _enzyme.mount)(_react["default"].createElement(_["default"], {
      value: "test",
      onChange: onChange,
      "data-name": ""
    }));
    var input = wrapper.find('input');
    expect(input.length).toBe(1);
    expect(input.instance().value).toBe('test');
    input.simulate('change', {
      target: {
        name: '',
        value: 'changed'
      }
    });
    expect(onChange).toHaveBeenCalledTimes(1);
  });
  test('should pass additional props through', function () {
    var wrapper = (0, _enzyme.mount)(_react["default"].createElement(_["default"], {
      value: "",
      onChange: noop,
      "data-name": "",
      "data-myprop": "exists"
    }));
    var input = wrapper.find('input');
    expect(input.length).toBe(1);
    expect(input.props()).toHaveProperty('data-myprop', 'exists');
  });
  test('should render a checkbox', function () {
    var onChange = jest.fn(function (e) {
      expect(e.target.value).toBe(false);
      expect(e.target.name).toBe('');
    });
    var wrapper = (0, _enzyme.mount)(_react["default"].createElement(_["default"], {
      value: true,
      onChange: onChange,
      "data-name": "",
      type: "checkbox"
    }));
    var input = wrapper.find('input');
    expect(input.length).toBe(1);
    expect(input.instance().value).toBe('on');
    expect(input.instance().checked).toBe(true);
    input.simulate('change', {
      target: {
        name: '',
        checked: false
      }
    });
    expect(onChange).toHaveBeenCalledTimes(1);
  });
  test('should render a radio', function () {
    var onChange = jest.fn(function (value) {
      return expect(value).toBe('b');
    });
    var wrapper = (0, _enzyme.mount)(_react["default"].createElement(_["default"], {
      name: "test",
      value: "a",
      "data-value": "a",
      onChange: onChange,
      "data-name": "radioName",
      type: "radio"
    }));
    var input = wrapper.find('input');
    expect(input.length).toBe(1); // Value is 'a', but the checked flag is set if it matches data-value

    expect(input.instance().value).toBe('a');
    expect(input.instance().checked).toBe(true); // It should use data-name, since that should be fairly unique in the form

    expect(input.props()).toHaveProperty('name', 'radioName'); // Emit an event with a name, and the onChange handler should just pass the
    // value on. Name is stripped intentionally

    input.simulate('change', {
      target: {
        name: 'radioName',
        value: 'b'
      }
    });
    expect(onChange).toHaveBeenCalledTimes(1); // Try rendering a radio that isn't currently selected

    var wrapper2 = (0, _enzyme.mount)(_react["default"].createElement(_["default"], {
      name: "test",
      value: "a",
      "data-value": "b",
      onChange: noop,
      "data-name": "radioName",
      type: "radio"
    }));
    var input2 = wrapper2.find('input');
    expect(input2.length).toBe(1); // Value is 'a', but the checked flag is set if it matches data-value

    expect(input2.instance().value).toBe('a');
    expect(input2.instance().checked).toBe(false);
  });
});