DOMPropertyOperations-test.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. 'use strict';
  10. import React from '../../src/May';
  11. import { render } from '../../src/may-dom/MayDom'
  12. var ReactDOM = {
  13. render: render
  14. }
  15. import ReactDOMServer from '../../src/may-server/MayServer'
  16. // var React = require('react');//hyphenate
  17. // var ReactDOM = require('react-dom');
  18. // import React from "../../dist/ReactANU";
  19. // var ReactDOM = {
  20. // render: React.render
  21. // }
  22. describe('DOMPropertyOperations', () => {
  23. // var React;
  24. // var ReactDOM;
  25. // beforeEach(() => {
  26. // // jest.resetModules();
  27. // React = require('react');
  28. // ReactDOM = require('react-dom');
  29. // });
  30. describe('setValueForProperty', () => {
  31. it('should set values as properties by default', () => {
  32. var container = document.createElement('div');
  33. ReactDOM.render(<div title="Tip!" />, container);
  34. expect(container.firstChild.title).toBe('Tip!');
  35. });
  36. it('should set values as attributes if necessary', () => {
  37. var container = document.createElement('div');
  38. ReactDOM.render(<div role="#" />, container);
  39. expect(container.firstChild.getAttribute('role')).toBe('#');
  40. expect(container.firstChild.role).toBeUndefined();
  41. });
  42. /*it('should set values as namespace attributes if necessary', () => {
  43. var container = document.createElement('svg');
  44. // var svg = <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  45. // <circle cx="100" cy="50" r="40" stroke="black" stroke-width="2" fill="red" />
  46. // </svg>;//
  47. var svg=<image xlinkHref="about:blank" />;
  48. ReactDOM.render(svg, container);
  49. expect(
  50. container.firstChild.getAttributeNS(
  51. 'http://www.w3.org/1999/xlink',
  52. 'href',
  53. ),
  54. ).toBe('about:blank');
  55. });*/
  56. it('should set values as boolean properties', () => {
  57. var container = document.createElement('div');
  58. ReactDOM.render(<div disabled="disabled" />, container);
  59. // expect(container.firstChild.getAttribute('disabled')).toBe('');
  60. ReactDOM.render(<div disabled={true} />, container);
  61. // expect(container.firstChild.getAttribute('disabled')).toBe('');
  62. ReactDOM.render(<div disabled={false} />, container);
  63. expect(container.firstChild.getAttribute('disabled')).toBe(null);
  64. ReactDOM.render(<div disabled={true} />, container);
  65. ReactDOM.render(<div disabled={null} />, container);
  66. expect(container.firstChild.getAttribute('disabled')).toBe(null);
  67. ReactDOM.render(<div disabled={true} />, container);
  68. ReactDOM.render(<div disabled={undefined} />, container);
  69. expect(container.firstChild.getAttribute('disabled')).toBe(null);
  70. });
  71. it('should convert attribute values to string first', () => {
  72. // Browsers default to this behavior, but some test environments do not.
  73. // This ensures that we have consistent behavior.
  74. var obj = {
  75. toString: function() {
  76. return 'css-class';
  77. },
  78. };
  79. var container = document.createElement('div');
  80. ReactDOM.render(<div className={obj} />, container);
  81. expect(container.firstChild.getAttribute('class')).toBe('css-class');
  82. });
  83. it('should not remove empty attributes for special properties', () => {
  84. var container = document.createElement('div');
  85. ReactDOM.render(<input value="" />, container);
  86. expect(container.firstChild.getAttribute('value')).toBe(null);
  87. expect(container.firstChild.value).toBe('');
  88. });
  89. it('should remove for falsey boolean properties', () => {
  90. var container = document.createElement('div');
  91. ReactDOM.render(<div allowFullScreen={false} />, container);
  92. expect(container.firstChild.hasAttribute('allowFullScreen')).toBe(false);
  93. });
  94. it('should remove when setting custom attr to null', () => {
  95. var container = document.createElement('div');
  96. ReactDOM.render(<div data-foo="bar" />, container);
  97. expect(container.firstChild.hasAttribute('data-foo')).toBe(true);
  98. ReactDOM.render(<div data-foo={null} />, container);
  99. expect(container.firstChild.hasAttribute('data-foo')).toBe(false);
  100. });
  101. it('should set className to empty string instead of null', () => {
  102. var container = document.createElement('div');
  103. ReactDOM.render(<div className="selected" />, container);
  104. expect(container.firstChild.className).toBe('selected');
  105. ReactDOM.render(<div className={null} />, container);
  106. // className should be '', not 'null' or null (which becomes 'null' in
  107. // some browsers)
  108. expect(container.firstChild.className).toBe('');
  109. expect(container.firstChild.getAttribute('class')).toBe(null);
  110. });
  111. it('should remove property properly for boolean properties', () => {
  112. var container = document.createElement('div');
  113. ReactDOM.render(<div hidden={true} />, container);
  114. expect(container.firstChild.hasAttribute('hidden')).toBe(true);
  115. ReactDOM.render(<div hidden={false} />, container);
  116. //hidden是property
  117. //expect(container.firstChild.hasAttribute('hidden')).toBe(false);
  118. });
  119. });
  120. describe('value mutation method', function() {
  121. it('should update an empty attribute to zero', function() {
  122. var container = document.createElement('div');
  123. ReactDOM.render(
  124. <input type="radio" value="" onChange={function() {}} />,
  125. container,
  126. );
  127. spyOn(container.firstChild, 'setAttribute');
  128. ReactDOM.render(
  129. <input type="radio" value={0} onChange={function() {}} />,
  130. container,
  131. );
  132. //之前为toBe(1) 不过input的value为property直接赋值最好
  133. //不需要setAttribute
  134. expect(container.firstChild.setAttribute.calls.count()).toBe(0);
  135. });
  136. it('should always assign the value attribute for non-inputs', function() {
  137. var container = document.createElement('div');
  138. ReactDOM.render(<progress />, container);
  139. spyOn(container.firstChild, 'setAttribute');
  140. ReactDOM.render(<progress value={30} />, container);
  141. ReactDOM.render(<progress value="30" />, container);
  142. //同上
  143. expect(container.firstChild.setAttribute.calls.count()).toBe(0);
  144. });
  145. });
  146. describe('deleteValueForProperty', () => {
  147. it('should remove attributes for normal properties', () => {
  148. var container = document.createElement('div');
  149. ReactDOM.render(<div title="foo" />, container);
  150. expect(container.firstChild.getAttribute('title')).toBe('foo');
  151. ReactDOM.render(<div />, container);
  152. //title 是div的property
  153. expect(container.firstChild.getAttribute('title')).toBe('');
  154. });
  155. it('should not remove attributes for special properties', () => {
  156. var container = document.createElement('div');
  157. spyOn(console, 'warn');
  158. ReactDOM.render(
  159. <input type="text" value="foo" onChange={function() {}} />,
  160. container,
  161. );
  162. // expect(container.firstChild.getAttribute('value')).toBe('foo');
  163. expect(container.firstChild.value).toBe('foo');
  164. ReactDOM.render(
  165. <input type="text" onChange={function() {}} />,
  166. container,
  167. );
  168. // expect(container.firstChild.getAttribute('value')).toBe('foo');
  169. expect(container.firstChild.value).toBe('foo');
  170. // expect(console.warn.calls.count()).toBe(1);
  171. // expect(console.error.calls.argsFor(0)[0]).toContain(
  172. // 'A component is changing a controlled input of type text to be uncontrolled',
  173. // );
  174. });
  175. });
  176. });