"use strict";

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

var _enzyme = require("enzyme");

var _formControl = _interopRequireDefault(require("../form-control.js"));

var _styledComponents = require("../styled-components.js");

var _index = require("../../input/index.js");

var _index2 = require("../../textarea/index.js");

var _index3 = require("../../checkbox/index.js");

var _index4 = require("../../radio/index.js");

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

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, 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; }

describe('FormControl - Label and Caption for controls', function () {
  test('Renders label, caption, and error for the Input component', function () {
    var rendered = (0, _enzyme.mount)(_react.default.createElement(_formControl.default, {
      label: "Label test",
      caption: "Caption test"
    }, _react.default.createElement(_index.Input, null)));
    var label = rendered.find(_styledComponents.Label).first();
    expect(label).toExist();
    expect(label).toHaveText('Label test');
    expect(label.props()).toMatchObject({
      $disabled: false,
      $error: false,
      $required: false,
      $size: 'default',
      children: 'Label test'
    });
    var caption = rendered.find(_styledComponents.Caption).first();
    expect(caption).toExist();
    expect(caption).toHaveText('Caption test');
    expect(caption.props()).toMatchObject({
      $disabled: false,
      $error: false,
      $required: false,
      $size: 'default',
      children: 'Caption test'
    });
    rendered.setProps({
      error: 'Error test'
    });
    caption = rendered.find(_styledComponents.Caption).first();
    expect(caption).toHaveText('Error test');
    expect(caption.props()).toMatchObject({
      $disabled: false,
      $error: 'Error test',
      $required: false,
      $size: 'default',
      children: 'Error test'
    });
  });
  test('Accepts a node for label, caption, and error', function () {
    var rendered = (0, _enzyme.mount)(_react.default.createElement(_formControl.default, {
      label: _react.default.createElement("span", null, "Label test"),
      caption: _react.default.createElement("span", null, "Caption test")
    }, _react.default.createElement(_index.Input, null)));
    var label = rendered.find(_styledComponents.Label).first();
    expect(label).toHaveText('Label test');
    var caption = rendered.find(_styledComponents.Caption).first();
    expect(caption).toHaveText('Caption test');
    rendered.setProps({
      error: _react.default.createElement("span", null, "Error test")
    });
    expect(caption).toHaveText('Error test');
  });
  test('Accepts a function for label, caption, and error', function () {
    var label = jest.fn().mockReturnValue(_react.default.createElement("span", null, "Label test"));
    var caption = jest.fn().mockReturnValue(_react.default.createElement("span", null, "Caption test"));
    var error = jest.fn().mockReturnValue(_react.default.createElement("span", null, "Error test"));
    var rendered = (0, _enzyme.mount)(_react.default.createElement(_formControl.default, {
      label: label,
      caption: caption
    }, _react.default.createElement(_index.Input, null)));
    var sharedProps = {
      $disabled: false,
      $error: false,
      $required: false,
      $size: 'default'
    };
    var labelRendered = rendered.find(_styledComponents.Label).first();
    expect(label).toHaveBeenCalledWith(sharedProps);
    expect(labelRendered).toHaveText('Label test');
    var captionRendered = rendered.find(_styledComponents.Caption).first();
    expect(caption).toHaveBeenCalledWith(sharedProps);
    expect(captionRendered).toHaveText('Caption test');
    expect(error).not.toHaveBeenCalled();
    rendered.setProps({
      error: error
    });
    expect(error).toHaveBeenCalledWith(_objectSpread({}, sharedProps, {
      $error: error
    }));
    expect(captionRendered).toHaveText('Error test');
  });
  test('Renders caption if error is set on the control', function () {
    var rendered = (0, _enzyme.mount)(_react.default.createElement(_formControl.default, {
      label: "Label test",
      caption: "Caption test"
    }, _react.default.createElement(_index.Input, {
      error: true
    })));
    var caption = rendered.find(_styledComponents.Caption).first();
    expect(caption).toHaveText('Caption test');
    expect(caption.props()).toMatchObject({
      $disabled: false,
      $error: true,
      $required: false,
      $size: 'default',
      children: 'Caption test'
    });
  });
  test('Passes correct props from the control to label and caption', function () {
    var rendered = (0, _enzyme.mount)(_react.default.createElement(_formControl.default, {
      label: "Label test",
      caption: "Caption test"
    }, _react.default.createElement(_index.Input, {
      disabled: true,
      required: true,
      error: true
    })));
    var label = rendered.find(_styledComponents.Label).first();
    expect(label.props()).toMatchObject({
      $disabled: true,
      $error: true,
      $required: true,
      $size: 'default',
      children: 'Label test'
    });
    var caption = rendered.find(_styledComponents.Caption).first();
    expect(caption.props()).toMatchObject({
      $disabled: true,
      $error: true,
      $required: true,
      $size: 'default',
      children: 'Caption test'
    });
    rendered.setProps({
      error: 'Error test'
    });
    expect(label.props()).toMatchObject({
      $disabled: true,
      $error: true,
      $required: true,
      $size: 'default',
      children: 'Label test'
    });
    expect(caption.props()).toMatchObject({
      $disabled: true,
      $error: true,
      $required: true,
      $size: 'default',
      children: 'Caption test'
    });
    expect(caption).toHaveText('Error test');
  });
  test('Renders label and caption for the Textarea component', function () {
    var rendered = (0, _enzyme.mount)(_react.default.createElement(_formControl.default, {
      label: "Label test",
      caption: "Caption test"
    }, _react.default.createElement(_index2.Textarea, {
      required: true
    })));
    var label = rendered.find(_styledComponents.Label).first();
    expect(label).toExist();
    expect(label.props()).toMatchObject({
      $disabled: false,
      $error: false,
      $required: true,
      $size: 'default',
      children: 'Label test'
    });
    var caption = rendered.find(_styledComponents.Caption).first();
    expect(caption).toExist();
    expect(caption.props()).toMatchObject({
      $disabled: false,
      $error: false,
      $required: true,
      $size: 'default',
      children: 'Caption test'
    });
  });
});
test('Renders label and caption for the Checkbox component', function () {
  var rendered = (0, _enzyme.mount)(_react.default.createElement(_formControl.default, {
    label: "Label test",
    caption: "Caption test"
  }, _react.default.createElement(_index3.Checkbox, {
    required: true
  })));
  var label = rendered.find(_styledComponents.Label).first();
  expect(label).toExist();
  expect(label.props()).toMatchObject({
    $disabled: false,
    $error: undefined,
    $required: true,
    children: 'Label test'
  });
  var caption = rendered.find(_styledComponents.Caption).first();
  expect(caption).toExist();
  expect(caption.props()).toMatchObject({
    $disabled: false,
    $error: undefined,
    $required: true,
    children: 'Caption test'
  });
});
test('Renders label and caption for the RadioGroup component', function () {
  var rendered = (0, _enzyme.mount)(_react.default.createElement(_formControl.default, {
    label: "Label test",
    caption: "Caption test"
  }, _react.default.createElement(_index4.RadioGroup, {
    required: true
  }, _react.default.createElement(_index4.StyledRadio, {
    value: "1"
  }, "First"), _react.default.createElement(_index4.StyledRadio, {
    value: "2"
  }, "Second"), _react.default.createElement(_index4.StyledRadio, {
    value: "3"
  }, "Third"))));
  var label = rendered.find(_styledComponents.Label).first();
  expect(label).toExist();
  expect(label.props()).toMatchObject({
    $disabled: false,
    $error: undefined,
    $required: true,
    children: 'Label test'
  });
  var caption = rendered.find(_styledComponents.Caption).first();
  expect(caption).toExist();
  expect(caption.props()).toMatchObject({
    $disabled: false,
    $error: undefined,
    $required: true,
    children: 'Caption test'
  });
});
describe('FormControl - overrides', function () {
  test('Renders control container override', function () {
    var ControlContainerOverride = function ControlContainerOverride() {
      return _react.default.createElement(_styledComponents.ControlContainer, {
        className: "override"
      });
    };

    var formControlOverrides = {
      ControlContainer: ControlContainerOverride
    };
    var rendered = (0, _enzyme.mount)(_react.default.createElement(_formControl.default, {
      overrides: formControlOverrides
    }, _react.default.createElement(_index.Input, null)));
    expect(rendered.contains(_react.default.createElement(_styledComponents.ControlContainer, {
      className: "override"
    }))).toEqual(true);
  });
  test('Renders label override', function () {
    var LabelOverride = function LabelOverride() {
      return _react.default.createElement(_styledComponents.Label, {
        className: "override"
      });
    };

    var formControlOverrides = {
      Label: LabelOverride
    };
    var rendered = (0, _enzyme.mount)(_react.default.createElement(_formControl.default, {
      overrides: formControlOverrides,
      label: "Label test"
    }, _react.default.createElement(_index.Input, null)));
    expect(rendered.contains(_react.default.createElement(_styledComponents.Label, {
      className: "override"
    }))).toEqual(true);
  });
  test('Renders caption override', function () {
    var CaptionOverride = function CaptionOverride() {
      return _react.default.createElement(_styledComponents.Caption, {
        className: "override"
      });
    };

    var formControlOverrides = {
      Caption: CaptionOverride
    };
    var rendered = (0, _enzyme.mount)(_react.default.createElement(_formControl.default, {
      overrides: formControlOverrides,
      caption: "Caption test"
    }, _react.default.createElement(_index.Input, null)));
    expect(rendered.contains(_react.default.createElement(_styledComponents.Caption, {
      className: "override"
    }))).toEqual(true);
  });
});