<commit_before>import {DropdownItem} from 'pui-react-dropdowns';

describe('DropdownItem', () => {
  let props = {
    className: 'test-item-class',
    id: 'test-item-id',
    style: {
      opacity: '1'
    }
  };

  afterEach(() => {
    ReactDOM.unmountComponentAtNode(root);
  });

  it('passes through header', () => {
    ReactDOM.render(
      <DropdownItem header>HeadText</DropdownItem>,
      root);
    expect('#root li.dropdown-header').toContainText('HeadText');
  });

  it('passes through divider', () => {
    ReactDOM.render(
      <DropdownItem divider />,
      root);
    expect('#root li.divider').toExist();
  });

  it('passes through className to the li ', () => {
    ReactDOM.render(
      <DropdownItem {...props}>Item</DropdownItem>,
      root);
    expect('#root li').toHaveClass(props.className);
  });

  it('passes through style to the li', () => {
    ReactDOM.render(
      <DropdownItem {...props}>Item</DropdownItem>,
      root);
    expect('#root li').toHaveCss(props.style);
  });

  describe('href', () => {
    it('passes through id and href to the anchor', () => {
      ReactDOM.render(
        <DropdownItem href='test' {...props}>Item</DropdownItem>,
        root);
      expect('#root li a#test-item-id').toExist();
      expect('#root li a#test-item-id').toHaveAttr('href', 'test');
    });

    describe('target', () => {
      it('passes through target to the anchor when provided', () => {
        ReactDOM.render(
          <DropdownItem href='test' target="_blank" {...props}>Item</DropdownItem>,
          root);
        expect('#root li a#test-item-id').toHaveAttr('target', '_blank');
      });

      it('does not pass through target to the anchor when provided', () => {
        ReactDOM.render(
          <DropdownItem href='test' {...props}>Item</DropdownItem>,
          root);
        expect('#root li a#test-item-id').not.toHaveAttr('target');
      });
    });
  });

  describe('onSelect handling', () => {
    let handleSelectSpy;
    describe('with href', () => {
      it('passes through onSelect with eventKey', () => {
        handleSelectSpy = jasmine.createSpy('handleSelect');
        const eventKey = '1';
        ReactDOM.render(
          <DropdownItem href="/whatever" onSelect={handleSelectSpy} eventKey={eventKey}>Item</DropdownItem>,
          root);

        $('#root li a').simulate('click');
        expect(handleSelectSpy).toHaveBeenCalled();
      });
    });

    describe('without href', () => {
      it('passes through onSelect with eventKey', () => {
        handleSelectSpy = jasmine.createSpy('handleSelect');
        const eventKey = '1';
        ReactDOM.render(
          <DropdownItem onSelect={handleSelectSpy} eventKey={eventKey}>Item</DropdownItem>,
          root);

        $('#root li').simulate('click');
        expect(handleSelectSpy).toHaveBeenCalled();
      });
    });

    describe('with disabled prop', () => {
      it('does not call onSelect handler', () => {
        handleSelectSpy = jasmine.createSpy('handleSelect');
        ReactDOM.render(
          <DropdownItem disabled href="/whatever" onSelect={handleSelectSpy}>Item</DropdownItem>,
          root);
        expect('#root li').toHaveClass('disabled');
        expect('#root li a').toHaveAttr('disabled');
        $('#root li a').simulate('click');
        expect(handleSelectSpy).not.toHaveBeenCalled();
      });
    });
  });

  describe('when href is not passed in as a prop', () => {
    describe('when an a tag is passed in as a child', () => {
      beforeEach(() => {
        ReactDOM.render(
          <DropdownItem><a href="custom">link</a></DropdownItem>,
          root);
      });

      it('renders the child link', () => {
        expect('#root li a').toHaveAttr('href', 'custom');
      });
    });

    it('does not render an anchor element', () => {
      ReactDOM.render(
        <DropdownItem>Item</DropdownItem>,
        root);
      expect('#root li a').not.toExist();
    });
  });
});<commit_msg>chore(dropdown item) Update tests to ReactTestUtils<commit_after>import '../spec_helper'
import {DropdownItem} from 'pui-react-dropdowns'
import ReactTestUtils from 'react-addons-test-utils'
import {findByClass, findByTag, clickOn} from '../spec_helper'

describe('DropdownItem', () => {
  let result
  const renderComponent = props => ReactTestUtils.renderIntoDocument(
    <DropdownItem {...props}>DropdownItem Text</DropdownItem>
  )

  it('passes through header', () => {
    result = renderComponent({header: true})
    expect(findByClass(result, 'dropdown-header')).toContainText('DropdownItem Text')
  })

  it('passes through divider', () => {
    result = renderComponent({divider: true})
    expect(findByClass(result, 'divider')).toBeDefined()
  })

  it('passes through className and style to the li ', () => {
    result = renderComponent({
      className: 'test-item-class',
      style: {opacity: '0.5'}
    })

    const listItem = findByTag(result, 'li')
    expect(listItem).toHaveClass('test-item-class')
    expect(listItem).toHaveCss({opacity: '0.5'})
  })

  it('passes through id to the anchor if an href is provided', () => {
    result = renderComponent({id: 'test-item-id'})

    let listItem = findByTag(result, 'li')
    expect(listItem).not.toHaveAttr('id')

    result = renderComponent({id: 'test-item-id', href: 'test'})

    listItem = findByTag(result, 'li')
    expect(listItem).not.toHaveAttr('id')
    expect(listItem.querySelector('a')).toHaveAttr('id', 'test-item-id')
  })

  it('passes through href and target to the anchor', () => {
    result = renderComponent({href: 'test', target: '_blank'})
    expect(findByTag(result, 'a')).toHaveAttr('href', 'test')
    expect(findByTag(result, 'a')).toHaveAttr('target', '_blank')
  })

  describe('onSelect handling', () => {
    let handleSelectSpy
    describe('with href', () => {
      it('passes through onSelect on anchor click', () => {
        handleSelectSpy = jasmine.createSpy('handleSelect')
        const eventKey = '1'
        result = renderComponent({href: 'test', onSelect: handleSelectSpy, eventKey})

        clickOn(findByTag(result, 'a'))
        expect(handleSelectSpy).toHaveBeenCalled()
      })
    })

    describe('without href', () => {
      it('passes through onSelect on list item click', () => {
        handleSelectSpy = jasmine.createSpy('handleSelect')
        const eventKey = '1'
        result = renderComponent({onSelect: handleSelectSpy, eventKey})

        clickOn(findByTag(result, 'li'))
        expect(handleSelectSpy).toHaveBeenCalled()
      })
    })

    describe('with disabled prop', () => {
      it('does not call onSelect handler', () => {
        handleSelectSpy = jasmine.createSpy('handleSelect')
        result = renderComponent({href: 'test', onSelect: handleSelectSpy, disabled: true})

        expect(findByTag(result, 'li')).toHaveClass('disabled')
        expect(findByTag(result, 'a')).toHaveAttr('disabled')

        clickOn(findByTag(result, 'a'))

        expect(handleSelectSpy).not.toHaveBeenCalled()
      })
    })
  })

  describe('when an a tag is passed in as a child', () => {
    beforeEach(() => {
      result = ReactTestUtils.renderIntoDocument(
        <DropdownItem><a href="custom">link</a></DropdownItem>
      )

      it('renders the child link', () => {
        const listItem = findByTag(result, 'li')
        expect(listItem.querySelector('a')).toHaveAttr('id', 'custom')
      })
    })
  })
})
{'fraction_non_alphanumeric': 0.17271874581267588, 'fraction_numerical': 0.0012059493501272946, 'mean_word_length': 3.0764609503003824, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 74, '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': '7057689', 'n_tokens_mistral': 2282, 'n_tokens_neox': 2268, 'n_words': 446}