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 StatefulContainer from '../stateful-container.js';
import { KEY_STRINGS, STATE_CHANGE_TYPES } from '../constants.js';
import { scrollItemIntoView } from '../utils.js';
jest.mock('../utils');
var mockItems = [{
  label: 'item1'
}, {
  disabled: true,
  label: 'item2'
}];
var mockChildrenFn = jest.fn().mockImplementation(function () {
  return React.createElement("div", null);
});
var mockItemSelect = jest.fn();
var originalAddEventListener = document.addEventListener;
var originalRemoveEventListener = document.removeEventListener;

function getSharedProps() {
  return {
    items: mockItems,
    onItemSelect: mockItemSelect,
    children: mockChildrenFn,
    stateReducer: jest.fn().mockImplementation(function (changeType, changes) {
      return changes;
    })
  };
}

describe('Menu StatefulContainer', function () {
  beforeAll(function () {
    // $FlowFixMe
    document.addEventListener = jest.fn(); // $FlowFixMe

    document.removeEventListener = jest.fn();
  });
  beforeEach(function () {
    jest.clearAllMocks();
  });
  afterAll(function () {
    // $FlowFixMe
    document.addEventListener = originalAddEventListener; // $FlowFixMe

    document.removeEventListener = originalRemoveEventListener;
  });
  test('renders and passes required props to children function', function () {
    var props = {
      items: mockItems,
      onItemSelect: mockItemSelect,
      children: mockChildrenFn
    };
    var component = mount(React.createElement(StatefulContainer, props));
    expect(component.instance().refList).toEqual([]);
    expect(component.instance().rootRef).toEqual(React.createRef());
    expect(mockChildrenFn.mock.calls[0][0]).toEqual({
      highlightedIndex: -1,
      items: mockItems,
      rootRef: React.createRef(),
      getRequiredItemProps: component.instance().getRequiredItemProps
    });
  });
  test('with initialState', function () {
    var props = _objectSpread({}, getSharedProps(), {
      initialState: {
        highlightedIndex: 5
      }
    });

    var component = mount(React.createElement(StatefulContainer, props));
    expect(component.state('highlightedIndex')).toBe(5);
  });
  test('add and remove event listeners', function () {
    var component = mount(React.createElement(StatefulContainer, getSharedProps()));
    expect(document.addEventListener.mock.calls.length).toBe(1);
    component.unmount();
    expect(document.removeEventListener.mock.calls.length).toBe(1);
  });
  test('getRequiredItemProps returns correct props', function () {
    var component = mount(React.createElement(StatefulContainer, getSharedProps()));
    var item = mockItems[0];
    var props = component.instance().getRequiredItemProps(item, 0);
    expect(props).toEqual({
      ref: React.createRef(),
      isHighlighted: false,
      onClick: props.onClick,
      'aria-activedescendant': false
    });
    props.onClick();
    expect(mockItemSelect.mock.calls[0][0]).toEqual({
      item: item
    });
  });
  test('getRequiredItemProps returns correct props for disabled item', function () {
    var component = mount(React.createElement(StatefulContainer, getSharedProps()));
    var item = mockItems[1];
    var props = component.instance().getRequiredItemProps(item, 1);
    expect(props).toEqual({
      disabled: true,
      ref: React.createRef(),
      isHighlighted: false,
      'aria-activedescendant': false
    });
  });
  test('getRequiredItemProps returns correct props for active child', function () {
    var component = mount(React.createElement(StatefulContainer, getSharedProps()));
    component.setState({
      highlightedIndex: 0
    });
    var item = mockItems[0];
    var props = component.instance().getRequiredItemProps(item, 0);
    expect(props).toEqual({
      ref: React.createRef(),
      isHighlighted: true,
      onClick: props.onClick,
      'aria-activedescendant': true
    });
  });
  test('onKeyDown - handleArrowKey', function () {
    var props = getSharedProps();
    var component = mount(React.createElement(StatefulContainer, props));
    expect(component.state('highlightedIndex')).toEqual(-1);
    component.instance().refList = [React.createRef(), React.createRef()];
    component.instance().onKeyDown({
      key: KEY_STRINGS.ArrowUp,
      preventDefault: jest.fn(),
      stopPropagation: jest.fn()
    });
    expect(component.state('highlightedIndex')).toEqual(0);
    expect(props.stateReducer.mock.calls[0]).toEqual([STATE_CHANGE_TYPES.moveUp, {
      highlightedIndex: 0
    }, {
      highlightedIndex: -1
    }]); // $FlowFixMe

    expect(scrollItemIntoView.mock.calls[0][0]).toEqual({
      node: React.createRef(),
      parentNode: React.createRef(),
      isFirst: true,
      isLast: false
    });
    component.instance().onKeyDown({
      key: KEY_STRINGS.ArrowDown,
      preventDefault: jest.fn(),
      stopPropagation: jest.fn()
    });
    expect(component.state('highlightedIndex')).toEqual(1);
    expect(props.stateReducer.mock.calls[1]).toEqual([STATE_CHANGE_TYPES.moveDown, {
      highlightedIndex: 1
    }, {
      highlightedIndex: 0
    }]);
  });
  test('onKeyDown - handleEnterKey', function () {
    var props = getSharedProps();
    var component = mount(React.createElement(StatefulContainer, props));
    var event = {
      key: KEY_STRINGS.Enter,
      preventDefault: jest.fn()
    };
    component.instance().onKeyDown(event);
    expect(mockItemSelect.mock.calls.length).toBe(0);
    component.setState({
      highlightedIndex: 0
    });
    component.instance().onKeyDown(event);
    expect(mockItemSelect.mock.calls[0]).toEqual([{
      item: mockItems[0],
      event: event
    }]);
    component.setState({
      highlightedIndex: 1
    });
    component.instance().onKeyDown(event);
    expect(mockItemSelect.mock.calls.length).toBe(1);
  });
});