refs-destruction-test.jsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // import {
  2. // beforeHook,
  3. // afterHook,
  4. // browser
  5. // } from "karma-event-driver-ext/cjs/event-driver-hooks";
  6. // import getTestDocument from "./getTestDocument";
  7. // import ReactTestUtils from "lib/ReactTestUtils";
  8. import ReactTestUtils from "../../lib/ReactTestUtils";
  9. import React from '../../src/May';
  10. import { render,unmountComponentAtNode } from '../../src/may-dom/MayDom'
  11. var ReactDOM = {
  12. render: render,
  13. unmountComponentAtNode:unmountComponentAtNode
  14. }
  15. React.render = render;
  16. // import React from "../../dist/ReactANU";
  17. // var ReactDOM=React;
  18. // var React = require('react');//hyphenate
  19. // var ReactDOM = require('react-dom');
  20. //https://github.com/facebook/react/blob/master/src/renderers/dom/test/__tests__/ReactTestUtils-test.js
  21. describe("refs-destruction", function() {
  22. // this.timeout(200000);
  23. // before(async () => {
  24. // await beforeHook();
  25. // });
  26. // after(async () => {
  27. // await afterHook(false);
  28. // });
  29. /**
  30. * Counts clicks and has a renders an item for each click. Each item rendered
  31. * has a ref of the form "clickLogN".
  32. */
  33. var TestComponent = class extends React.Component {
  34. render() {
  35. return (
  36. <div>
  37. {this.props.destroy
  38. ? null
  39. : <div ref="theInnerDiv">
  40. Lets try to destroy this.
  41. </div>}
  42. </div>
  43. );
  44. }
  45. };
  46. it('should remove refs when destroying the parent', () => {
  47. var container = document.createElement('div');
  48. var testInstance = ReactDOM.render(<TestComponent />, container);
  49. expect(ReactTestUtils.isDOMComponent(testInstance.refs.theInnerDiv)).toBe(
  50. true,
  51. );
  52. expect(
  53. Object.keys(testInstance.refs || {}).filter(key => testInstance.refs[key])
  54. .length,
  55. ).toEqual(1);
  56. ReactDOM.unmountComponentAtNode(container);
  57. expect(
  58. Object.keys(testInstance.refs || {}).filter(key => testInstance.refs[key])
  59. .length,
  60. ).toEqual(0);
  61. });
  62. it('should remove refs when destroying the child', () => {
  63. var container = document.createElement('div');
  64. var testInstance = ReactDOM.render(<TestComponent />, container);
  65. expect(testInstance.refs.theInnerDiv.tagName).toBe(
  66. "DIV",
  67. );
  68. expect(
  69. Object.keys(testInstance.refs || {}).filter(key => testInstance.refs[key])
  70. .length,
  71. ).toBe(1);
  72. ReactDOM.render(<TestComponent destroy={true} />, container);
  73. expect(
  74. Object.keys(testInstance.refs || {}).filter(key => testInstance.refs[key])
  75. .length,
  76. ).toBe(0);
  77. });
  78. it('should not error when destroying child with ref asynchronously', () => {
  79. class Modal extends React.Component {
  80. componentDidMount() {
  81. this.div = document.createElement('div');
  82. document.body.appendChild(this.div);
  83. this.componentDidUpdate();
  84. }
  85. componentDidUpdate() {
  86. ReactDOM.render(<div>{this.props.children}</div>, this.div);
  87. }
  88. componentWillUnmount() {
  89. var self = this;
  90. // some async animation
  91. setTimeout(function() {
  92. expect(function() {
  93. ReactDOM.unmountComponentAtNode(self.div);
  94. }).not.toThrow();
  95. document.body.removeChild(self.div);
  96. }, 0);
  97. }
  98. render() {
  99. return null;
  100. }
  101. }
  102. class AppModal extends React.Component {
  103. render() {
  104. return (
  105. <Modal>
  106. <a ref="ref" />
  107. </Modal>
  108. );
  109. }
  110. }
  111. class App extends React.Component {
  112. render() {
  113. return this.props.hidden ? null : <AppModal onClose={this.close} />;
  114. }
  115. }
  116. var container = document.createElement('div');
  117. ReactDOM.render(<App />, container);
  118. ReactDOM.render(<App hidden={true} />, container);
  119. });
  120. })