'use strict';

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _enzyme = require('enzyme');

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _semanticUiReact = require('semantic-ui-react');

var _ = require('../');

var _2 = _interopRequireDefault(_);

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

var buildWrapper = function buildWrapper(props) {
  return (0, _enzyme.shallow)(_react2.default.createElement(
    _2.default,
    _extends({
      className: 'test-class',
      onSubmit: jest.fn(),
      onSubmitSuccess: jest.fn(),
      validate: function validate(_ref) {
        var age = _ref.age,
            name = _ref.name;

        var errors = {};
        if (!name) {
          errors.name = 'Name is required';
        }
        if (!Number.isInteger(age)) {
          errors.age = 'Age should be an integer';
        }
        return errors;
      },
      value: {
        age: 29
      }
    }, props),
    'Test Children Content'
  ));
};

describe('FormValidator', function () {
  it('should render form and context provider with no errors at first', function () {
    expect(buildWrapper()).toMatchSnapshot();
  });

  describe('when the form is submitted with errors', function () {
    var wrapper = void 0;

    beforeEach(function () {
      wrapper = buildWrapper();
      wrapper.find(_semanticUiReact.Form).simulate('submit');
    });

    it('should render a context provider with the validation errors', function () {
      expect(wrapper).toMatchSnapshot();
    });

    it('should call "onSubmit" prop', function () {
      var _wrapper$instance = wrapper.instance(),
          onSubmit = _wrapper$instance.props.onSubmit;

      expect(onSubmit).toHaveBeenCalled();
    });

    describe('when the "value" changes', function () {
      it('should clear existing errors that were resolved', function () {
        wrapper.setProps({
          value: { name: 'Maira', age: 29 }
        });
        expect(wrapper).toMatchSnapshot();
      });

      it('should not show new errors', function () {
        wrapper.setProps({ value: { age: 'String age?' } });
        expect(wrapper).toMatchSnapshot();
      });
    });
  });

  describe('when the form is submitted without errors', function () {
    var wrapper = void 0;

    beforeEach(function () {
      wrapper = buildWrapper({
        value: { name: 'Maira', age: 29 }
      });
      wrapper.find(_semanticUiReact.Form).simulate('submit');
    });

    it('should call the "onSubmitSuccess" prop', function () {
      var _wrapper$instance2 = wrapper.instance(),
          onSubmitSuccess = _wrapper$instance2.props.onSubmitSuccess;

      expect(onSubmitSuccess).toHaveBeenCalled();
    });
  });
});