ReactStatelessComponent-test.jsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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/renderers/dom/test/__tests__/ReactTestUtils-test.js
  21. describe("ReactStatelessComponent", function () {
  22. // this.timeout(200000);
  23. // before(async () => {
  24. // await beforeHook();
  25. // });
  26. // after(async () => {
  27. // await afterHook(false);
  28. // });
  29. function StatelessComponent(props) {
  30. return <div>{props.name}</div>;
  31. }
  32. it('should render stateless component', () => {
  33. var el = document.createElement('div');
  34. ReactDOM.render(<StatelessComponent name="A" />, el);
  35. expect(el.textContent).toBe('A');
  36. });
  37. it('should update stateless component', () => {
  38. class Parent extends React.Component {
  39. render() {
  40. return <StatelessComponent {...this.props} />;
  41. }
  42. }
  43. var el = document.createElement('div');
  44. ReactDOM.render(<Parent name="A" />, el);
  45. expect(el.textContent).toBe('A');
  46. ReactDOM.render(<Parent name="B" />, el);
  47. expect(el.textContent).toBe('B');
  48. });
  49. it('should unmount stateless component', () => {
  50. var container = document.createElement('div');
  51. ReactDOM.render(<StatelessComponent name="A" />, container);
  52. expect(container.textContent).toBe('A');
  53. ReactDOM.unmountComponentAtNode(container);
  54. expect(container.textContent).toBe('');
  55. });
  56. it('should pass context thru stateless component', () => {
  57. class Child extends React.Component {
  58. static contextTypes = {
  59. test: PropTypes.string,
  60. };
  61. render() {
  62. return <div>{this.context.test}</div>;
  63. }
  64. }
  65. function Parent() {
  66. return <Child />;
  67. }
  68. class GrandParent extends React.Component {
  69. static childContextTypes = {
  70. test: PropTypes.string,
  71. };
  72. getChildContext() {
  73. return { test: this.props.test };
  74. }
  75. render() {
  76. return <Parent />;
  77. }
  78. }
  79. var el = document.createElement('div');
  80. ReactDOM.render(<GrandParent test="test" />, el);
  81. expect(el.textContent).toBe('test');
  82. ReactDOM.render(<GrandParent test="mest" />, el);
  83. expect(el.textContent).toBe('mest');
  84. });
  85. it('should use correct name in key warning', () => {
  86. function Child() {
  87. return <div>{[<span>3</span>]}</div>;
  88. }
  89. var s = ReactTestUtils.renderIntoDocument(<Child />);
  90. expect(s.textContent).toBe("3")
  91. });
  92. it('should support default props and prop types', () => {
  93. function Child(props) {
  94. return <div>{props.test}</div>;
  95. }
  96. Child.defaultProps = {test: 2};
  97. Child.propTypes = {test: PropTypes.string};
  98. spyOn(console, 'error');
  99. var s = ReactTestUtils.renderIntoDocument(<Child />);
  100. expect(s.textContent).toBe("2")
  101. });
  102. it('should receive context', () => {
  103. class Parent extends React.Component {
  104. static childContextTypes = {
  105. lang: PropTypes.string,
  106. };
  107. getChildContext() {
  108. return {lang: 'en'};
  109. }
  110. render() {
  111. return <Child />;
  112. }
  113. }
  114. function Child(props, context) {
  115. return <div>{context.lang}</div>;
  116. }
  117. Child.contextTypes = {lang:PropTypes.string};
  118. var el = document.createElement('div');
  119. ReactDOM.render(<Parent />, el);
  120. expect(el.textContent).toBe('en');
  121. });
  122. it('should work with arrow functions', () => {
  123. var Child = function() {
  124. return <div />;
  125. };
  126. // Will create a new bound function without a prototype, much like a native
  127. // arrow function.
  128. Child = Child.bind(this);
  129. expect(() => ReactTestUtils.renderIntoDocument(<Child />)).not.toThrow();
  130. });
  131. it('should allow simple functions to return null', () => {
  132. var Child = function() {
  133. return null;
  134. };
  135. expect(() => ReactTestUtils.renderIntoDocument(<Child />)).not.toThrow();
  136. });
  137. it('should allow simple functions to return false', () => {
  138. function Child() {
  139. return false;
  140. }
  141. expect(() => ReactTestUtils.renderIntoDocument(<Child />)).not.toThrow();
  142. });
  143. })