diffProps.spec.jsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. describe('diffProps', function () {
  10. // this.timeout(200000);
  11. // before(async() => {
  12. // await beforeHook();
  13. // });
  14. // after(async() => {
  15. // await afterHook(false);
  16. // })
  17. var body = document.body, div
  18. beforeEach(function () {
  19. div = document.createElement('div')
  20. body.appendChild(div)
  21. })
  22. afterEach(function () {
  23. body.removeChild(div)
  24. })
  25. it('使用对象解构', async () => {
  26. class App extends React.Component {
  27. constructor(props) {
  28. super(props)
  29. this.state = {
  30. title: 'xxx',
  31. className: 'aaa'
  32. }
  33. }
  34. render() {
  35. return <div ref='a' {...this.state}>
  36. xxx
  37. </div>
  38. }
  39. }
  40. var s = ReactDOM.render(<App />, div)
  41. var dom = s.refs.a
  42. expect(dom.title).toBe('xxx')
  43. expect(dom.className).toBe('aaa')
  44. s.setState({ title: '123', id: 'uuuu' })
  45. expect(dom.title).toBe('123')
  46. expect(dom.className).toBe('aaa')
  47. expect(dom.id).toBe('uuuu')
  48. })
  49. it('改变属性', async () => {
  50. var index = 1
  51. class App extends React.Component {
  52. constructor(props) {
  53. super(props)
  54. this.state = {
  55. title: 'xxx',
  56. className: 'aaa'
  57. }
  58. }
  59. onClick() {
  60. index = 0
  61. this.forceUpdate()
  62. }
  63. render() {
  64. return index
  65. ? <div
  66. ref='a'
  67. title='xxx'
  68. className='ddd'
  69. id='h33'
  70. onClick={this
  71. .onClick
  72. .bind(this)}
  73. dangerouslySetInnerHTML={{
  74. __html: '<b>xxx</b>'
  75. }}></div>
  76. : <div ref='a' title='yyy' id='h44' data-bbb='sss'>
  77. xxx{new Date - 0}
  78. </div>
  79. }
  80. }
  81. var s = ReactDOM.render(<App />, div)
  82. var dom = s.refs.a
  83. expect(dom.title).toBe('xxx')
  84. expect(dom.className).toBe('ddd')
  85. expect(dom.id).toBe('h33')
  86. var events = dom._listener || {}
  87. var t;
  88. if (typeof events.onClick === 'function' || typeof events.click === 'function') {
  89. t = 'function';
  90. }
  91. expect(t).toBe('function')
  92. expect(dom.getElementsByTagName('b').length).toBe(1)
  93. index = 0
  94. ReactTestUtils.Simulate.click(document.getElementById('h33'));
  95. dom = s.refs.a
  96. expect(dom.title).toBe('yyy')
  97. expect(dom.className).toBe('')
  98. expect(dom.id).toBe('h44')
  99. expect(dom.getAttribute('data-bbb')).toBe('sss')
  100. var events = dom._listener || {}
  101. if (typeof events.onClick === 'undefined' && typeof events.click === 'undefined') {
  102. t = 'undefined';
  103. }
  104. expect(t).toBe('undefined')
  105. expect(dom.getElementsByTagName('b').length).toBe(0)
  106. })
  107. })