ReactDOM-test.jsx 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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/__tests__/EventPluginHub-test.js
  21. describe("ReactDOM", function () {
  22. // this.timeout(200000);
  23. it('allows a DOM element to be used with a string', () => {
  24. var element = React.createElement('div', { className: 'foo' });
  25. var instance = ReactTestUtils.renderIntoDocument(element);
  26. expect(ReactDOM.findDOMNode(instance).tagName).toBe('DIV');
  27. });
  28. it('should allow children to be passed as an argument', () => {
  29. var argDiv = ReactTestUtils.renderIntoDocument(
  30. React.createElement('div', null, 'child'),
  31. );
  32. var argNode = ReactDOM.findDOMNode(argDiv);
  33. expect(argNode.innerHTML).toBe('child');
  34. });
  35. it('should overwrite props.children with children argument', () => {
  36. var conflictDiv = ReactTestUtils.renderIntoDocument(
  37. React.createElement('div', { children: 'fakechild' }, 'child'),
  38. );
  39. var conflictNode = ReactDOM.findDOMNode(conflictDiv);
  40. expect(conflictNode.innerHTML).toBe('child');
  41. });
  42. /**
  43. * We need to make sure that updates occur to the actual node that's in the
  44. * DOM, instead of a stale cache.
  45. */
  46. it('should purge the DOM cache when removing nodes', () => {
  47. var myDiv = ReactTestUtils.renderIntoDocument(
  48. <div>
  49. <div key="theDog" className="dog" />,
  50. <div key="theBird" className="bird" />
  51. </div>,
  52. );
  53. // Warm the cache with theDog
  54. myDiv = ReactTestUtils.renderIntoDocument(
  55. <div>
  56. <div key="theDog" className="dogbeforedelete" />,
  57. <div key="theBird" className="bird" />,
  58. </div>,
  59. );
  60. // Remove theDog - this should purge the cache
  61. myDiv = ReactTestUtils.renderIntoDocument(
  62. <div>
  63. <div key="theBird" className="bird" />,
  64. </div>,
  65. );
  66. // Now, put theDog back. It's now a different DOM node.
  67. myDiv = ReactTestUtils.renderIntoDocument(
  68. <div>
  69. <div key="theDog" className="dog" />,
  70. <div key="theBird" className="bird" />,
  71. </div>,
  72. );
  73. // Change the className of theDog. It will use the same element
  74. myDiv = ReactTestUtils.renderIntoDocument(
  75. <div>
  76. <div key="theDog" className="bigdog" />,
  77. <div key="theBird" className="bird" />,
  78. </div>,
  79. );
  80. var root = ReactDOM.findDOMNode(myDiv);
  81. var dog = root.childNodes[0];
  82. expect(dog.className).toBe('bigdog');
  83. });
  84. })