import React from "dist/React";
import getTestDocument from "./getTestDocument";
import ReactTestUtils from "lib/ReactTestUtils";
import ReactDOMServer from "dist/ReactDOMServer";
// https://github.com/facebook/react/blob/master/src/renderers/__tests__/EventPluginHub-test.js
var ReactDOM = window.ReactDOM || React;

describe("ReactElementClone", function() {
    this.timeout(200000);

    // NOTE: We're explicitly not using JSX here. This is intended to test
    // classic JS without JSX.
    var ComponentClass = class extends React.Component {
        render() {
            return React.createElement("div");
        }
    };


    it("should clone a DOM component with new props", () => {
        class Grandparent extends React.Component {
            render() {
                return <Parent child={<div className="child" />} />;
            }
        }
        class Parent extends React.Component {
            render() {
                return (
                    <div className="parent">
                        {React.cloneElement(this.props.child, {className: "xyz"})}
                    </div>
                );
            }
        }
        var component = ReactTestUtils.renderIntoDocument(<Grandparent />);
        expect(ReactDOM.findDOMNode(component).childNodes[0].className).toBe("xyz");
    });

    it("should clone a composite component with new props", () => {
        class Child extends React.Component {
            render() {
                return <div className={this.props.className} />;
            }
        }
        class Grandparent extends React.Component {
            render() {
                return <Parent child={<Child className="child" />} />;
            }
        }
        class Parent extends React.Component {
            render() {
                return (
                    <div className="parent">
                        {React.cloneElement(this.props.child, {className: "xyz"})}
                    </div>
                );
            }
        }
        var component = ReactTestUtils.renderIntoDocument(<Grandparent />);
        expect(ReactDOM.findDOMNode(component).childNodes[0].className).toBe("xyz");
    });

    it("does not fail if config has no prototype", () => {
        var config = Object.create(null, {foo: {value: 1, enumerable: true}});
        React.cloneElement(<div />, config);
    });

    it("should keep the original ref if it is not overridden", () => {
        class Grandparent extends React.Component {
            render() {
                return <Parent child={<div ref="yolo" />} />;
            }
        }

        class Parent extends React.Component {
            render() {
                return (
                    <div>
                        {React.cloneElement(this.props.child, {className: "xyz"})}
                    </div>
                );
            }
        }

        var component = ReactTestUtils.renderIntoDocument(<Grandparent />);
        expect(component.refs.yolo.tagName).toBe("DIV");
    });

    it('should transfer the key property', () => {
    class Component extends React.Component {
      render() {
        return null;
      }
    }
    var clone = React.cloneElement(<Component />, {key: 'xyz'});
    expect(clone.key).toBe('xyz');
  });

  it('should transfer children', () => {
    class Component extends React.Component {
      render() {
        expect(this.props.children).toBe('xyz');
        return <div />;
      }
    }

    ReactTestUtils.renderIntoDocument(
      React.cloneElement(<Component />, {children: 'xyz'}),
    );
  });
  it('should shallow clone children', () => {
    class Component extends React.Component {
      render() {
        expect(this.props.children).toBe('xyz');
        return <div />;
      }
    }

    ReactTestUtils.renderIntoDocument(
      React.cloneElement(<Component>xyz</Component>, {}),
    );
  });

  it('should accept children as rest arguments', () => {
    class Component extends React.Component {
      render() {
        return null;
      }
    }

    var clone = React.cloneElement(
      <Component>xyz</Component>,
      {children: <Component />},
      <div />,
      <span />,
    );

    expect(clone.props.children).toEqual([<div />, <span />]);
  });
  it('should override children if undefined is provided as an argument', () => {
    var element = React.createElement(
      ComponentClass,
      {
        children: 'text',
      },
      undefined,
    );
    expect(element.props.children).toBe(undefined);

    var element2 = React.cloneElement(
      React.createElement(ComponentClass, {
        children: 'text',
      }),
      {},
      undefined,
    );
    expect(element2.props.children).toBe(undefined);
  });

});