import React from 'react';
import { act } from 'react-dom/test-utils';
import { render, fireEvent } from '../../../tests/utils';
import Select from '..';
import Icon from '../../icon';
import focusTest from '../../../tests/shared/focusTest';
import mountTest from '../../../tests/shared/mountTest';
import rtlTest from '../../../tests/shared/rtlTest';

const { Option } = Select;

describe('Select', () => {
  focusTest(Select, { refFocus: true });
  mountTest(Select);
  rtlTest(Select);

  function toggleOpen(container) {
    act(() => {
      fireEvent.mouseDown(container.querySelector('.ant-select-selector'));
      jest.runAllTimers();
    });
  }

  beforeEach(() => {
    jest.useFakeTimers();
  });

  afterEach(() => {
    jest.useRealTimers();
  });

  it('should have default notFoundContent', () => {
    const { container } = render(<Select mode="multiple" />);
    toggleOpen(container);
    expect(container.querySelectorAll('.ant-select-item-option').length).toBe(0);
    expect(container.querySelectorAll('.ant-empty').length).toBeTruthy();
  });

  it('should support set notFoundContent to null', () => {
    const { container } = render(<Select mode="multiple" notFoundContent={null} />);
    toggleOpen(container);
    expect(container.querySelectorAll('.ant-empty').length).toBe(0);
  });

  it('should not have default notFoundContent when mode is combobox', () => {
    const { container } = render(<Select mode={Select.SECRET_COMBOBOX_MODE_DO_NOT_USE} />);
    toggleOpen(container);
    expect(container.querySelector('.ant-empty')).toBeFalsy();
  });

  it('should not have notFoundContent when mode is combobox and notFoundContent is set', () => {
    const { container } = render(
      <Select mode={Select.SECRET_COMBOBOX_MODE_DO_NOT_USE} notFoundContent="not at all" />,
    );
    toggleOpen(container);
    expect(container.querySelector('.ant-select-item-option')).toBeFalsy();
    expect(container.querySelector('.ant-select-item-empty')).toHaveTextContent('not at all');
  });

  it('should be controlled by open prop', () => {
    const onDropdownVisibleChange = jest.fn();
    const { container, rerender } = render(
      <Select open onDropdownVisibleChange={onDropdownVisibleChange}>
        <Option value="1">1</Option>
      </Select>,
    );
    expect(container.querySelectorAll('.ant-select-dropdown').length).toBe(1);
    toggleOpen(container);
    expect(onDropdownVisibleChange).toHaveBeenLastCalledWith(false);
    expect(container.querySelectorAll('.ant-select-dropdown').length).toBe(1);

    rerender(
      <Select open={false} onDropdownVisibleChange={onDropdownVisibleChange}>
        <Option value="1">1</Option>
      </Select>,
    );
    expect(container.querySelectorAll('.ant-select-dropdown').length).toBe(1); // FIXME

    toggleOpen(container);
    expect(onDropdownVisibleChange).toHaveBeenLastCalledWith(true);
    expect(container.querySelectorAll('.ant-select-dropdown').length).toBe(1);
  });

  it('should show search icon when showSearch and open', () => {
    jest.useFakeTimers();
    const { container } = render(
      <Select showSearch>
        <Option value="1">1</Option>
      </Select>,
    );
    expect(container.querySelectorAll('.anticon-down').length).toBe(1);
    expect(container.querySelectorAll('.anticon-search').length).toBe(0);
    toggleOpen(container);
    expect(container.querySelectorAll('.anticon-down').length).toBe(0);
    expect(container.querySelectorAll('.anticon-search').length).toBe(1);
  });
  //
  describe('Select Custom Icons', () => {
    it('should support customized icons', () => {
      const { rerender, asFragment } = render(
        <Select
          removeIcon={<Icon type="close" />}
          clearIcon={<Icon type="close" />}
          menuItemSelectedIcon={<Icon type="close" />}
        >
          <Option value="1">1</Option>
        </Select>,
      );
      rerender(
        <Select
          count={10}
          removeIcon={<Icon type="close" />}
          clearIcon={<Icon type="close" />}
          menuItemSelectedIcon={<Icon type="close" />}
        >
          <Option value="1">1</Option>
        </Select>,
      );
      jest.runAllTimers();
      expect(asFragment().firstChild).toMatchSnapshot();
    });
  });

  describe('Deprecated', () => {
    it('should ignore mode="combobox"', () => {
      const { asFragment } = render(
        <Select mode="combobox">
          <Option value="1">1</Option>
        </Select>,
      );
      expect(asFragment().firstChild).toMatchSnapshot();
    });
  });
});

@@ -2122,352 +2122,167 @@
 nst 
-%7B container, rerender %7D = render(
+TestComponent = () =%3E %7B
 %0A      
-%3CSelec
+cons
 t 
+%5B
 open
- onDropdownVisibleChange=%7BonDropdownVisibleChange%7D%3E%0A        %3COption value=%221%22%3E1%3C/Option%3E%0A      %3C/Select%3E,%0A    );%0A    expect(container.querySelectorAll('.ant-select-dropdown').length).toBe(1);%0A    toggleOpen(container);%0A    expect(onDropdownVisibleChange).toHaveBeenLastCalledWith(fals
+, setOpen%5D = React.useState(false);%0A      const handleChange = value =%3E %7B%0A        onDropdownVisibleChange(valu
 e);%0A    
 expe
@@ -2281,104 +2281,58 @@
     
-expect(container.querySelectorAll('.ant-select-dropdown').length).toBe(1);%0A%0A    rerender
+    setOpen(value);%0A      %7D;%0A      return 
 (%0A      
 %3CSel
@@ -2319,32 +2319,34 @@
  return (%0A      
+  
 %3CSelect open=%7Bfa
@@ -2343,21 +2343,20 @@
 t open=%7B
-false
+open
 %7D onDrop
@@ -2374,31 +2374,20 @@
 Change=%7B
-onDropdownVisib
+hand
 leChange
@@ -2389,16 +2389,18 @@
 hange%7D%3E%0A
+  
         
@@ -2426,32 +2426,34 @@
 %3C/Option%3E%0A      
+  
 %3C/Select%3E,%0A    )
@@ -2437,38 +2437,99 @@
        %3C/Select%3E
-,
 %0A    
+  );%0A    %7D;%0A    const %7B container %7D = render(%3CTestComponent /%3E
 );%0A    expect(co
@@ -2541,35 +2541,32 @@
 er.querySelector
-All
 ('.ant-select-dr
@@ -2577,35 +2577,22 @@
 wn')
-.length).toBe(1); // FIXME%0A
+).toBeFalsy();
 %0A   
@@ -2630,141 +2630,141 @@
 ect(
-onDropdownVisibleChange).toHaveBeenLastCalledWith(true);%0A    expect(container.querySelectorAll('.ant-select-dropdown').length).toBe(1
+container.querySelectorAll('.ant-select-dropdown').length).toBe(1);%0A    expect(onDropdownVisibleChange).toHaveBeenLastCalledWith(true
 );%0A 

improve Select test case (#35633)