"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.
*/

/* global document */
test('StatefulContainer - basic render', function () {
  var props = {
    initialState: {
      value: 'initial value'
    },
    onChange: jest.fn(),
    stateReducer: jest.fn(),
    value: 'value prop'
  };
  var children = jest.fn();
  var component = (0, _enzyme.shallow)(_react.default.createElement(_index.StatefulContainer, props, children));
  expect(children).toHaveBeenCalledTimes(1);
  expect(children.mock.calls[0]).toMatchSnapshot('function-as-child called with correct args'); // state.value passed to children as a prop
  // $FlowFixMe

  var childrenValueProp = children.mock.calls[0][0].value;
  expect(childrenValueProp).toBe(component.instance().state.value);
  expect(childrenValueProp).not.toBe(props.value);
});
test('StatefulContainer - children function receives onChange handler', function () {
  var input = document.createElement('input');
  input.type = 'text';
  var event = {
    target: input
  };
  var props = {
    onChange: jest.fn(),
    stateReducer: jest.fn()
  };
  var children = jest.fn();
  var component = (0, _enzyme.shallow)(_react.default.createElement(_index.StatefulContainer, props, children));
  expect(children).toHaveBeenCalledTimes(1); // onChange is passed to children function
  // $FlowFixMe

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

  childrenOnChangeProp(event);
  expect(props.onChange).toHaveBeenCalledTimes(1);
  expect(props.onChange).toHaveBeenLastCalledWith(event);
});
test('StatefulContainer - stateReducer', function () {
  var input = document.createElement('input');
  input.type = 'text';
  input.value = 'input value';
  var event = {
    target: input
  };
  var props = {
    stateReducer: jest.fn(),
    initialState: {
      value: 'initial value'
    }
  };
  var children = jest.fn();
  var component = (0, _enzyme.shallow)(_react.default.createElement(_index.StatefulContainer, props, children)); // onChange event happens

  props.stateReducer.mockReturnValueOnce({
    value: 'new value'
  });
  component.instance().onChange(event);
  expect(props.stateReducer).toHaveBeenCalledTimes(1);
  expect(props.stateReducer).toHaveBeenLastCalledWith(_index.STATE_CHANGE_TYPE.change, {
    value: 'input value'
  }, {
    value: 'initial value'
  });
  expect(component).toHaveState('value', 'new value');
});
test('StatefulContainer - no stateReducer passed in', function () {
  var input = document.createElement('input');
  input.type = 'text';
  input.value = 'input value';
  var event = {
    target: input
  };
  var props = {
    initialState: {
      value: 'initial value'
    }
  };
  var children = jest.fn();
  var component = (0, _enzyme.shallow)(_react.default.createElement(_index.StatefulContainer, props, children));
  component.instance().onChange(event);
  expect(component).toHaveState('value', 'input value');
});