/**
 * TEAM: frontend_infra
 * @flow
 */
// TODO: Fix this eslint issue on next edit. This is an autogenerated comment.
// eslint-disable-next-line flexport/no-legacy-dependencies
import {mount} from "enzyme";

import {_test} from "../CustomDropdownButton";
import Button from "../button/Button";

const {CustomDropdownButton} = _test;

process.on("unhandledRejection", reason => {
  // eslint-disable-next-line no-console
  console.error(
    "[UnhandledPromiseRejection] This test generates exceptions due to incorrect use of enzyme and side effects.",
    reason
  );
});

function mountDropdown(propOverrides: {...} = {}) {
  const defaultProps = {
    button: <Button>Button</Button>,
  };

  // $FlowFixMe[cannot-spread-inexact] upgrade 0.121.1 -> 0.122.0
  const mergedProps = {
    ...defaultProps,
    ...propOverrides,
  };
  return mount(
    <CustomDropdownButton {...mergedProps}>
      <div title="content">content</div>
    </CustomDropdownButton>
  );
}

describe("Dropdown", () => {
  it("clicking on button opens toggle menu", () => {
    const dropdown = mountDropdown();
    expect(dropdown.find("div[title='content']").length).toEqual(0);
    dropdown.find("Button").simulate("click");
    expect(dropdown.find("div[title='content']").length).toEqual(1);
  });
  it("closes menu if disabled while open", () => {
    const dropdown = mountDropdown();
    expect(dropdown.find("div[title='content']").length).toEqual(0);
    dropdown.find("Button").simulate("click");
    expect(dropdown.find("div[title='content']").length).toEqual(1);
    dropdown.setProps({disabled: true});
    expect(dropdown.find("div[title='content']").length).toEqual(0);
  });
});
