"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('StatefulListContainer', function () {
  test('basic render', function () {
    var props = {
      overrides: {
        Root: function CustomRoot() {
          return _react.default.createElement("span", null);
        }
      },
      initialState: {
        items: ['Item 1', 'Item 2']
      },
      onChange: jest.fn(),
      stateReducer: jest.fn()
    };
    var children = jest.fn();
    (0, _enzyme.shallow)(_react.default.createElement(_index.StatefulListContainer, props, children));
    expect(children).toHaveBeenCalledTimes(1);
  });
  test('children function receives onChange handler', function () {
    var props = {
      onChange: jest.fn(),
      stateReducer: jest.fn()
    };
    var children = jest.fn();
    var event = {
      newIndex: undefined,
      newState: [undefined],
      oldIndex: undefined
    };
    var component = (0, _enzyme.shallow)(_react.default.createElement(_index.StatefulListContainer, props, children));
    expect(children).toHaveBeenCalledTimes(1); // onChange is passed to children function

    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('stateReducer', function () {
    var props = {
      initialState: {
        items: ['Item 1', 'Item 2']
      },
      stateReducer: jest.fn()
    };
    var children = jest.fn();
    var component = (0, _enzyme.shallow)(_react.default.createElement(_index.StatefulListContainer, props, children));
    props.stateReducer.mockReturnValueOnce({
      items: ['Item 2', 'Item 1']
    });
    component.instance().onChange({
      oldIndex: 0,
      newIndex: 1
    });
    expect(props.stateReducer).toHaveBeenCalledTimes(1);
    expect(props.stateReducer).toHaveBeenLastCalledWith(_index.STATE_CHANGE_TYPE.change, {
      items: ['Item 2', 'Item 1']
    }, {
      items: ['Item 1', 'Item 2']
    });
    expect(component).toHaveState('items', ['Item 2', 'Item 1']);
  });
});