ReactEmptyComponent-test.jsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import PropTypes from '../../lib/ReactPropTypes';
  2. import ReactTestUtils from "../../lib/ReactTestUtils";
  3. import React from '../../src/May';
  4. import { render, unmountComponentAtNode, findDOMNode } from '../../src/may-dom/MayDom';
  5. import {shallowCompare} from '../../src/PureComponent';
  6. var ReactDOM = {
  7. render: render,
  8. unmountComponentAtNode: unmountComponentAtNode,
  9. findDOMNode: findDOMNode
  10. }
  11. React.render = render;
  12. // import React from "../../dist/ReactANU";
  13. // var ReactDOM = React;
  14. // var ReactTestUtils = {
  15. // renderIntoDocument: function (element) {
  16. // var div = document.createElement("div");
  17. // return React.render(element, div);
  18. // }
  19. // };
  20. //https://github.com/facebook/react/blob/master/src/isomorphic/children/__tests__/ReactChildren-test.js
  21. describe("ReactComponent", function() {
  22. // this.timeout(200000);
  23. it("should not produce child DOM nodes for null and false", function() {
  24. class Component1 extends React.Component {
  25. render() {
  26. return null;
  27. }
  28. }
  29. class Component2 extends React.Component {
  30. render() {
  31. return false;
  32. }
  33. }
  34. var container1 = document.createElement("div");
  35. ReactDOM.render(<Component1 />, container1);
  36. expect(container1.children.length).toBe(0);
  37. var container2 = document.createElement("div");
  38. ReactDOM.render(<Component2 />, container2);
  39. expect(container2.children.length).toBe(0);
  40. });
  41. it("works when switching components", () => {
  42. var assertions = 0;
  43. class Inner extends React.Component {
  44. render() {
  45. return <span />;
  46. }
  47. componentDidMount() {
  48. // Make sure the DOM node resolves properly even if we're replacing a
  49. // `null` component
  50. expect(ReactDOM.findDOMNode(this)).not.toBe(null);
  51. assertions++;
  52. }
  53. componentWillUnmount() {
  54. // Even though we're getting replaced by `null`, we haven't been
  55. // replaced yet!
  56. expect(ReactDOM.findDOMNode(this)).not.toBe(null);
  57. assertions++;
  58. }
  59. }
  60. class Wrapper extends React.Component {
  61. render() {
  62. return this.props.showInner ? <Inner /> : null;
  63. }
  64. }
  65. var el = document.createElement("div");
  66. var component;
  67. // Render the <Inner /> component...
  68. component = ReactDOM.render(<Wrapper showInner={true} />, el);
  69. expect(ReactDOM.findDOMNode(component)).not.toBe(null);
  70. // Switch to null...
  71. component = ReactDOM.render(<Wrapper showInner={false} />, el);
  72. // expect(ReactDOM.findDOMNode(component).nodeName).toBe("#comment");
  73. // ...then switch back.
  74. component = ReactDOM.render(<Wrapper showInner={true} />, el);
  75. expect(ReactDOM.findDOMNode(component)).not.toBe(null);
  76. expect(assertions).toBe(3);
  77. });
  78. it("preserves the dom node during updates", () => {
  79. class Empty extends React.Component {
  80. render() {
  81. return null;
  82. }
  83. }
  84. var container = document.createElement("div");
  85. ReactDOM.render(<Empty />, container);
  86. var noscript1 = container.firstChild;
  87. expect(noscript1.nodeName).toBe("#comment");
  88. // This update shouldn't create a DOM node
  89. ReactDOM.render(<Empty />, container);
  90. var noscript2 = container.firstChild;
  91. expect(noscript2.nodeName).toBe("#comment");
  92. expect(noscript1).toBe(noscript2);
  93. });
  94. });