"use strict";

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

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

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

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

require("core-js/modules/es.object.define-properties");

require("core-js/modules/es.object.define-property");

require("core-js/modules/es.object.get-own-property-descriptor");

require("core-js/modules/es.object.get-own-property-descriptors");

require("core-js/modules/es.object.keys");

require("core-js/modules/web.dom-collections.for-each");

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

var _enzyme = require("enzyme");

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

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

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var values = {
  a: true,
  b: null,
  c: ['1', '2'],
  d: {
    d1: 'nested'
  },
  e: "value"
};

var noop = function noop() {};

var context = {
  ffGetValue: function ffGetValue() {
    return values;
  },
  ffFullName: function ffFullName() {
    return [];
  },
  ffFormIndex: 1,
  ffOnChange: noop,
  formSubscription: {
    subscribe: noop,
    unsubscribe: noop
  }
};
describe('components/Field', function () {
  test('should filter out internal props from the passthrough props', function () {
    var wrapper = (0, _enzyme.shallow)(_react["default"].createElement(_["default"], {
      name: "a",
      component: "input",
      onChange: noop,
      a: "1"
    }), {
      context: context
    });
    expect(wrapper.instance().getOtherProps()).toEqual({
      a: '1',
      children: null
    });
  });
  test('should support overriding the input component', function () {
    expect((0, _enzyme.mount)(_react["default"].createElement(_["default"], null), {
      context: context
    }).find('input').length).toBe(1);

    var TestInput = function TestInput() {
      return _react["default"].createElement("h1", null, "Test");
    };

    var wrapper = (0, _enzyme.shallow)(_react["default"].createElement(_["default"], {
      component: TestInput
    }), {
      context: context
    });
    expect(wrapper.find('input').length).toBe(0);
    expect(wrapper.find(TestInput).length).toBe(1);
  });
  test('should pass data-value with the true value, if it receives value as a prop', function () {
    var wrapper = (0, _enzyme.mount)(_react["default"].createElement(_["default"], {
      value: "test",
      name: "a"
    }), {
      context: context
    });
    var input = wrapper.find('input');
    expect(input.props()).toHaveProperty('value', 'test');
    expect(input.props()).toHaveProperty('data-value', true);
  });
  test('should pass data-name as a string of the entire form path', function () {
    expect((0, _enzyme.mount)(_react["default"].createElement(_["default"], {
      value: "test",
      name: "a"
    }), {
      context: context
    }).find('input').props()).toHaveProperty('data-name', 'a');
    expect((0, _enzyme.mount)(_react["default"].createElement(_["default"], {
      value: "test",
      name: "d1"
    }), {
      context: _objectSpread({}, context, {
        ffFullName: function ffFullName() {
          return ['d'];
        },
        ffGetValue: function ffGetValue() {
          return {
            d1: 'test'
          };
        }
      })
    }).find('input').props()).toHaveProperty('data-name', 'd.d1');
  });
  test('should pass id as a string of the entire form path with a form identifier', function () {
    expect((0, _enzyme.mount)(_react["default"].createElement(_["default"], {
      value: "test",
      name: "a"
    }), {
      context: context
    }).find('input').props()).toHaveProperty('id', '1.a');
    expect((0, _enzyme.mount)(_react["default"].createElement(_["default"], {
      value: "test",
      name: "d1"
    }), {
      context: _objectSpread({}, context, {
        ffFullName: function ffFullName() {
          return ['d'];
        },
        ffGetValue: function ffGetValue() {
          return {
            d1: 'test'
          };
        }
      })
    }).find('input').props()).toHaveProperty('id', '1.d.d1');
  });
  test('should pass a working onChange into the input', function () {
    var ffOnChange = jest.fn();
    var input = (0, _enzyme.mount)(_react["default"].createElement(_["default"], {
      name: "a"
    }), {
      context: _objectSpread({}, context, {
        ffOnChange: ffOnChange
      })
    }).find('input');
    input.simulate('change', {
      target: {
        name: '',
        value: false
      }
    });
    expect(ffOnChange).toHaveBeenCalledTimes(1);
    expect(ffOnChange.mock.calls[0][0].target).toEqual({
      name: ['a'],
      value: false
    });
  });
  test('should render out children', function () {
    var FakeComponent = function FakeComponent(_ref) {
      var children = _ref.children;
      return _react["default"].createElement("div", null, children);
    };

    var select = (0, _enzyme.mount)(_react["default"].createElement(_["default"], {
      name: "e",
      component: FakeComponent
    }, "test"), {
      context: context
    });
    expect(select.text()).toBe('test');
  });
});