// @flow
import React from 'react';
import { mountWrappedComponent } from '../../../../../utils/tests';

import type { ContextMenuListItemProps } from './context-menu-list-item.proptypes';
import { ContextMenuListItem } from './index';

const EXAMPLE_ITEM = {
  id: 'remove',
  label: 'Label',
  onSelect: () => {},
  danger: true,
};

const DEFAULT_PROPS: ContextMenuListItemProps<typeof EXAMPLE_ITEM> = {
  item: EXAMPLE_ITEM,
  onSelect: EXAMPLE_ITEM.onSelect,
  closeMenu: () => {},
  colorName: 'white',
  danger: false,
  children: 'some_child',
};

describe('ContextMenuListItem', () => {
  it('passes valid props to child button', () => {
    const item = mountWrappedComponent(
      <ContextMenuListItem {...DEFAULT_PROPS}>
        <div>Example Child</div>
      </ContextMenuListItem>,
    );

    expect(item.find('button').props()).toEqual(
      expect.objectContaining({
        children: expect.anything(),
        className: expect.any(String),
        onClick: expect.any(Function),
        role: 'button',
        type: 'button',
      }),
    );
  });

  it('stops event propagation when clicked', () => {
    const handleStopPropagation = jest.fn();
    const item = mountWrappedComponent(
      <ContextMenuListItem {...DEFAULT_PROPS}>
        <div>Example Child</div>
      </ContextMenuListItem>,
    );

    item
      .find('button')
      .simulate('click', { stopPropagation: handleStopPropagation });

    expect(handleStopPropagation).toHaveBeenCalledTimes(1);
  });

  it('closes menu when clicked', () => {
    const handleCloseMenu = jest.fn();
    const item = mountWrappedComponent(
      <ContextMenuListItem {...DEFAULT_PROPS} closeMenu={handleCloseMenu}>
        <div>Example Child</div>
      </ContextMenuListItem>,
    );

    item.find('button').simulate('click');

    expect(handleCloseMenu).toHaveBeenCalledTimes(1);
  });

  it('selects item when clicked', () => {
    const handleOnSelect = jest.fn();
    const item = mountWrappedComponent(
      <ContextMenuListItem {...DEFAULT_PROPS} onSelect={handleOnSelect}>
        <div>Example Child</div>
      </ContextMenuListItem>,
    );

    item.find('button').simulate('click');

    expect(handleOnSelect).toHaveBeenCalledTimes(1);
    expect(handleOnSelect).toHaveBeenCalledWith(DEFAULT_PROPS.item);
  });
});
