svg.spec.jsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import React from '../../src/May'
  2. var ReactDOM = React;
  3. describe('SVG元素', function () {
  4. // this.timeout(200000);
  5. // before(async () => {
  6. // await beforeHook()
  7. // })
  8. // after(async () => {
  9. // await afterHook(false)
  10. // })
  11. var body = document.body,
  12. div
  13. beforeEach(function () {
  14. div = document.createElement('div')
  15. body.appendChild(div)
  16. })
  17. afterEach(function () {
  18. body.removeChild(div)
  19. })
  20. var rsvg = /^\[object SVG\w*Element\]$/
  21. it('circle', async () => {
  22. var s = ReactDOM.render(
  23. <svg><circle cx='25' cy='25' r='20' fill='green' /></svg>, div)
  24. expect(rsvg.test(s.firstChild)).toBe(true)
  25. })
  26. it('ellipse', async() => {
  27. var s = ReactDOM.render(
  28. <svg><ellipse cx='25' cy='25' rx='20' ry='10' fill='green'/></svg>, div)
  29. expect(rsvg.test(s.firstChild)).toBe(true)
  30. })
  31. it('line', async() => {
  32. var s = ReactDOM.render(
  33. <svg><line x1='5' y1='5' x2='45' y2='45' stroke='green'/></svg>, div)
  34. expect(rsvg.test(s.firstChild)).toBe(true)
  35. })
  36. it('path', async() => {
  37. var s = ReactDOM.render(
  38. <svg><path d='M5,5 C5,45 45,45 45,5' fill="none" stroke='red'/></svg>, div)
  39. expect(rsvg.test(s.firstChild)).toBe(true)
  40. })
  41. it('polygon', async() => {
  42. var s = ReactDOM.render(
  43. <svg><polygon points='5,5 45,45 5,45 45,5' fill="none" stroke='red'/></svg>, div)
  44. expect(rsvg.test(s.firstChild)).toBe(true)
  45. })
  46. it('polyline', async() => {
  47. var s = ReactDOM.render(
  48. <svg><polyline points='5,5 45,45 5,45 45,5' fill="none" stroke='red'/></svg>, div)
  49. expect(rsvg.test(s.firstChild)).toBe(true)
  50. })
  51. it('rect', async() => {
  52. var s = ReactDOM.render(
  53. <svg><rect
  54. x='5'
  55. y='5'
  56. rx='5'
  57. ry='5'
  58. width='40'
  59. height='40'
  60. fill="green"
  61. stroke='red'/></svg>, div)
  62. expect(rsvg.test(s.firstChild)).toBe(true)
  63. })
  64. it('defs', async() => {
  65. var s = ReactDOM.render(
  66. <svg>
  67. <defs>
  68. <rect id='rect' style='fill:green' width='15' height='15'/>
  69. </defs>
  70. <use x='5' y='5' xlinkHref='#rect'/>
  71. <use x='30' y='30' xlinkHref='#rect'/>
  72. </svg>, div)
  73. expect(rsvg.test(s.firstChild)).toBe(true)
  74. })
  75. //svg一般用的不会太复杂,先支持比较简单的
  76. /*it('attribute throw error', async() => {
  77. var a = {}
  78. a.toString = function () {
  79. throw "xxx"
  80. }
  81. var s = ReactDOM.render(React.createElement('div', {aaa: a}), div)
  82. expect(s.getAttribute('aaa')).toBeNull()
  83. })
  84. it('use元素的xlinkHref', async() => {
  85. function Test() {
  86. return (
  87. <svg className="icon-twitter" width="16px" height="16px">
  88. <use xlinkHref="#twitter" xlinkRole="#aaa" id='aaa'/>
  89. </svg>
  90. )
  91. }
  92. ReactDOM.render(
  93. <Test/>, div)
  94. var el = div.getElementsByTagName('use')
  95. expect(el.length).toBe(1)
  96. expect(el[0].getAttribute('xlink:href')).toBe('#twitter')
  97. expect(el[0].getAttribute('xlink:role')).toBe('#aaa')
  98. })*/
  99. })