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

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

/* eslint-env browser */
import React from 'react';
import { mount } from 'enzyme';
import { STATE_CHANGE_TYPE } from '../constants.js';
import StatefulContainer from '../stateful-container.js';
var mockChildrenFn = jest.fn().mockImplementation(function () {
  return React.createElement("div", null);
});
var mockOnPageChangeFn = jest.fn();
var mockStateReducerFn = jest.fn().mockImplementation(function (changeType, changes) {
  return changes;
});

function getSharedProps() {
  return {
    numPages: 7,
    children: mockChildrenFn,
    onPageChange: mockOnPageChangeFn,
    stateReducer: mockStateReducerFn
  };
}

describe('Pagination StatefulContainer', function () {
  beforeEach(function () {
    jest.clearAllMocks();
  });
  test('renders and passes required props to children function', function () {
    var component = mount(React.createElement(StatefulContainer, getSharedProps()));
    expect(mockChildrenFn.mock.calls[0][0]).toEqual({
      currentPage: 1,
      onPageChange: component.instance().onPageChange
    });
  });
  test('with initialState', function () {
    var props = _objectSpread({}, getSharedProps(), {
      initialState: {
        currentPage: 5
      }
    });

    var component = mount(React.createElement(StatefulContainer, props));
    expect(component.state('currentPage')).toBe(5);
  });
  test('onPageChange called if and only if page has changed', function () {
    var component = mount(React.createElement(StatefulContainer, getSharedProps()));
    var instance = component.instance();
    instance.onPageChange({
      nextPage: 5
    });
    expect(component.state('currentPage')).toBe(5);
    expect(mockOnPageChangeFn.mock.calls.length).toBe(1); // Invoke again with same page

    instance.onPageChange({
      nextPage: 5
    });
    expect(mockOnPageChangeFn.mock.calls.length).toBe(1);
  });
  test('internalSetState calls stateReducer with the correct args', function () {
    var component = mount(React.createElement(StatefulContainer, getSharedProps()));
    component.instance().internalSetState(STATE_CHANGE_TYPE.changePage, {
      currentPage: 2
    });
    expect(mockStateReducerFn.mock.calls[0][0]).toEqual(STATE_CHANGE_TYPE.changePage);
    expect(mockStateReducerFn.mock.calls[0][1]).toEqual({
      currentPage: 2
    });
  });
  test('internalSetState should still call setState with no stateReducer', function () {
    var props = _objectSpread({}, getSharedProps(), {
      stateReducer: null
    }); // $FlowFixMe


    var component = mount(React.createElement(StatefulContainer, props));
    component.instance().internalSetState(STATE_CHANGE_TYPE.changePage, {
      currentPage: 2
    });
    expect(component.state('currentPage')).toBe(2);
  });
});