<commit_before>import React from 'react';
import { mount } from 'enzyme';

const RestrictFocusToChildren = require('../RestrictFocusToChildren');

class TestComponent extends React.Component {
  state = {
    showContent: false
  }

  render () {
    return (
      <div>
        <button
          id='open-button'
          onClick={e => {
            //Enzyme simulate click was not focusing the button
            e.target.focus();
            this.setState({ showContent: true });
          }}
        >
          Open Content
        </button>
        {this.state.showContent ? (
          <RestrictFocusToChildren>
            <div>
              <button id='close-button' onClick={() => this.setState({ showContent: false })}>
                Close Content
              </button>
            </div>
          </RestrictFocusToChildren>
        ) : null}
      </div>
    );
  }
}

describe('RestrictFocusToChildren', () => {
  let openButton;
  let wrapper;

  beforeEach(() => {
    wrapper = mount(<TestComponent />, { attachTo: window.document });
    openButton = wrapper.find('#open-button').first();

    openButton.simulate('click');
  });

  afterEach(() => {
    wrapper.unmount();
  });

  it('should remember the last focused node', () => {
    expect(wrapper.find(RestrictFocusToChildren).first().instance().previousFocusedNode)
      .toEqual(openButton.instance());
  });

  it('should return focus to the last focused node on unmount', () => {
    const closeButton = wrapper.find('#close-button').first();

    closeButton.simulate('click');

    expect(window.document.activeElement).toEqual(openButton.instance());
  });

  it('should hide content in parent that is focusable when child is rendered', () => {
    const buttonHTML = openButton.html();

    expect(buttonHTML).toContain('tabindex="-1"');
    expect(buttonHTML).toContain('aria-hidden="true"');
  });

  it('should unhide content in parent that is focusable when child is removed', () => {
    const closeButton = wrapper.find('#close-button').first();

    closeButton.simulate('click');

    const openButtonHTML = openButton.html();

    expect(openButtonHTML).not.toContain('tabindex');
    expect(openButtonHTML).not.toContain('aria-hidden');
  });
});<commit_msg>Add test for preserving tabindex<commit_after>import React from 'react';
import { mount } from 'enzyme';

const RestrictFocusToChildren = require('../RestrictFocusToChildren');

class TestComponent extends React.Component {
  state = {
    showContent: false
  }

  render () {
    return (
      <div>
        <button
          id='open-button'
          onClick={e => {
            //Enzyme simulate click was not focusing the button
            e.target.focus();
            this.setState({ showContent: true });
          }}
        >
          Open Content
        </button>
        <div id='focusable-div' tabIndex={2}>Focusable Div</div>
        {this.state.showContent ? (
          <RestrictFocusToChildren>
            <div>
              <button id='close-button' onClick={() => this.setState({ showContent: false })}>
                Close Content
              </button>
            </div>
          </RestrictFocusToChildren>
        ) : null}
      </div>
    );
  }
}

describe('RestrictFocusToChildren', () => {
  let openButton;
  let wrapper;

  beforeEach(() => {
    wrapper = mount(<TestComponent />, { attachTo: window.document });
    openButton = wrapper.find('#open-button').first();

    openButton.simulate('click');
  });

  afterEach(() => {
    wrapper.unmount();
  });

  it('should remember the last focused node', () => {
    expect(wrapper.find(RestrictFocusToChildren).first().instance().previousFocusedNode)
      .toEqual(openButton.instance());
  });

  it('should return focus to the last focused node on unmount', () => {
    const closeButton = wrapper.find('#close-button').first();

    closeButton.simulate('click');

    expect(window.document.activeElement).toEqual(openButton.instance());
  });

  it('should hide content in parent that is focusable when child is rendered', () => {
    const buttonHTML = openButton.html();

    expect(buttonHTML).toContain('tabindex="-1"');
    expect(buttonHTML).toContain('aria-hidden="true"');
  });

  it('should unhide content in parent that is focusable when child is removed', () => {
    const closeButton = wrapper.find('#close-button').first();

    closeButton.simulate('click');

    const openButtonHTML = openButton.html();

    expect(openButtonHTML).not.toContain('tabindex');
    expect(openButtonHTML).not.toContain('aria-hidden');
  });

  it('should preserve tabindex on unmount', () => {
    const focusableDiv = wrapper.find('#focusable-div').first();
    const closeButton = wrapper.find('#close-button').first();

    expect(focusableDiv.html()).toContain('tabindex="-1"');
    closeButton.simulate('click');
    expect(focusableDiv.html()).toContain('tabindex="2"');
  });
});
{'fraction_non_alphanumeric': 0.15513754045307443, 'fraction_numerical': 0.0010113268608414239, 'mean_word_length': 2.9029202841357535, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 46, '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': '6957523', 'n_tokens_mistral': 1403, 'n_tokens_neox': 1371, 'n_words': 284}