refs-test.jsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import ReactTestUtils from "../../lib/ReactTestUtils";
  2. import React from '../../src/May';
  3. import { render, unmountComponentAtNode } from '../../src/may-dom/MayDom'
  4. var ReactDOM = {
  5. render: render,
  6. unmountComponentAtNode: unmountComponentAtNode
  7. }
  8. React.render = render;
  9. // import React from "../../dist/ReactANU";
  10. // var ReactDOM=React;
  11. // var React = require('react');//hyphenate
  12. // var ReactDOM = require('react-dom');
  13. //https://github.com/facebook/react/blob/master/src/renderers/dom/test/__tests__/ReactTestUtils-test.js
  14. // var ReactDOM = window.ReactDOM || React;
  15. describe("reactiverefs", function () {
  16. // this.timeout(200000);
  17. // before(async () => {
  18. // await beforeHook();
  19. // });
  20. // after(async () => {
  21. // await afterHook(false);
  22. // });
  23. var body = document.body,
  24. div;
  25. beforeEach(function () {
  26. div = document.createElement("div");
  27. body.appendChild(div);
  28. });
  29. afterEach(function () {
  30. body.removeChild(div);
  31. });
  32. /**
  33. * Counts clicks and has a renders an item for each click. Each item rendered
  34. * has a ref of the form "clickLogN".
  35. */
  36. class ClickCounter extends React.Component {
  37. state = { count: this.props.initialCount };
  38. triggerReset = () => {
  39. this.setState({ count: this.props.initialCount });
  40. };
  41. handleClick = () => {
  42. this.setState({ count: this.state.count + 1 });
  43. };
  44. render() {
  45. var children = [];
  46. var i;
  47. for (i = 0; i < this.state.count; i++) {
  48. children.push(
  49. <div
  50. className="clickLogDiv"
  51. key={'clickLog' + i}
  52. ref={'clickLog' + i}
  53. />,
  54. );
  55. }
  56. return (
  57. <span className="clickIncrementer" onClick={this.handleClick}>
  58. {children}
  59. </span>
  60. );
  61. }
  62. }
  63. /**
  64. * Only purpose is to test that refs are tracked even when applied to a
  65. * component that is injected down several layers. Ref systems are difficult to
  66. * build in such a way that ownership is maintained in an airtight manner.
  67. */
  68. class GeneralContainerComponent extends React.Component {
  69. render() {
  70. return <div>{this.props.children}</div>;
  71. }
  72. }
  73. /**
  74. * Notice how refs ownership is maintained even when injecting a component
  75. * into a different parent.
  76. */
  77. class TestRefsComponent extends React.Component {
  78. doReset = () => {
  79. this.refs.myCounter.triggerReset();
  80. };
  81. render() {
  82. return (
  83. <div>
  84. <div ref="resetDiv" onClick={this.doReset}>
  85. Reset Me By Clicking This.
  86. </div>
  87. <GeneralContainerComponent ref="myContainer">
  88. <ClickCounter ref="myCounter" initialCount={1} />
  89. </GeneralContainerComponent>
  90. </div>
  91. );
  92. }
  93. }
  94. /**
  95. * Render a TestRefsComponent and ensure that the main refs are wired up.
  96. */
  97. var renderTestRefsComponent = function () {
  98. var testRefsComponent = ReactDOM.render(
  99. <TestRefsComponent />,div
  100. );
  101. expect(testRefsComponent instanceof TestRefsComponent).toBe(true);
  102. var generalContainer = testRefsComponent.refs.myContainer;
  103. expect(generalContainer instanceof GeneralContainerComponent).toBe(true);
  104. var counter = testRefsComponent.refs.myCounter;
  105. expect(counter instanceof ClickCounter).toBe(true);
  106. return testRefsComponent;
  107. };
  108. var expectClickLogsLengthToBe = function (instance, length) {
  109. var clickLogs = ReactTestUtils.scryRenderedDOMComponentsWithClass(
  110. instance,
  111. 'clickLogDiv',
  112. );
  113. expect(clickLogs.length).toBe(length);
  114. expect(Object.keys(instance.refs.myCounter.refs).length).toBe(length);
  115. };
  116. it('Should increase refs with an increase in divs', () => {
  117. var testRefsComponent = renderTestRefsComponent();
  118. var clickIncrementer = ReactTestUtils.findRenderedDOMComponentWithClass(
  119. testRefsComponent,
  120. 'clickIncrementer',
  121. );
  122. expectClickLogsLengthToBe(testRefsComponent, 1);
  123. // // After clicking the reset, there should still only be one click log ref.
  124. // ReactTestUtils.Simulate.click(testRefsComponent.refs.resetDiv);
  125. // expectClickLogsLengthToBe(testRefsComponent, 1);
  126. // // Begin incrementing clicks (and therefore refs).
  127. // ReactTestUtils.Simulate.click(clickIncrementer);
  128. // expectClickLogsLengthToBe(testRefsComponent, 2);
  129. // ReactTestUtils.Simulate.click(clickIncrementer);
  130. // expectClickLogsLengthToBe(testRefsComponent, 3);
  131. // // Now reset again
  132. // ReactTestUtils.Simulate.click(testRefsComponent.refs.resetDiv);
  133. // expectClickLogsLengthToBe(testRefsComponent, 1);
  134. });
  135. describe('factory components', () => {
  136. it('Should correctly get the ref', () => {
  137. function Comp() {
  138. return <div ref="elemRef" />;
  139. }
  140. const inst = ReactTestUtils.renderIntoDocument(<Comp />);
  141. expect(inst.refs.elemRef.tagName).toBe('DIV');
  142. });
  143. });
  144. it('coerces numbers to strings', () => {
  145. class A extends React.Component {
  146. render() {
  147. return <div ref={1} />;
  148. }
  149. }
  150. const a = ReactTestUtils.renderIntoDocument(<A />);
  151. expect(a.refs[1].nodeName).toBe('DIV');
  152. });
  153. })