import React from 'react';
import { mount } from 'enzyme';
import karmadataTheme from '../../themes/karmadata.scss';
import { FilterItem } from 'karmadata';
import { ANY } 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('Any Option', () => {
    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);
    });
  });
  describe('Events', () => {
    it('onChange', () => {
      const customProps = { ...props, mainSelector: mainSelectorOptions };
      const wrapper = mount(
        <KdSelectEntity
          kdFilterModel={kdFilterModel}
          theme={karmadataTheme}
          {...customProps}
        />);
      expect(events.onChange.mock.calls.lentgh).toBe(initialValues.length);
    });
  });
});
