"use strict";

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

var _enzyme = require("enzyme");

var _constants = require("../constants");

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

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('Stateful container', function () {
  var allProps, childFn;
  var wrapper;
  beforeEach(function () {
    var stateReducer = function stateReducer(type, nextState) {
      return nextState;
    };

    childFn = jest.fn(function () {
      return _react.default.createElement("div", null, "test");
    });
    allProps = {
      children: childFn,
      initialState: {},
      stateReducer: stateReducer,
      prop1: 'some other propq'
    };
  });
  afterEach(function () {
    jest.restoreAllMocks();
    wrapper && wrapper.unmount();
  });
  test('should provide all needed props to children render func', function () {
    wrapper = (0, _enzyme.mount)(_react.default.createElement(_index.StatefulContainer, allProps));
    var actualProps = childFn.mock.calls[0][0];
    expect(actualProps).toMatchObject({
      prop1: allProps.prop1
    });
  });
  test('should provide initial state as part of state', function () {
    allProps.initialState = {
      prop3: 'some initial state'
    };
    wrapper = (0, _enzyme.mount)(_react.default.createElement(_index.StatefulContainer, allProps));
    var actualProps = childFn.mock.calls[0][0];
    expect(actualProps).toMatchObject(allProps.initialState);
  });
  describe('Events', function () {
    var events, stateReducerMock, instance, event;
    event = {
      target: {
        value: 'someValue'
      }
    };
    var handlers = [['onChange', _constants.STATE_TYPE.change, {
      value: event.target.value
    }]];
    beforeEach(function () {
      events = {
        onChange: jest.fn()
      };
      allProps = _objectSpread({}, allProps, events);
      stateReducerMock = jest.fn();
      allProps.stateReducer = stateReducerMock;
      wrapper = (0, _enzyme.mount)(_react.default.createElement(_index.StatefulContainer, allProps));
      instance = wrapper.instance();
    });
    test.each([handlers[0]])('should call state reducer to apply new state for %s event', function (eventHandler, type, newState) {
      var handler = instance[eventHandler];
      handler(event);
      expect(stateReducerMock).toHaveBeenCalledWith(type, newState, {}, event);
    });
    test.each(handlers)('should call handler for %s event if it is present', function (eventHandler) {
      var handler = instance[eventHandler];
      handler(event);
      expect(events[eventHandler]).toHaveBeenCalledWith(event);
    });
  });
});