"use strict";

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

var _enzyme = require("enzyme");

var _index = require("../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('Stateless checkbox', function () {
  var wrapper,
      events = {};
  var allProps = {},
      overrides,
      isError,
      mockFn;
  beforeEach(function () {
    mockFn = jest.fn();
    overrides = {
      Root: _index.StyledRoot,
      Checkmark: _index.StyledCheckmark,
      Label: _index.StyledLabel,
      Input: _index.StyledInput
    };
    isError = false;
    events = {
      onChange: mockFn
    };
    allProps = _objectSpread({
      overrides: overrides
    }, events, {
      labelPlacement: 'left',
      children: 'some',
      isError: isError,
      inputRef: _react.default.createRef(),
      autoFocus: false,
      isIndeterminate: false,
      disabled: false,
      checked: false
    });
  });
  afterEach(function () {
    jest.restoreAllMocks();
    wrapper && wrapper.unmount();
  });
  test.each([['Root'], ['Label'], ['Checkmark'], ['Input']])('should send props to %s', function (subcomponent) {
    var mockComp = jest.fn(function () {
      return _react.default.createElement("div", null, subcomponent);
    });
    overrides[subcomponent] = mockComp;
    wrapper = (0, _enzyme.mount)(_react.default.createElement(_index.Checkbox, allProps));
    var instance = wrapper.instance();
    var sharedProps = {
      $isError: allProps.isError,
      $checked: allProps.checked,
      $isIndeterminate: allProps.isIndeterminate,
      $required: allProps.required,
      $disabled: allProps.disabled
    };
    var rootEvents = {
      onMouseEnter: instance.onMouseEnter,
      onMouseLeave: instance.onMouseLeave,
      onMouseUp: instance.onMouseUp,
      onMouseDown: instance.onMouseDown
    };
    events = _objectSpread({
      onFocus: instance.onFocus,
      onBlur: instance.onBlur
    }, events);
    var actualProps = mockComp.mock.calls[0][0];
    var expectedProps = {
      Root: _objectSpread({}, sharedProps, rootEvents),
      Label: _objectSpread({}, sharedProps, {
        $labelPlacement: allProps.labelPlacement
      }),
      Checkmark: sharedProps,
      Input: _objectSpread({
        type: 'checkbox',
        disabled: false,
        $ref: allProps.inputRef
      }, sharedProps, events)
    };
    expect(actualProps).toMatchObject(expectedProps[subcomponent]);
  });
  test.each([['Root'], ['Label'], ['Checkmark'], ['Input']])('should default to standard subcomponent for %s', function (subcomponent) {
    wrapper = (0, _enzyme.mount)(_react.default.createElement(_index.Checkbox, allProps));
    var expectedProps = {
      Root: _index.StyledRoot,
      Label: _index.StyledLabel,
      Checkmark: _index.StyledCheckmark,
      Input: _index.StyledInput
    };
    var actualComp = wrapper.find(expectedProps[subcomponent]);
    expect(actualComp.length).toEqual(1);
  });
  test('should show label text in label', function () {
    var mockComp = jest.fn(function () {
      return _react.default.createElement("div", null, "test");
    });
    overrides.Label = mockComp;
    allProps.children = 'super-duper label';
    wrapper = (0, _enzyme.mount)(_react.default.createElement(_index.Checkbox, allProps));
    expect(mockComp.mock.calls[0][0].children).toEqual(allProps.children);
  });
  test.each([['top', 0], ['left', 0], ['right', 3], ['bottom', 3]])('should place label according to dock to %s', function (labelPlacement, index) {
    var mockComp = jest.fn(function () {
      return _react.default.createElement("div", null, "test");
    });
    overrides.Root = mockComp;
    allProps.children = 'super-duper label';
    allProps.labelPlacement = labelPlacement;
    wrapper = (0, _enzyme.mount)(_react.default.createElement(_index.Checkbox, allProps));
    var subComp = mockComp.mock.calls[0][0].children[index];

    var isLabel = function isLabel(comp) {
      return comp.props.children === allProps.children;
    };

    expect(isLabel(subComp)).toBeTruthy();
  });
  test('should focus on element', function () {
    var mockFocus = jest.fn();
    var current = global.document.createElement('input');
    current.focus = mockFocus;
    allProps.autoFocus = true;
    allProps.inputRef = {
      current: current
    };
    wrapper = (0, _enzyme.shallow)(_react.default.createElement(_index.Checkbox, allProps));
    expect(mockFocus).toHaveBeenCalled();
  });
  describe('Events', function () {
    var events, instance, event;
    event = {};
    var handlers = [['onMouseEnter', {
      isHovered: true
    }, true], ['onMouseLeave', {
      isHovered: false,
      isActive: false
    }, true], ['onMouseUp', {
      isActive: false
    }, true], ['onMouseDown', {
      isActive: true
    }, true], ['onFocus', {
      isFocused: true
    }, false], ['onBlur', {
      isFocused: false
    }, false]];
    beforeEach(function () {
      events = {
        onMouseEnter: jest.fn(),
        onMouseLeave: jest.fn(),
        onFocus: jest.fn(),
        onBlur: jest.fn()
      };
      allProps = _objectSpread({}, allProps, events);
      wrapper = (0, _enzyme.mount)(_react.default.createElement(_index.Checkbox, allProps));
      instance = wrapper.instance();
    });
    test.each(handlers)('should call handler for %s event if it is present', function (eventHandler, state, internalEvent) {
      var setStateMock = jest.spyOn(instance, 'setState');
      var handler = instance[eventHandler];
      handler(event);

      if (!internalEvent) {
        expect(events[eventHandler]).toHaveBeenCalledWith(event);
      }

      expect(setStateMock).toHaveBeenCalledWith(state);
    });
  });
});