<commit_before>import React from 'react';
import flatten from 'lodash/flatten';
import unique from 'lodash/uniq';
import compact from 'lodash/compact';
import cheerio from 'cheerio';

import ComplexSelector from './ComplexSelector';
import {
  nodeEqual,
  containsChildrenSubArray,
  propFromEvent,
  withSetStateAllowed,
  propsOfNode,
  typeOfNode,
  isReactElementAlike,
  displayNameOfNode,
  isFunctionalComponent,
  isCustomComponentElement,
  ITERATOR_SYMBOL,
} from './Utils';
import {
  debugNodes,
} from './Debug';
import {
  getTextFromNode,
  hasClassName,
  childrenOfNode,
  parentsOfNode,
  treeFilter,
  buildPredicate,
} from './ShallowTraversal';
import {
  createShallowRenderer,
  renderToStaticMarkup,
  batchedUpdates,
  isDOMComponentElement,
} from './react-compat';

/**
 * Finds all nodes in the current wrapper nodes' render trees that match the provided predicate
 * function.
 *
 * @param {ShallowWrapper} wrapper
 * @param {Function} predicate
 * @param {Function} filter
 * @returns {ShallowWrapper}
 */
function findWhereUnwrapped(wrapper, predicate, filter = treeFilter) {
  return wrapper.flatMap(n => filter(n.getNode(), predicate));
}

/**
 * Returns a new wrapper instance with only the nodes of the current wrapper instance that match
 * the provided predicate function.
 *
 * @param {ShallowWrapper} wrapper
 * @param {Function} predicate
 * @returns {ShallowWrapper}
 */
function filterWhereUnwrapped(wrapper, predicate) {
  return wrapper.wrap(compact(wrapper.getNodes().filter(predicate)));
}

/**
 * @class ShallowWrapper
 */
class ShallowWrapper {

  constructor(nodes, root, options = {}) {
    if (!root) {
      this.root = this;
      this.unrendered = nodes;
      this.renderer = createShallowRenderer();
      withSetStateAllowed(() => {
        batchedUpdates(() => {
          this.renderer.render(nodes, options.context);
          const instance = this.instance();
          if (
            options.lifecycleExperimental &&
            instance &&
            typeof instance.componentDidMount === 'function'
          ) {
            instance.componentDidMount();
          }
        });
      });
      this.length = 1;
    } else {
      this.root = root;
      this.unrendered = null;
      this.renderer = null;
      if (!Array.isArray(nodes)) {
        this.initialNode = nodes;
        this.initialNodes = [nodes];
      } else {
        this.initialNode = nodes[0];
        this.initialNodes = nodes;
      }
      this.length = this.initialNodes.length;
    }
    this.options = options;
    this.complexSelector = new ComplexSelector(buildPredicate, findWhereUnwrapped, childrenOfNode);
  }

  /**
   * Returns the wrapped ReactElement.
   *
   * @return {ReactElement}
   */
  getNode() {
    return this.initialNode || this.renderer.getRenderOutput();
  }

  /**
   * Returns the wrapped ReactElements.
   *
   * @return {Array<ReactElement>}
   */
  getNodes() {
    return this.initialNodes || [this.renderer.getRenderOutput()];
  }

  /**
   * Gets the instance of the component being rendered as the root node passed into `shallow()`.
   *
   * NOTE: can only be called on a wrapper instance that is also the root instance.
   *
   * Example:
   * ```
   * const wrapper = shallow(<MyComponent />);
   * const inst = wrapper.instance();
   * expect(inst).to.be.instanceOf(MyComponent);
   * ```
   * @returns {ReactComponent}
   */
  instance() {
    if (this.root !== this) {
      throw new Error('ShallowWrapper::instance() can only be called on the root');
    }
    return this.renderer._instance ? this.renderer._instance._instance : null;
  }

  /**
   * Forces a re-render. Useful to run before checking the render output if something external
   * may be updating the state of the component somewhere.
   *
   * NOTE: can only be called on a wrapper instance that is also the root instance.
   *
   * @returns {ShallowWrapper}
   */
  update() {
    if (this.root !== this) {
      throw new Error('ShallowWrapper::update() can only be called on the root');
    }
    this.single(() => {
      const instance = this.instance();
      if (instance) {
        instance.forceUpdate();
      }
    });
    return this;
  }

  /**
   * A method is for re-render with new props and context.
   * This calls componentDidUpdate method if lifecycleExperimental is enabled.<commit_msg>account for react 0.13 and node <= 12<commit_after>import React from 'react';
import flatten from 'lodash/flatten';
import unique from 'lodash/uniq';
import compact from 'lodash/compact';
import cheerio from 'cheerio';

import ComplexSelector from './ComplexSelector';
import {
  nodeEqual,
  containsChildrenSubArray,
  propFromEvent,
  withSetStateAllowed,
  propsOfNode,
  typeOfNode,
  isReactElementAlike,
  displayNameOfNode,
  isFunctionalComponent,
  isCustomComponentElement,
  ITERATOR_SYMBOL,
} from './Utils';
import {
  debugNodes,
} from './Debug';
import {
  getTextFromNode,
  hasClassName,
  childrenOfNode,
  parentsOfNode,
  treeFilter,
  buildPredicate,
} from './ShallowTraversal';
import {
  createShallowRenderer,
  renderToStaticMarkup,
  batchedUpdates,
  isDOMComponentElement,
} from './react-compat';

/**
 * Finds all nodes in the current wrapper nodes' render trees that match the provided predicate
 * function.
 *
 * @param {ShallowWrapper} wrapper
 * @param {Function} predicate
 * @param {Function} filter
 * @returns {ShallowWrapper}
 */
function findWhereUnwrapped(wrapper, predicate, filter = treeFilter) {
  return wrapper.flatMap(n => filter(n.getNode(), predicate));
}

/**
 * Returns a new wrapper instance with only the nodes of the current wrapper instance that match
 * the provided predicate function.
 *
 * @param {ShallowWrapper} wrapper
 * @param {Function} predicate
 * @returns {ShallowWrapper}
 */
function filterWhereUnwrapped(wrapper, predicate) {
  return wrapper.wrap(compact(wrapper.getNodes().filter(predicate)));
}

/**
 * @class ShallowWrapper
 */
class ShallowWrapper {

  constructor(nodes, root, options = {}) {
    if (!root) {
      this.root = this;
      this.unrendered = nodes;
      this.renderer = createShallowRenderer();
      withSetStateAllowed(() => {
        batchedUpdates(() => {
          this.renderer.render(nodes, options.context);
          const instance = this.instance();
          if (
            options.lifecycleExperimental &&
            instance &&
            typeof instance.componentDidMount === 'function'
          ) {
            instance.componentDidMount();
          }
        });
      });
      this.length = 1;
    } else {
      this.root = root;
      this.unrendered = null;
      this.renderer = null;
      if (!Array.isArray(nodes)) {
        this.initialNode = nodes;
        this.initialNodes = [nodes];
      } else {
        this.initialNode = nodes[0];
        this.initialNodes = nodes;
      }
      this.length = this.initialNodes.length;
    }
    this.options = options;
    this.complexSelector = new ComplexSelector(buildPredicate, findWhereUnwrapped, childrenOfNode);
  }

  /**
   * Returns the wrapped ReactElement.
   *
   * @return {ReactElement}
   */
  getNode() {
    return this.initialNode || this.renderer.getRenderOutput();
  }

  /**
   * Returns the wrapped ReactElements.
   *
   * @return {Array<ReactElement>}
   */
  getNodes() {
    return this.initialNodes || [this.renderer.getRenderOutput()];
  }

  /**
   * Gets the instance of the component being rendered as the root node passed into `shallow()`.
   *
   * NOTE: can only be called on a wrapper instance that is also the root instance.
   *
   * Example:
   * ```
   * const wrapper = shallow(<MyComponent />);
   * const inst = wrapper.instance();
   * expect(inst).to.be.instanceOf(MyComponent);
   * ```
   * @returns {ReactComponent}
   */
  instance() {
    if (this.root !== this) {
      throw new Error('ShallowWrapper::instance() can only be called on the root');
    }
    return this.renderer._instance ? this.renderer._instance._instance : null;
  }

  /**
   * Forces a re-render. Useful to run before checking the render output if something external
   * may be updating the state of the component somewhere.
   *
   * NOTE: can only be called on a wrapper instance that is also the root instance.
   *
   * @returns {ShallowWrapper}
   */
  update() {
    if (this.root !== this) {
      throw new Error('ShallowWrapper::update() can only be called on the root');
    }
    this.single(() => {
      const instance = this.instance();
      if (instance) {
        withSetStateAllowed(() => {
          instance.forceUpdate();
        });
      }
    });
    return this;
  }

  /**
   * A method is for re-render with new props and context.
   * This calls componentDidUpdate method if lifecycleExperimental is enabled.
{'fraction_non_alphanumeric': 0.11578707916287534, 'fraction_numerical': 0.0010236578707916288, 'mean_word_length': 2.987755102040816, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 16, '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': '7182320', 'n_tokens_mistral': 2535, 'n_tokens_neox': 2513, 'n_words': 694}