event.spec.jsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 {
  10. dispatchEvent, SyntheticEvent, addEvent
  11. } from '../../src/event';
  12. // import React from "../../dist/ReactANU";
  13. // var ReactDOM = React;
  14. // var ReactTestUtils = { Simulate: {} };
  15. // "click,change,keyDown,keyUp,KeyPress,mouseDown,mouseUp,mouseMove".replace(/\w+/g, function (name) {
  16. // ReactTestUtils.Simulate[name] = function (node, opts) {
  17. // if (!node || node.nodeType !== 1) {
  18. // throw "第一个参数必须为元素节点";
  19. // }
  20. // var fakeNativeEvent = opts || {};
  21. // fakeNativeEvent.target = node;
  22. // fakeNativeEvent.simulated = true;
  23. // fakeNativeEvent.type = name.toLowerCase();
  24. // React.eventSystem.dispatchEvent(fakeNativeEvent, name.toLowerCase());
  25. // };
  26. // });
  27. describe("事件系统模块", function () {
  28. // this.timeout(200000);
  29. // before(async () => {
  30. // await beforeHook();
  31. // });
  32. // after(async () => {
  33. // await afterHook(false);
  34. // });
  35. var body = document.body,
  36. div;
  37. beforeEach(function () {
  38. div = document.createElement("div");
  39. body.appendChild(div);
  40. });
  41. afterEach(function () {
  42. body.removeChild(div);
  43. });
  44. it("事件与样式", async () => {
  45. class App extends React.Component {
  46. constructor() {
  47. super();
  48. this.state = {
  49. aaa: 111
  50. };
  51. this.click = this.click.bind(this)
  52. }
  53. click(e) {
  54. e.preventDefault();
  55. console.log('clicked' + this.state.aaa);
  56. // expect(e.currentTarget.nodeType).toBe(1);
  57. this.setState({
  58. aaa: this.state.aaa + 1
  59. });
  60. }
  61. render() {
  62. return (
  63. <div
  64. id="aaa3"
  65. style={{
  66. height: this.state.aaa,
  67. color: 'blue'
  68. }}
  69. onClick={this.click}
  70. >
  71. {this.state.aaa}
  72. </div>
  73. );
  74. }
  75. }
  76. var s = ReactDOM.render(<App />, div);
  77. //记一个坑 如果是在页面上点击该dom this.state.aaa会增加多次,那是因为
  78. //karma在跑完当前test之后 还会接着跑 ref.spec.jsx的test 跑ref-test的test等
  79. //故而会多次触发点击; 故测试用代码触发即可;ReactTestUtils.Simulate.click
  80. expect(s.state.aaa).toBe(111);
  81. ReactTestUtils.Simulate.click(s.mayInst.hostNode);
  82. expect(s.state.aaa).toBe(112);
  83. ReactTestUtils.Simulate.click(s.mayInst.hostNode);
  84. expect(s.state.aaa).toBe(113);
  85. //确保存在eventSystem对象
  86. // expect(React.eventSystem).toA("object");
  87. });
  88. it("冒泡", async () => {
  89. var aaa = "";
  90. class App extends React.PureComponent {
  91. constructor(props) {
  92. super(props);
  93. this.state = {
  94. aaa: {
  95. a: 7
  96. }
  97. };
  98. }
  99. click() {
  100. aaa += "aaa ";
  101. }
  102. click2(e) {
  103. aaa += "bbb ";
  104. e.stopPropagation();
  105. }
  106. click3(e) {
  107. aaa += "ccc ";
  108. }
  109. render() {
  110. return (
  111. <div onClick={this.click}>
  112. <p>=========</p>
  113. <div onClick={this.click2}>
  114. <p>=====</p>
  115. <div id="bubble" onClick={this.click3}>
  116. {this.state.aaa.a}
  117. </div>
  118. </div>
  119. </div>
  120. );
  121. }
  122. }
  123. var s = ReactDOM.render(<App />, div);
  124. ReactTestUtils.Simulate.click(document.getElementById("bubble"));
  125. expect(aaa.trim()).toBe("ccc bbb");
  126. });
  127. it("模拟mouseover,mouseout", async () => {
  128. var aaa = "";
  129. class App extends React.Component {
  130. constructor(props) {
  131. super(props);
  132. this.state = {
  133. aaa: {
  134. a: 7
  135. }
  136. };
  137. }
  138. mouseover() {
  139. aaa += "aaa ";
  140. }
  141. mouseout(e) {
  142. console.log(aaa);
  143. aaa += "bbb ";
  144. }
  145. render() {
  146. return (
  147. <div>
  148. <div
  149. id="mouse1"
  150. onMouseOver={this.mouseover}
  151. onMouseOut={this.mouseout}
  152. style={{
  153. width: 200,
  154. height: 200
  155. }}
  156. >67</div>
  157. <div id="mouse2" />
  158. </div>
  159. );
  160. }
  161. }
  162. var s = ReactDOM.render(<App />, div);
  163. ReactTestUtils.Simulate.mouseover(document.getElementById('mouse1'));
  164. ReactTestUtils.Simulate.mouseout(document.getElementById('mouse1'));
  165. expect(aaa.trim()).toBe("aaa bbb");
  166. });
  167. it("1.1.2checkbox绑定onChange事件会触发两次", async () => {
  168. var logIndex = 0;
  169. function refFn(e) {
  170. logIndex++;
  171. }
  172. var el = ReactDOM.render(<input id="ci" type="checkbox" onChange={refFn} />, div);
  173. ReactTestUtils.Simulate.change(document.getElementById('ci'));
  174. expect(logIndex).toBe(1);
  175. });
  176. it("模拟mouseenter,mouseleave", async () => {
  177. var aaa = "";
  178. class App extends React.Component {
  179. constructor(props) {
  180. super(props);
  181. this.state = {
  182. aaa: {
  183. a: 7
  184. }
  185. };
  186. }
  187. mouseover() {
  188. aaa += "aaa ";
  189. }
  190. mouseout(e) {
  191. aaa += "bbb ";
  192. }
  193. render() {
  194. return (
  195. <div>
  196. <div
  197. id="mouse3"
  198. onMouseEnter={this.mouseover}
  199. onMouseLeave={this.mouseout}
  200. style={{
  201. width: 200,
  202. height: 200
  203. }}
  204. />
  205. <div id="mouse4" />
  206. </div>
  207. );
  208. }
  209. }
  210. var s = ReactDOM.render(<App />, div);
  211. ReactTestUtils.Simulate.mouseEnter(document.getElementById('mouse3'));
  212. ReactTestUtils.Simulate.mouseLeave(document.getElementById('mouse3'));
  213. expect(aaa.trim()).toBe("aaa bbb");
  214. });
  215. it("捕获", async () => {
  216. var aaa = "";
  217. class App extends React.PureComponent {
  218. constructor(props) {
  219. super(props);
  220. this.state = {
  221. aaa: {
  222. a: 7
  223. }
  224. };
  225. }
  226. click() {
  227. aaa += "aaa ";
  228. }
  229. click2(e) {
  230. aaa += "bbb ";
  231. e.preventDefault();
  232. e.stopPropagation();
  233. }
  234. click3(e) {
  235. aaa += "ccc ";
  236. }
  237. render() {
  238. return (
  239. <div onClickCapture={this.click}>
  240. <p>=========</p>
  241. <div onClickCapture={this.click2}>
  242. <p>=====</p>
  243. <div id="capture" onClickCapture={this.click3}>
  244. {this.state.aaa.a}
  245. </div>
  246. </div>
  247. </div>
  248. );
  249. }
  250. }
  251. var s = ReactDOM.render(<App />, div);
  252. ReactTestUtils.Simulate.click(document.getElementById("capture"));
  253. expect(aaa.trim()).toBe("aaa bbb");
  254. });
  255. it("让focus能冒泡", async () => {
  256. var aaa = "";
  257. class App extends React.Component {
  258. constructor(props) {
  259. super(props);
  260. this.state = {
  261. aaa: {
  262. a: 7
  263. }
  264. };
  265. }
  266. onFocus1() {
  267. aaa += "aaa ";
  268. }
  269. onFocus2(e) {
  270. aaa += "bbb ";
  271. console.log('fff ' + aaa);
  272. }
  273. render() {
  274. return (
  275. <div
  276. onFocus={this.onFocus2}
  277. style={{
  278. width: 200,
  279. height: 200
  280. }}
  281. >
  282. <div
  283. id="focus2"
  284. tabIndex={-1}
  285. onFocus={this.onFocus1}
  286. style={{
  287. width: 100,
  288. height: 100
  289. }}
  290. >
  291. focus2
  292. </div>
  293. </div>
  294. );
  295. }
  296. }
  297. var s = ReactDOM.render(<App />, div);
  298. ReactTestUtils.Simulate.focus(document.getElementById("focus2"));
  299. expect(aaa.trim()).toBe("aaa bbb");
  300. });
  301. it("测试事件对象的属性", function () {
  302. var obj = {
  303. type: "change",
  304. srcElement: 1
  305. };
  306. var e = new SyntheticEvent(obj);
  307. expect(e.type).toBe("change");
  308. expect(typeof e.timeStamp).toBe("number");
  309. expect(e.target).toBe(1);
  310. expect(e.nativeEvent).toBe(obj);
  311. e.stopImmediatePropagation();
  312. expect(e._stopPropagation).toBe(true);
  313. expect(e.toString()).toBe("[object Event]");
  314. var e2 = new SyntheticEvent(e);
  315. expect(e2).toBe(e);
  316. // var p = new DOMElement();
  317. // p.addEventListener = false;
  318. // addEvent(p, "type", "xxx");
  319. });
  320. it("合并点击事件中的setState", async () => {
  321. var list = [];
  322. class App extends React.Component {
  323. constructor(props) {
  324. super(props);
  325. this.state = {
  326. path: "111"
  327. };
  328. }
  329. render() {
  330. list.push("render " + this.state.path);
  331. return (
  332. <div>
  333. <span id="click2time" onClick={this.onClick.bind(this)}>
  334. {this.state.path}
  335. </span>
  336. </div>
  337. );
  338. }
  339. onClick() {
  340. this.setState(
  341. {
  342. path: "click"
  343. },
  344. function () {
  345. list.push("click....");
  346. }
  347. );
  348. this.setState(
  349. {
  350. path: "click2"
  351. },
  352. function () {
  353. list.push("click2....");
  354. }
  355. );
  356. }
  357. componentWillUpdate() {
  358. list.push("will update");
  359. }
  360. componentDidUpdate() {
  361. list.push("did update");
  362. }
  363. }
  364. ReactDOM.render(<App />, div, function () {
  365. list.push("ReactDOM cb");
  366. });
  367. ReactTestUtils.Simulate.click(document.getElementById("click2time"));
  368. expect(list).toEqual(["render 111", "ReactDOM cb", "will update", "render click2", "did update", "click....", "click2...."]);
  369. });
  370. });