/* eslint-disable no-unused-expressions */
import React from 'react';
import sinon from 'sinon';
import PropTypes from 'prop-types';
import { expect } from 'chai';
import { describe, it } from 'mocha';
import { shallow } from 'enzyme';

import VirtualizedRendererWrap from '../../../javascripts/components/VirtualizedRendererWrap';

const defaultProps = {
  focusedOption: { label: 'focusedOn', value: 'focusedOn' },
  focusOption: sinon.spy(),
  key: 'key1',
  option: { label: 'option1', value: 'option1' },
  selectValue: sinon.spy(),
  valueArray: [],
};

function TestOption({ option }) {
  return (
    <span>{option.label}</span>
  );
}
TestOption.propTypes = {
  option: PropTypes.object.isRequired,
};

const defaultRenderer = opt => <TestOption option={opt} />;
const RendererWrap = VirtualizedRendererWrap(defaultRenderer);

describe('VirtualizedRendererWrap', () => {
  let wrapper;
  let props;
  beforeEach(() => {
    wrapper = shallow(<RendererWrap {...defaultProps} />);
    props = Object.assign({}, defaultProps);
  });

  it('uses the provided renderer', () => {
    const option = wrapper.find(TestOption);
    expect(option).to.have.lengthOf(1);
  });

  it('renders nothing when no option is provided', () => {
    props.option = null;
    wrapper = shallow(<RendererWrap {...props} />);
    const option = wrapper.find(TestOption);
    expect(option).to.have.lengthOf(0);
  });

  it('renders unfocused, unselected options with the default class', () => {
    const optionDiv = wrapper.find('div');
    expect(optionDiv).to.have.lengthOf(1);
    expect(optionDiv.props().className).to.equal('VirtualizedSelectOption');
  });

  it('renders focused option with the correct class', () => {
    props.option = props.focusedOption;
    wrapper = shallow(<RendererWrap {...props} />);
    const optionDiv = wrapper.find('div');
    expect(optionDiv.props().className).to.equal(
      'VirtualizedSelectOption VirtualizedSelectFocusedOption',
    );
  });

  it('renders disabled option with the correct class', () => {
    props.option.disabled = true;
    wrapper = shallow(<RendererWrap {...props} />);
    const optionDiv = wrapper.find('div');
    expect(optionDiv.props().className).to.equal(
      'VirtualizedSelectOption VirtualizedSelectDisabledOption',
    );
    props.option.disabled = false;
  });

  it('renders selected option with the correct class', () => {
    props.valueArray = [props.option, props.focusedOption];
    wrapper = shallow(<RendererWrap {...props} />);
    const optionDiv = wrapper.find('div');
    expect(optionDiv.props().className).to.equal(
      'VirtualizedSelectOption VirtualizedSelectSelectedOption',
    );
  });

  it('renders options with custom classes', () => {
    props.option.className = 'CustomClass';
    wrapper = shallow(<RendererWrap {...props} />);
    const optionDiv = wrapper.find('div');
    expect(optionDiv.props().className).to.equal(
      'VirtualizedSelectOption CustomClass',
    );
  });

  it('calls focusedOption on its own option onMouseEnter', () => {
    const optionDiv = wrapper.find('div');
    optionDiv.simulate('mouseEnter');
    expect(props.focusOption.calledWith(props.option)).to.be.true;
  });

  it('calls selectValue on its own option onClick', () => {
    const optionDiv = wrapper.find('div');
    optionDiv.simulate('click');
    expect(props.selectValue.calledWith(props.option)).to.be.true;
  });
});
