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; }

/*
Copyright (c) 2018 Uber Technologies, Inc.

This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
import React from 'react';
import { mount } from 'enzyme';
import FormControl from '../form-control.js';
import { Label, Caption, ControlContainer } from '../styled-components.js';
import { Input } from '../../input/index.js';
import { Textarea } from '../../textarea/index.js';
import { Checkbox } from '../../checkbox/index.js';
import { RadioGroup, StyledRadio } from '../../radio/index.js';
describe('FormControl - Label and Caption for controls', function () {
  test('Renders label, caption, and error for the Input component', function () {
    var rendered = mount(React.createElement(FormControl, {
      label: "Label test",
      caption: "Caption test"
    }, React.createElement(Input, null)));
    var label = rendered.find(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(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(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 = mount(React.createElement(FormControl, {
      label: React.createElement("span", null, "Label test"),
      caption: React.createElement("span", null, "Caption test")
    }, React.createElement(Input, null)));
    var label = rendered.find(Label).first();
    expect(label).toHaveText('Label test');
    var caption = rendered.find(Caption).first();
    expect(caption).toHaveText('Caption test');
    rendered.setProps({
      error: React.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.createElement("span", null, "Label test"));
    var caption = jest.fn().mockReturnValue(React.createElement("span", null, "Caption test"));
    var error = jest.fn().mockReturnValue(React.createElement("span", null, "Error test"));
    var rendered = mount(React.createElement(FormControl, {
      label: label,
      caption: caption
    }, React.createElement(Input, null)));
    var sharedProps = {
      $disabled: false,
      $error: false,
      $required: false,
      $size: 'default'
    };
    var labelRendered = rendered.find(Label).first();
    expect(label).toHaveBeenCalledWith(sharedProps);
    expect(labelRendered).toHaveText('Label test');
    var captionRendered = rendered.find(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 = mount(React.createElement(FormControl, {
      label: "Label test",
      caption: "Caption test"
    }, React.createElement(Input, {
      error: true
    })));
    var caption = rendered.find(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 = mount(React.createElement(FormControl, {
      label: "Label test",
      caption: "Caption test"
    }, React.createElement(Input, {
      disabled: true,
      required: true,
      error: true
    })));
    var label = rendered.find(Label).first();
    expect(label.props()).toMatchObject({
      $disabled: true,
      $error: true,
      $required: true,
      $size: 'default',
      children: 'Label test'
    });
    var caption = rendered.find(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 = mount(React.createElement(FormControl, {
      label: "Label test",
      caption: "Caption test"
    }, React.createElement(Textarea, {
      required: true
    })));
    var label = rendered.find(Label).first();
    expect(label).toExist();
    expect(label.props()).toMatchObject({
      $disabled: false,
      $error: false,
      $required: true,
      $size: 'default',
      children: 'Label test'
    });
    var caption = rendered.find(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 = mount(React.createElement(FormControl, {
    label: "Label test",
    caption: "Caption test"
  }, React.createElement(Checkbox, {
    required: true
  })));
  var label = rendered.find(Label).first();
  expect(label).toExist();
  expect(label.props()).toMatchObject({
    $disabled: false,
    $error: undefined,
    $required: true,
    children: 'Label test'
  });
  var caption = rendered.find(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 = mount(React.createElement(FormControl, {
    label: "Label test",
    caption: "Caption test"
  }, React.createElement(RadioGroup, {
    required: true
  }, React.createElement(StyledRadio, {
    value: "1"
  }, "First"), React.createElement(StyledRadio, {
    value: "2"
  }, "Second"), React.createElement(StyledRadio, {
    value: "3"
  }, "Third"))));
  var label = rendered.find(Label).first();
  expect(label).toExist();
  expect(label.props()).toMatchObject({
    $disabled: false,
    $error: undefined,
    $required: true,
    children: 'Label test'
  });
  var caption = rendered.find(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.createElement(ControlContainer, {
        className: "override"
      });
    };

    var formControlOverrides = {
      ControlContainer: ControlContainerOverride
    };
    var rendered = mount(React.createElement(FormControl, {
      overrides: formControlOverrides
    }, React.createElement(Input, null)));
    expect(rendered.contains(React.createElement(ControlContainer, {
      className: "override"
    }))).toEqual(true);
  });
  test('Renders label override', function () {
    var LabelOverride = function LabelOverride() {
      return React.createElement(Label, {
        className: "override"
      });
    };

    var formControlOverrides = {
      Label: LabelOverride
    };
    var rendered = mount(React.createElement(FormControl, {
      overrides: formControlOverrides,
      label: "Label test"
    }, React.createElement(Input, null)));
    expect(rendered.contains(React.createElement(Label, {
      className: "override"
    }))).toEqual(true);
  });
  test('Renders caption override', function () {
    var CaptionOverride = function CaptionOverride() {
      return React.createElement(Caption, {
        className: "override"
      });
    };

    var formControlOverrides = {
      Caption: CaptionOverride
    };
    var rendered = mount(React.createElement(FormControl, {
      overrides: formControlOverrides,
      caption: "Caption test"
    }, React.createElement(Input, null)));
    expect(rendered.contains(React.createElement(Caption, {
      className: "override"
    }))).toEqual(true);
  });
});