<commit_before>/* @flow */

import React from 'react';
import ReactDOM from 'react-dom';
import TestUtils from 'react-dom/test-utils';
import FloatAnchor from 'react-float-anchor';
import {MenuButton, Dropdown, MenuList, MenuItem} from '../src';

afterEach(() => {
  delete window.addEventListener;
  delete window.removeEventListener;
});

xtest('opens and closes on click', () => {
  const mountPoint = document.createElement('div');
  const root: MenuButton = (ReactDOM.render(
    <MenuButton
      menu={
        <Dropdown>
          <MenuList>
            <MenuItem>A</MenuItem>
            <MenuItem>B</MenuItem>
          </MenuList>
        </Dropdown>
      }
    >
      foo button
    </MenuButton>,
    mountPoint
  ): any);

  root.reposition(); // just check this doesn't throw

  const button = TestUtils.findRenderedDOMComponentWithTag(root, 'button');
  const floatAnchor: FloatAnchor = TestUtils.findRenderedComponentWithType(root, FloatAnchor);

  expect(TestUtils.scryRenderedComponentsWithType((floatAnchor:any).portal, MenuItem).length).toBe(0);

  TestUtils.Simulate.mouseDown(button, {button: 0});

  const menuListItems = TestUtils.scryRenderedComponentsWithType((floatAnchor:any).portal, MenuItem);

  expect(menuListItems.map(c=>c.props.children)).toEqual(
    ['A', 'B']
  );

  root.reposition(); // just check this doesn't throw

  TestUtils.Simulate.mouseDown(button, {button: 0});

  expect(TestUtils.scryRenderedComponentsWithType((floatAnchor:any).portal, MenuItem).length).toBe(0);

  ReactDOM.unmountComponentAtNode(mountPoint);
});

xtest('closes on outside click', () => {
  window.addEventListener = jest.fn(window.addEventListener);
  window.removeEventListener = jest.fn(window.removeEventListener);

  const mountPoint = document.createElement('div');
  const root: MenuButton = (ReactDOM.render(
    <MenuButton
      menu={
        <Dropdown>
          <MenuList>
            <MenuItem>A</MenuItem>
            <MenuItem>B</MenuItem>
          </MenuList>
        </Dropdown>
      }
    >
      foo button
    </MenuButton>,
    mountPoint
  ): any);

  const button = TestUtils.findRenderedDOMComponentWithTag(root, 'button');
  const floatAnchor: FloatAnchor = TestUtils.findRenderedComponentWithType(root, FloatAnchor);

  expect(TestUtils.scryRenderedComponentsWithType((floatAnchor:any).portal, MenuItem).length).toBe(0);
  expect(window.addEventListener.mock.calls.filter(c => c[0] === 'click').length).toBe(0);

  TestUtils.Simulate.mouseDown(button, {button: 0});
  TestUtils.Simulate.focus(button);

  expect(window.addEventListener.mock.calls.filter(c => c[0] === 'click').length).toBe(1);
  expect(window.addEventListener.mock.calls.filter(c => c[0] === 'click')[0][2]).toBe(true);

  expect(window.removeEventListener.mock.calls.filter(c => c[0] === 'click').length).toBe(0);

  const menuListItems = TestUtils.scryRenderedComponentsWithType((floatAnchor:any).portal, MenuItem);

  expect(menuListItems.map(c=>c.props.children)).toEqual(
    ['A', 'B']
  );

  // Simulate a click on the page
  window.addEventListener.mock.calls.filter(c => c[0] === 'click')[0][1].call(window, {
    target: 'window'
  });

  expect(TestUtils.scryRenderedComponentsWithType((floatAnchor:any).portal, MenuItem).length).toBe(0);

  expect(window.addEventListener.mock.calls.filter(c => c[0] === 'click').length).toBe(1);
  expect(window.removeEventListener.mock.calls.filter(c => c[0] === 'click').length).toBe(1);

  ReactDOM.unmountComponentAtNode(mountPoint);
});<commit_msg>Fix up MenuButton test.<commit_after>/* @flow */

import React from 'react';
import ReactDOM from 'react-dom';
import TestUtils from 'react-dom/test-utils';
import FloatAnchor from 'react-float-anchor';
import {MenuButton, Dropdown, MenuList, MenuItem} from '../src';

afterEach(() => {
  delete window.addEventListener;
  delete window.removeEventListener;
});

test('opens and closes on click', () => {
  const mountPoint = document.createElement('div');
  const root: MenuButton = (ReactDOM.render(
    <MenuButton
      menu={
        <Dropdown>
          <MenuList>
            <MenuItem>A</MenuItem>
            <MenuItem>B</MenuItem>
          </MenuList>
        </Dropdown>
      }
    >
      foo button
    </MenuButton>,
    mountPoint
  ): any);

  root.reposition(); // just check this doesn't throw

  const button = TestUtils.findRenderedDOMComponentWithTag(root, 'button');
  expect(TestUtils.scryRenderedComponentsWithType(root, Dropdown).length).toBe(0);

  TestUtils.Simulate.mouseDown(button, {button: 0});

  const dropdown: Dropdown = TestUtils.findRenderedComponentWithType(root, Dropdown);
  const menuListItems = TestUtils.scryRenderedComponentsWithType(dropdown, MenuItem);

  expect(menuListItems.map(c=>c.props.children)).toEqual(
    ['A', 'B']
  );

  root.reposition(); // just check this doesn't throw

  TestUtils.Simulate.mouseDown(button, {button: 0});

  expect(TestUtils.scryRenderedComponentsWithType(root, Dropdown).length).toBe(0);

  ReactDOM.unmountComponentAtNode(mountPoint);
});

test('closes on outside click', () => {
  window.addEventListener = jest.fn(window.addEventListener);
  window.removeEventListener = jest.fn(window.removeEventListener);

  const mountPoint = document.createElement('div');
  const root: MenuButton = (ReactDOM.render(
    <MenuButton
      menu={
        <Dropdown>
          <MenuList>
            <MenuItem>A</MenuItem>
            <MenuItem>B</MenuItem>
          </MenuList>
        </Dropdown>
      }
    >
      foo button
    </MenuButton>,
    mountPoint
  ): any);

  const button = TestUtils.findRenderedDOMComponentWithTag(root, 'button');

  expect(TestUtils.scryRenderedComponentsWithType(root, Dropdown).length).toBe(0);
  expect(window.addEventListener.mock.calls.filter(c => c[0] === 'click').length).toBe(0);

  TestUtils.Simulate.mouseDown(button, {button: 0});
  TestUtils.Simulate.focus(button);

  expect(window.addEventListener.mock.calls.filter(c => c[0] === 'click').length).toBe(1);
  expect(window.addEventListener.mock.calls.filter(c => c[0] === 'click')[0][2]).toBe(true);

  expect(window.removeEventListener.mock.calls.filter(c => c[0] === 'click').length).toBe(0);

  const dropdown: Dropdown = TestUtils.findRenderedComponentWithType(root, Dropdown);
  const menuListItems = TestUtils.scryRenderedComponentsWithType(dropdown, MenuItem);

  expect(menuListItems.map(c=>c.props.children)).toEqual(
    ['A', 'B']
  );

  // Simulate a click on the page
  window.addEventListener.mock.calls.filter(c => c[0] === 'click')[0][1].call(window, {
    target: 'window'
  });

  expect(TestUtils.scryRenderedComponentsWithType(root, Dropdown).length).toBe(0);

  expect(window.addEventListener.mock.calls.filter(c => c[0] === 'click').length).toBe(1);
  expect(window.removeEventListener.mock.calls.filter(c => c[0] === 'click').length).toBe(1);

  ReactDOM.unmountComponentAtNode(mountPoint);
});
{'fraction_non_alphanumeric': 0.15516992046276212, 'fraction_numerical': 0.006652205350686912, 'mean_word_length': 4.271341463414634, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3706663', 'n_tokens_mistral': 2140, 'n_tokens_neox': 2114, 'n_words': 355}