context.spec.jsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import PropTypes from "../../lib/ReactPropTypes";
  2. import ReactTestUtils from "../../lib/ReactTestUtils";
  3. import React from '../../src/May';
  4. import { render, unmountComponentAtNode } from '../../src/may-dom/MayDom'
  5. var ReactDOM = {
  6. render: render,
  7. unmountComponentAtNode: unmountComponentAtNode
  8. }
  9. React.render = render;
  10. // import React from "../../dist/ReactANU";
  11. // var ReactDOM=React;
  12. // var React = require('react');//hyphenate
  13. // var ReactDOM = require('react-dom');
  14. describe('context', function () {
  15. // this.timeout(200000);
  16. var body = document.body, div
  17. beforeEach(function () {
  18. div = document.createElement('div')
  19. body.appendChild(div)
  20. })
  21. afterEach(function () {
  22. body.removeChild(div)
  23. })
  24. it('getChildContext', async () => {
  25. var arr = ['111', '222', '333']
  26. class App extends React.Component {
  27. getChildContext() {
  28. return {
  29. name: "Jonas",
  30. fruit: "Banana"
  31. };
  32. }
  33. handleClick() {
  34. this.getChildContext = function () {
  35. return {
  36. name: "Jonas",
  37. fruit: arr.shift()
  38. };
  39. }
  40. this.forceUpdate()
  41. }
  42. render() {
  43. return <div ref='a' onClick={this.handleClick.bind(this)}><h4>{new Date - 0}</h4><B /></div>;
  44. }
  45. }
  46. App.childContextTypes = {
  47. name: PropTypes.string,
  48. fruit: PropTypes.string
  49. }
  50. class B extends React.Component {
  51. render() {
  52. return <div><C /><strong>{this.context.fruit}</strong></div>;
  53. }
  54. }
  55. B.contextTypes = {
  56. fruit: PropTypes.string
  57. }
  58. class C extends React.Component {
  59. render() {
  60. return <strong>{this.context.fruit}</strong>;
  61. }
  62. }
  63. var s = ReactDOM.render(<App />, div)
  64. var strongs = div.getElementsByTagName('strong')
  65. expect(strongs[0].innerHTML).toBe('')
  66. expect(strongs[1].innerHTML).toBe('Banana')
  67. ReactTestUtils.Simulate.click(s.refs.a)
  68. strongs = div.getElementsByTagName('strong')
  69. expect(strongs[0].innerHTML).toBe('')
  70. expect(strongs[1].innerHTML).toBe('111')
  71. ReactTestUtils.Simulate.click(s.refs.a)
  72. strongs = div.getElementsByTagName('strong')
  73. expect(strongs[0].innerHTML).toBe('')
  74. expect(strongs[1].innerHTML).toBe('222')
  75. })
  76. })