import React from 'react';
import { mount } from 'enzyme';
import karmadataTheme from '../../themes/karmadata.scss';
import { FilterItem } from 'karmadata';
import { ANY, USER_SPECIFIED_OPTION } from '../../constants'
import renderer from 'react-test-renderer';

import KdSelectEntity from '../../components/KdSelectEntity';
import { chicklets, apiOptions, events, autoCompleteOptions, initialValues, mainSelectorOptions } from '../__utils';

const props = {
  mainSelector: mainSelectorOptions,
  selectedValuesInitial: initialValues,
  autoComplete: autoCompleteOptions,
  api: apiOptions,
  events,
  chicklets,
};


describe('KdSelectEntity', () => {
  let kdFilterModel;
  beforeEach(() => {
    kdFilterModel = FilterItem('KdDrugClassEPC', 'KdId', 'KdDrugClassEPC');
  });

  describe('Setup', () => {
    it('renders correctly', () => {
      const tree = renderer.create(
        <KdSelectEntity
          kdFilterModel={kdFilterModel}
          theme={karmadataTheme}
          {...props}
        />
      ).toJSON();
      expect(tree).toMatchSnapshot();
    });

    it('should set initialValues', () => {
      const wrapper = mount(
        <KdSelectEntity
          kdFilterModel={kdFilterModel}
          theme={karmadataTheme}
          {...props}
        />);
      expect(wrapper.node.getChicklets().length).toBe(kdFilterModel.getSelected().length);
    });
  });

  describe('mainSelector', () => {
    it('visible', () => {
      const customProps = { ...props, mainSelector: mainSelectorOptions };
      const wrapper = mount(
        <KdSelectEntity
          kdFilterModel={kdFilterModel}
          theme={karmadataTheme}
          {...customProps}
        />);
      expect(wrapper.find('select').length).toBe(1);
      expect(wrapper.find('input.o-kd-autocomplete-input').length).toBe(1);
    });

    it('not visible', () => {
      const mainSelector = { ...mainSelectorOptions, visible: false };
      const customProps = { ...props, mainSelector };
      const wrapper = mount(
        <KdSelectEntity
          kdFilterModel={kdFilterModel}
          theme={karmadataTheme}
          {...customProps}
        />);
      expect(wrapper.find('select').length).toBe(0);
      expect(wrapper.find('input.o-kd-autocomplete-input').length).toBe(1);
    });

    it('Any option visible', () => {
      const customProps = { ...props, mainSelector: mainSelectorOptions };
      const wrapper = mount(
        <KdSelectEntity
          kdFilterModel={kdFilterModel}
          theme={karmadataTheme}
          {...customProps}
        />);
      expect(wrapper.find(`option[value="${ANY}"]`).length).toBe(1);
    });

    it('User specified options visible', () => {
      const customProps = { ...props, mainSelector: mainSelectorOptions };
      const wrapper = mount(
        <KdSelectEntity
          kdFilterModel={kdFilterModel}
          theme={karmadataTheme}
          {...customProps}
        />);
      expect(wrapper.find(`option[value="${USER_SPECIFIED_OPTION}"]`).length).toBe(1);
    });

    it('Custom values visible', () => {
      const customProps = { ...props, mainSelector: mainSelectorOptions };
      const wrapper = mount(
        <KdSelectEntity
          kdFilterModel={kdFilterModel}
          theme={karmadataTheme}
          {...customProps}
        />);
      expect(wrapper.find('option[value="test"]').length).toBe(1);
    });
  });

  describe('Events', () => {
    it('Not generate on change with initials', () => {
      const onChange = jest.fn();
      const customProps = {
        ...props,
        mainSelector: mainSelectorOptions,
        events: { onChange },
      };
      mount(
        <KdSelectEntity
          kdFilterModel={kdFilterModel}
          theme={karmadataTheme}
          {...customProps}
        />
      );

      expect(onChange.mock.calls.length).toBeGreaterThanOrEqual(0);
    });

    it('onChange', () => {
      const onChange = jest.fn();
      const customProps = {
        ...props,
        mainSelector: mainSelectorOptions,
        selectedValuesInitial: [],
        events: { onChange },
      };
      const wrapper = mount(
        <KdSelectEntity
          kdFilterModel={kdFilterModel}
          theme={karmadataTheme}
          {...customProps}
        />
      );

      wrapper.find('select').simulate('change', { target: { value: 'test' } });
      expect(onChange.mock.calls.length).toBeGreaterThanOrEqual(1);
    });

    it('onSelect', () => {
      const onSelect = jest.fn();
      const customProps = {
        ...props,
        mainSelector: mainSelectorOptions,
        selectedValuesInitial: [],
        events: { onSelect },
      };
      const wrapper = mount(
        <KdSelectEntity
          kdFilterModel={kdFilterModel}
          theme={karmadataTheme}
          {...customProps}
        />
      );

      wrapper.find('select').simulate('change', { target: { value: 'test' } });
      expect(onSelect.mock.calls.length).toBeGreaterThanOrEqual(1);
    });
  });

  describe('behavior', () => {
    it('Be disabled', () => {
      const customProps = {
        ...props,
        mainSelector: mainSelectorOptions,
        isDisabled: true,
      };
      const wrapper = mount(
        <KdSelectEntity
          kdFilterModel={kdFilterModel}
          theme={karmadataTheme}
          {...customProps}
        />
      );

      expect(wrapper.find('.u-disabled').length).toBe(1);
    });

    it('Be active', () => {
      const customProps = {
        ...props,
        mainSelector: mainSelectorOptions,
        isDisabled: false,
      };
      const wrapper = mount(
        <KdSelectEntity
          kdFilterModel={kdFilterModel}
          theme={karmadataTheme}
          {...customProps}
        />
      );

      expect(wrapper.find('.u-disabled').length).toBe(0);
    });

    it('None', () => {
      const customProps = {
        ...props,
        mainSelector: mainSelectorOptions,
        selectedValuesInitial: [],
        selectBehavior: 'None',
      };
      const wrapper = mount(
        <KdSelectEntity
          kdFilterModel={kdFilterModel}
          theme={karmadataTheme}
          {...customProps}
        />
      );

      wrapper.find('select').simulate('change', { target: { value: 'test' } });
      expect(wrapper.find('.c-kd-selected-chicklets').length).toBe(0);
      expect(wrapper.node.getChicklets().length).toBe(1);

      wrapper.find('select').simulate('change', { target: { value: 'test2' } });
      expect(wrapper.find('.c-kd-selected-chicklets').length).toBe(0);
      expect(wrapper.node.getChicklets().length).toBe(1);
    });

    it('AddChicklet', () => {
      const customProps = {
        ...props,
        mainSelector: mainSelectorOptions,
        selectedValuesInitial: [],
        selectBehavior: 'AddChicklet',
      };
      const wrapper = mount(
        <KdSelectEntity
          kdFilterModel={kdFilterModel}
          theme={karmadataTheme}
          {...customProps}
        />
      );

      wrapper.find('select').simulate('change', { target: { value: 'test' } });
      expect(wrapper.find('.c-kd-selected-chicklets').length).toBe(1);
      expect(wrapper.node.getChicklets().length).toBe(1);

      wrapper.find('select').simulate('change', { target: { value: 'test2' } });
      expect(wrapper.find('.c-kd-selected-chicklets').length).toBe(1);
      expect(wrapper.find('.c-kd-selected-chicklets').children().length).toBe(2);
      expect(wrapper.node.getChicklets().length).toBe(2);
    });

    it('AddChickletSingle', () => {
      const customProps = {
        ...props,
        mainSelector: mainSelectorOptions,
        selectedValuesInitial: [],
        selectBehavior: 'AddChickletSingle',
      };
      const wrapper = mount(
        <KdSelectEntity
          kdFilterModel={kdFilterModel}
          theme={karmadataTheme}
          {...customProps}
        />
      );

      wrapper.find('select').simulate('change', { target: { value: 'test' } });
      expect(wrapper.find('.c-kd-selected-chicklets').length).toBe(1);
      expect(wrapper.node.getChicklets().length).toBe(1);

      wrapper.find('select').simulate('change', { target: { value: 'test2' } });
      expect(wrapper.find('.c-kd-selected-chicklets').length).toBe(1);
      expect(wrapper.find('.c-kd-selected-chicklets').children().length).toBe(1);
      expect(wrapper.node.getChicklets().length).toBe(1);
    });
  });
});
