"use strict";

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

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

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 = {
  number: 1,
  json: JSON.stringify({
    nested: 'value'
  })
};

var noop = function noop(v) {
  return v;
};

var context = {
  ffOnChange: noop,
  ffGetValue: function ffGetValue() {
    return values;
  },
  ffFullName: function ffFullName() {
    return [];
  },
  formSubscription: {
    subscribe: noop,
    unsubscribe: noop
  }
};
describe('components/ValueTransformer', function () {
  test('changes values being passed down', function () {
    var wrapper = (0, _enzyme.shallow)(_react["default"].createElement(_["default"], {
      name: "number",
      transformOnChange: noop,
      transformValue: function transformValue(v) {
        return v * 2;
      }
    }), {
      context: context
    });
    var component = wrapper.instance();
    expect(component.getChildContext().ffGetValue()).toBe(2);
  });
  test('changes values from onChange', function () {
    var onChange = jest.fn();
    var wrapper = (0, _enzyme.shallow)(_react["default"].createElement(_["default"], {
      name: "number",
      transformOnChange: function transformOnChange(v) {
        return v * 2;
      },
      transformValue: noop
    }), {
      context: _objectSpread({}, context, {
        ffOnChange: onChange
      })
    });
    var component = wrapper.instance();
    component.getChildContext().ffOnChange({
      target: {
        name: ['number'],
        value: 1
      }
    });
    expect(onChange.mock.calls[0][0].target.value).toBe(2);
  });
  test('allows the type to change in transformOnChange', function () {
    var onChange = jest.fn();
    var wrapper = (0, _enzyme.shallow)(_react["default"].createElement(_["default"], {
      name: "json",
      transformOnChange: function transformOnChange(v) {
        return JSON.stringify(v);
      },
      transformValue: noop
    }), {
      context: _objectSpread({}, context, {
        ffOnChange: onChange
      })
    });
    var component = wrapper.instance();
    component.getChildContext().ffOnChange({
      target: {
        name: ['json'],
        value: {
          nested: 'changed'
        }
      }
    });
    expect(onChange.mock.calls[0][0].target.value).toBe(JSON.stringify({
      nested: 'changed'
    }));
  });
});