import ReactTestUtils from "../../lib/ReactTestUtils"; import React from '../../src/May'; import { render } from '../../src/may-dom/MayDom' var ReactDOM = { render: render } React.render = render; // import React from "../../dist/ReactANU"; // var React = require('react');//hyphenate // var ReactDOM = require('react-dom'); //https://github.com/facebook/react/blob/master/src/isomorphic/children/__tests__/ReactChildren-test.js // var ReactDOM = window.ReactDOM || React; describe("ref", function () { // this.timeout(200000); var body = document.body, div; beforeEach(function () { div = document.createElement("div"); body.appendChild(div); }); afterEach(function () { body.removeChild(div); }); it("patchRef", function () { class App extends React.Component { constructor(props) { super(props); this.handleClick = this .handleClick .bind(this); } handleClick() { // Explicitly focus the text input using the raw DOM API. if (this.myTextInput !== null) { this .myTextInput .focus(); } } render() { return (
this.myTextInput = ref} />
); } } var s = ReactDOM.render(, div); var dom = s.refs.a; ReactTestUtils.Simulate.click(dom); expect(document.activeElement).toBe(s.myTextInput); expect(s.myTextInput).toBeDefined(); }); it("patchRef Component", function() { class App extends React.Component { render() { return
; } } var index = 1; class A extends React.Component { componentWillReceiveProps() { index = 0; this.forceUpdate(); } render() { return index ? 111 : 111; } } var s = ReactTestUtils.renderIntoDocument(); expect(s.refs.a instanceof A).toBe(true); }); it("没有组件的情况", function() { var index = 0; function ref(a) { index ++; expect(a.tagName).toBe("DIV"); } var s = ReactTestUtils.renderIntoDocument(
); expect(index).toBe(1); }); it("should invoke refs in Component.render()", function() { var i = 0; let outer = function (a) { expect(a).toBe(div.firstChild); i++; }; let inner = function (a) { expect(a).toBe(div.firstChild.firstChild); i++; }; class Foo extends React.Component { render() { return (
); } } ReactDOM.render(, div); expect(i).toBe(2); }); it("rener方法在存在其他组件,那么组件以innerHTML方式引用子节点,子节点有ref", function() { class App extends React.Component { constructor(props) { super(props); this.state = { tip: "g-up-tips", text: "xxxx" }; } render() { return
child
; } } class Child extends React.Component { constructor(props) { super(props); this.state = {}; } render() { return
{this.props.children}
; } } var s = ReactTestUtils.renderIntoDocument(); expect(Object.keys(s.refs).sort()).toEqual(["child", "inner", "parent"]); }); it("用户在构造器里生成虚拟DOM", function() { var a; class App extends React.Component { constructor(props) { super(props); this.sliderLeftJSX = this.renderSlider("btnLeft"); this.state = {}; } renderSlider(which = "btnLeft") { return ( { this[which] = dom; }} />); } componentDidMount() { a = !!this.btnLeft; } render() { return (
{this.sliderLeftJSX}
); } } var s = ReactTestUtils.renderIntoDocument(); expect(a).toBe(true); }); it("Stateless组件也会被执行", function() { var b; function App() { return (
StateLess
); } function refFn(a){ b = a; } ReactTestUtils.renderIntoDocument(); expect(b).toBe(null); }); it("ReactDOM.render中的元素也会被执行", function() { var b; function refFn(a){ b = a; } ReactTestUtils.renderIntoDocument(

); expect(b && b.tagName).toBe("H1"); }); it("带ref的组件被子组件cloneElement", function() { class Select extends React.Component { constructor(props) { super(props); this.state = { aaa: 1 }; } render() { return React.createElement(SelectTrigger, { ref: "trigger" }, React.createElement( "div", { ref: "root" }, "xxxx")); } } class SelectTrigger extends React.Component { constructor(props) { super(props); this.state = { aaa: 2 }; } render() { return React.createElement(Trigger, Object.assign({ title: "xxx" }, this.props), this.props.children); } } class Trigger extends React.Component { constructor(props) { super(props); this.state = { aaa: 2 }; } render() { var props = this.props; var children = props.children; var child = React.Children.only(children); return React.cloneElement(child, { className: "5555" }); } } var s = React.render(