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

/*
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.
*/
describe('StatefulComponentContainer', function () {
  test('basic render', function () {
    var props = {
      overrides: {
        Root: function CustomRoot() {
          return _react.default.createElement("span", null);
        }
      },
      initialState: {
        prop: false
      },
      onClick: jest.fn(),
      stateReducer: jest.fn()
    };
    var children = jest.fn();
    (0, _enzyme.shallow)(_react.default.createElement(_index.StatefulContainer, props, children));
    expect(children).toHaveBeenCalledTimes(1);
  });
  test('children function receives onClick handler', function () {
    var props = {
      onClick: jest.fn(),
      stateReducer: jest.fn()
    };
    var children = jest.fn();
    var event = {};
    var component = (0, _enzyme.shallow)(_react.default.createElement(_index.StatefulContainer, props, children));
    expect(children).toHaveBeenCalledTimes(1); // onClick is passed to children function

    var childrenOnClickProp = children.mock.calls[0][0].onClick;
    expect(childrenOnClickProp).toBe(component.instance().onClick);
    expect(childrenOnClickProp).not.toBe(props.onClick); // user's onClick handler is called

    childrenOnClickProp(event);
    expect(props.onClick).toHaveBeenCalledTimes(1);
    expect(props.onClick).toHaveBeenLastCalledWith(event);
  });
  test('stateReducer', function () {
    var props = {
      stateReducer: jest.fn()
    };
    var children = jest.fn();
    var component = (0, _enzyme.shallow)(_react.default.createElement(_index.StatefulContainer, props, children));
    props.stateReducer.mockReturnValueOnce({
      prop: true
    });
    component.instance().onClick();
    expect(props.stateReducer).toHaveBeenCalledTimes(1);
    expect(props.stateReducer).toHaveBeenLastCalledWith(_index.STATE_CHANGE_TYPE.click, {
      prop: false
    }, {
      prop: true
    });
    expect(component).toHaveState('prop', true);
  });
  test('null stateReducer', function () {
    var props = {
      stateReducer: null
    };
    var children = jest.fn();
    var component = (0, _enzyme.shallow)( // $FlowFixMe - Allow null stateReducer for the sake of testing
    _react.default.createElement(_index.StatefulContainer, props, children)); // null state reducer shouldn't break component

    component.instance().onClick();
    expect(component).toHaveState('prop', false);
  });
});