"use strict";

var React = _interopRequireWildcard(require("react"));

var _overrides = require("../overrides");

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function getMockComponent() {
  var mock = function mock() {
    return null;
  };

  return mock;
}

describe('Helpers - Overrides', function () {
  test('getOverride', function () {
    var CustomComponent = getMockComponent();
    expect((0, _overrides.getOverride)(null)).toEqual(null);
    expect((0, _overrides.getOverride)(CustomComponent)).toEqual(CustomComponent);
    expect((0, _overrides.getOverride)({
      component: CustomComponent
    })).toEqual(CustomComponent);
  });
  test('getOverrideProps', function () {
    var CustomComponent = getMockComponent();
    var override = {
      props: {
        propName: 'propsValue'
      },
      style: {
        color: 'blue'
      }
    };
    expect((0, _overrides.getOverrideProps)(null)).toMatchSnapshot('returns empty object when no overrides');
    expect((0, _overrides.getOverrideProps)(CustomComponent)).toMatchSnapshot('returns empty object when override is a component');
    expect((0, _overrides.getOverrideProps)(override)).toMatchSnapshot('returns correct object when override has props and styles');
  });
  test('toObjectOverride', function () {
    var CustomComponent = getMockComponent(); // $FlowFixMe - Calling toObjectOverride with no args

    expect((0, _overrides.toObjectOverride)()).toEqual({}); // $FlowFixMe - Calling toObjectOverride with null

    expect((0, _overrides.toObjectOverride)(null)).toEqual({});
    expect((0, _overrides.toObjectOverride)(CustomComponent)).toEqual({
      component: CustomComponent
    });
    expect((0, _overrides.toObjectOverride)({
      component: CustomComponent,
      style: {
        width: '300px'
      }
    })).toEqual({
      component: CustomComponent,
      style: {
        width: '300px'
      }
    });
  });
  test('mergeOverrides', function () {
    var CustomRoot = getMockComponent();
    var CustomFoo = getMockComponent();
    var CustomBar = getMockComponent();
    var overrides1 = {
      Root: CustomRoot
    };
    var overrides2 = {
      Root: {
        component: CustomFoo
      },
      Bar: CustomBar
    };
    expect((0, _overrides.mergeOverrides)(overrides1)).toEqual({
      Root: {
        component: CustomRoot
      }
    });
    expect((0, _overrides.mergeOverrides)(overrides1, overrides2)).toEqual({
      Root: {
        component: CustomFoo
      },
      Bar: {
        component: CustomBar
      }
    });
  });
  test('mergeOverride can merge props and style objects', function () {
    expect((0, _overrides.mergeOverride)({
      props: {
        foo: true,
        bar: false
      },
      style: {
        color: 'red',
        textTransform: 'uppercase'
      }
    }, {
      props: {
        foo: false
      },
      style: {
        color: 'blue'
      }
    })).toEqual({
      props: {
        foo: false,
        bar: false
      },
      style: {
        color: 'blue',
        textTransform: 'uppercase'
      }
    });
  });
  test('mergeOverride doesnt unnecessarily create new objects', function () {
    var override1 = {
      props: {
        foo: true
      }
    };
    var override2 = {
      style: {
        color: 'red'
      }
    };
    var result = (0, _overrides.mergeOverride)(override1, override2);
    expect(result.props).toBe(override1.props);
    expect(result.style).toBe(override2.style);
  });
  test('mergeOverride can compose style functions', function () {
    var override1 = {
      style: function style() {
        return {
          color: 'red',
          textTransform: 'uppercase'
        };
      }
    };
    var override2 = {
      style: function style() {
        return {
          color: 'blue'
        };
      }
    };
    var result = (0, _overrides.mergeOverride)(override1, override2);
    expect(_typeof(result.style)).toBe('function'); // $FlowFixMe style should be a function here

    expect(result.style()).toEqual({
      color: 'blue',
      textTransform: 'uppercase'
    });
  });
  test('getOverrides', function () {
    var DefaultComponent = getMockComponent();
    var OverrideComponent = getMockComponent();
    expect((0, _overrides.getOverrides)(null, DefaultComponent)).toEqual([DefaultComponent, {}]);
    expect((0, _overrides.getOverrides)(OverrideComponent, DefaultComponent)).toEqual([OverrideComponent, {}]);
    expect((0, _overrides.getOverrides)({
      component: OverrideComponent,
      props: {
        custom: 'prop'
      },
      style: {
        cursor: 'pointer'
      }
    }, DefaultComponent)).toEqual([OverrideComponent, {
      custom: 'prop',
      $style: {
        cursor: 'pointer'
      }
    }]);
  });
});