/*
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.
*/
import React from 'react';
import { shallow } from 'enzyme';
import { StatefulContainer, STATE_CHANGE_TYPE } from '../index.js';
describe('StatefulComponentContainer', function () {
  test('basic render', function () {
    var props = {
      initialState: {
        value: new Date()
      },
      onSelect: jest.fn(),
      stateReducer: jest.fn()
    };
    var children = jest.fn();
    shallow(React.createElement(StatefulContainer, props, children));
    expect(children).toHaveBeenCalledTimes(1);
  });
  describe('Children function receives correct props', function () {
    var props = {
      initialState: {
        value: new Date(2019, 2, 10)
      },
      stateReducer: jest.fn(),
      onSelect: jest.fn(),
      onDayMouseOver: jest.fn(),
      onDayMouseLeave: jest.fn()
    };
    var children = jest.fn();
    var eArgs = {
      date: new Date(),
      event: {}
    };
    var component = shallow(React.createElement(StatefulContainer, props, children));
    var handlers = [['onDayMouseOver', false], ['onDayMouseLeave', false], ['onSelect', true]];
    test.each(handlers)('Event handlers', function (handler, replaced) {
      // event handler is passed to children function
      var childrenHandler = children.mock.calls[0][0][handler];

      if (replaced) {
        expect(childrenHandler).toBe(component.instance()[handler]);
        expect(childrenHandler).not.toBe(props[handler]);
      } else {
        expect(childrenHandler).toBe(props[handler]);
      } // event handler from props is called
      // $FlowFixMe


      childrenHandler(eArgs);
      expect(props[handler]).toHaveBeenCalledTimes(1);
      expect(props[handler]).toHaveBeenLastCalledWith(eArgs);
    });
  });
  test('stateReducer', function () {
    var props = {
      initialState: {
        value: new Date(2016, 2, 10)
      },
      stateReducer: jest.fn()
    };
    var children = jest.fn();
    var newDate = {
      date: new Date(2018, 2, 10)
    };
    var state = {
      value: newDate.date
    };
    var stateUpdated = {
      value: new Date(2019, 2, 10)
    };
    var component = shallow(React.createElement(StatefulContainer, props, children));
    props.stateReducer.mockReturnValueOnce(stateUpdated);
    component.instance().onSelect(newDate);
    expect(props.stateReducer).toHaveBeenCalledTimes(1);
    expect(props.stateReducer.mock.calls[0][0]).toEqual(STATE_CHANGE_TYPE.change);
    expect(props.stateReducer.mock.calls[0][1]).toEqual(state);
    expect(props.stateReducer.mock.calls[0][2]).toMatchObject({
      value: props.initialState.value
    });
    expect(component).toHaveState('value', stateUpdated.value);
  });
});