findDOMNode-test.jsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import ReactTestUtils from "../../lib/ReactTestUtils";
  2. import React from '../../src/May';
  3. import { render, unmountComponentAtNode,findDOMNode } from '../../src/may-dom/MayDom'
  4. var ReactDOM = {
  5. render: render,
  6. unmountComponentAtNode: unmountComponentAtNode,
  7. findDOMNode:findDOMNode
  8. }
  9. React.render = render;
  10. // https://github.com/facebook/react/blob/master/src/renderers/__tests__/EventPluginHub-test.js
  11. describe("findDOMNode", function() {
  12. // this.timeout(200000);
  13. it("findDOMNode should find dom element", () => {
  14. class MyNode extends React.Component {
  15. render() {
  16. return <div><span>Noise</span></div>;
  17. }
  18. }
  19. var myNode = ReactTestUtils.renderIntoDocument(<MyNode />);
  20. var myDiv = ReactDOM.findDOMNode(myNode);
  21. var mySameDiv = ReactDOM.findDOMNode(myDiv);
  22. expect(myDiv.tagName).toBe("DIV");
  23. expect(mySameDiv).toBe(myDiv);
  24. });
  25. it("findDOMNode should find dom element after an update from null", () => {
  26. function Bar({flag}) {
  27. if (flag) {
  28. return <span>A</span>;
  29. }
  30. return null;
  31. }
  32. class MyNode extends React.Component {
  33. render() {
  34. return <Bar flag={this.props.flag} />;
  35. }
  36. }
  37. var container = document.createElement("div");
  38. var myNodeA = ReactDOM.render(<MyNode />, container);
  39. var a = ReactDOM.findDOMNode(myNodeA);
  40. expect(a && a.nodeType).toBe(8);
  41. var myNodeB = ReactDOM.render(<MyNode flag={true} />, container);
  42. expect(myNodeA === myNodeB).toBe(true);
  43. var b = ReactDOM.findDOMNode(myNodeB);
  44. expect(b.tagName).toBe("SPAN");
  45. });
  46. });