ReactChildReconciler-test.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * Copyright (c) 2013-present, Facebook, Inc.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @emails react-core
  8. */
  9. // NOTE: We're explicitly not using JSX here. This is intended to test
  10. // the current stack addendum without having source location added by babel.
  11. 'use strict';
  12. import React from '../../src/May';
  13. import { render } from '../../src/may-dom/MayDom'
  14. var ReactDOM = {
  15. render: render
  16. }
  17. // import React from "../../dist/ReactANU";
  18. // var ReactDOM = {
  19. // render: React.render
  20. // }
  21. // var React = require('react');//hyphenate
  22. // var ReactDOM = require('react-dom');
  23. var ReactTestUtils = {};
  24. ReactTestUtils.renderIntoDocument = function (component) {
  25. var div = document.createElement('div');
  26. ReactDOM.render(component, div);
  27. }
  28. describe('ReactChildReconciler', () => {
  29. function normalizeCodeLocInfo(str) {
  30. return str && str.replace(/\(at .+?:\d+\)/g, '(at **)');
  31. }
  32. // beforeEach(() => {
  33. // // jest.resetModules();
  34. // React = require('react');
  35. // ReactTestUtils = require('react-dom/test-utils');
  36. // });
  37. function createIterable(array) {
  38. return {
  39. '@@iterator': function() {
  40. var i = 0;
  41. return {
  42. next() {
  43. const next = {
  44. value: i < array.length ? array[i] : undefined,
  45. done: i === array.length,
  46. };
  47. i++;
  48. return next;
  49. },
  50. };
  51. },
  52. };
  53. }
  54. it('warns for duplicated array keys', () => {
  55. spyOn(console, 'error');
  56. class Component extends React.Component {
  57. render() {
  58. return <div>{[<div key="1" />, <div key="1" />]}</div>;
  59. }
  60. }
  61. ReactTestUtils.renderIntoDocument(<Component />);
  62. });
  63. it('warns for duplicated array keys with component stack info', () => {
  64. spyOn(console, 'error');
  65. class Component extends React.Component {
  66. render() {
  67. return <p>{[<span key="1" />, <span key="1" />]}</p>;
  68. }
  69. }
  70. class Parent extends React.Component {
  71. componentWillMount(){
  72. console.log('Parent Will Mount');
  73. }
  74. componentDidMount(){
  75. console.log('Parent Did Mount');
  76. }
  77. render() {
  78. return React.cloneElement(this.props.child);
  79. }
  80. }
  81. class GrandParent extends React.Component {
  82. componentWillMount(){
  83. console.log('GrandParent Will Mount');
  84. }
  85. componentDidMount(){
  86. console.log('GrandParent Did Mount');
  87. }
  88. render() {
  89. return <Parent child={<Component />} />;
  90. }
  91. }
  92. ReactTestUtils.renderIntoDocument(<GrandParent />);
  93. });
  94. it('warns for duplicated iterable keys', () => {
  95. spyOn(console, 'error');
  96. class Component extends React.Component {
  97. render() {
  98. return <div>{createIterable([<div key="1" />, <div key="1" />])}</div>;
  99. }
  100. }
  101. ReactTestUtils.renderIntoDocument(<Component />);
  102. });
  103. it('warns for duplicated iterable keys with component stack info', () => {
  104. spyOn(console, 'error');
  105. class Component extends React.Component {
  106. render() {
  107. return <div>{createIterable([<div key="1" />, <div key="1" />])}</div>;
  108. }
  109. }
  110. class Parent extends React.Component {
  111. render() {
  112. return React.cloneElement(this.props.child);
  113. }
  114. }
  115. class GrandParent extends React.Component {
  116. render() {
  117. return <Parent child={<Component />} />;
  118. }
  119. }
  120. ReactTestUtils.renderIntoDocument(<GrandParent />);
  121. });
  122. });