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/isomorphic/children/__tests__/ReactChildren-test.js
var ReactDOM = window.ReactDOM || React;

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

    it("should allow key property to express identity", () => {
        var node;
        var Component = props => (
            <div ref={c => (node = c)}>
                <div key={props.swap ? "banana" : "apple"} />
                <div key={props.swap ? "apple" : "banana"} />
            </div>
        );

        var container = document.createElement("div");
        ReactDOM.render(<Component />, container);
        var origChildren = Array.from(node.childNodes);
        ReactDOM.render(<Component swap={true} />, container);
        var newChildren = Array.from(node.childNodes);
        expect(origChildren[0]).toBe(newChildren[1]);
        expect(origChildren[1]).toBe(newChildren[0]);
    });

    it("should use composite identity", () => {
        class Wrapper extends React.Component {
            render() {
                return <a>{this.props.children}</a>;
            }
        }

        var container = document.createElement("div");
        var node1;
        var node2;
        ReactDOM.render(
            <Wrapper key="wrap1"><span ref={c => (node1 = c)} /></Wrapper>,
            container
        );
        ReactDOM.render(
            <Wrapper key="wrap2"><span ref={c => (node2 = c)} /></Wrapper>,
            container
        );

        expect(node1).not.toBe(node2);
    });


    function renderAComponentWithKeyIntoContainer(key, container) {
        class Wrapper extends React.Component {
            render() {
                return <div><span ref="span" key={key} /></div>;
            }
        }

        var instance = ReactDOM.render(<Wrapper />, container);
        var span = instance.refs.span;
        expect(ReactDOM.findDOMNode(span)).not.toBe(null);
    }

    it("should allow any character as a key, in a detached parent", () => {
        var detachedContainer = document.createElement("div");
        renderAComponentWithKeyIntoContainer("<'WEIRD/&\\key'>", detachedContainer);
    });

    it("should allow any character as a key, in an attached parent", () => {
    // This test exists to protect against implementation details that
    // incorrectly query escaped IDs using DOM tools like getElementById.
        var attachedContainer = document.createElement("div");
        document.body.appendChild(attachedContainer);

        renderAComponentWithKeyIntoContainer("<'WEIRD/&\\key'>", attachedContainer);

        document.body.removeChild(attachedContainer);
    });
});