svg.spec.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import { setupPuppeteer, getExampleUrl, E2E_TIMEOUT } from './e2eUtils'
  2. declare const globalStats: {
  3. label: string
  4. value: number
  5. }[]
  6. declare function valueToPoint(
  7. value: number,
  8. index: number,
  9. total: number
  10. ): {
  11. x: number
  12. y: number
  13. }
  14. describe('e2e: svg', () => {
  15. const { page, click, count, setValue, typeValue } = setupPuppeteer()
  16. // assert the shape of the polygon is correct
  17. async function assertPolygon(total: number) {
  18. expect(
  19. await page().evaluate(
  20. total => {
  21. const points = globalStats
  22. .map((stat, i) => {
  23. const point = valueToPoint(stat.value, i, total)
  24. return point.x + ',' + point.y
  25. })
  26. .join(' ')
  27. return (
  28. document.querySelector('polygon')!.attributes[0].value === points
  29. )
  30. },
  31. [total]
  32. )
  33. ).toBe(true)
  34. }
  35. // assert the position of each label is correct
  36. async function assertLabels(total: number) {
  37. const positions = await page().evaluate(
  38. total => {
  39. return globalStats.map((stat, i) => {
  40. const point = valueToPoint(+stat.value + 10, i, total)
  41. return [point.x, point.y]
  42. })
  43. },
  44. [total]
  45. )
  46. for (let i = 0; i < total; i++) {
  47. const textPosition = await page().$eval(
  48. `text:nth-child(${i + 3})`,
  49. node => [+node.attributes[0].value, +node.attributes[1].value]
  50. )
  51. expect(textPosition).toEqual(positions[i])
  52. }
  53. }
  54. // assert each value of stats is correct
  55. async function assertStats(expected: number[]) {
  56. const statsValue = await page().evaluate(() => {
  57. return globalStats.map(stat => +stat.value)
  58. })
  59. expect(statsValue).toEqual(expected)
  60. }
  61. function nthRange(n: number) {
  62. return `#demo div:nth-child(${n + 1}) input[type="range"]`
  63. }
  64. async function testSvg(apiType: 'classic' | 'composition') {
  65. await page().goto(getExampleUrl('svg', apiType))
  66. await page().waitForSelector('svg')
  67. expect(await count('g')).toBe(1)
  68. expect(await count('polygon')).toBe(1)
  69. expect(await count('circle')).toBe(1)
  70. expect(await count('text')).toBe(6)
  71. expect(await count('label')).toBe(6)
  72. expect(await count('button')).toBe(7)
  73. expect(await count('input[type="range"]')).toBe(6)
  74. await assertPolygon(6)
  75. await assertLabels(6)
  76. await assertStats([100, 100, 100, 100, 100, 100])
  77. await setValue(nthRange(1), '10')
  78. await assertPolygon(6)
  79. await assertLabels(6)
  80. await assertStats([10, 100, 100, 100, 100, 100])
  81. await click('button.remove')
  82. expect(await count('text')).toBe(5)
  83. expect(await count('label')).toBe(5)
  84. expect(await count('button')).toBe(6)
  85. expect(await count('input[type="range"]')).toBe(5)
  86. await assertPolygon(5)
  87. await assertLabels(5)
  88. await assertStats([100, 100, 100, 100, 100])
  89. await typeValue('input[name="newlabel"]', 'foo')
  90. await click('#add > button')
  91. expect(await count('text')).toBe(6)
  92. expect(await count('label')).toBe(6)
  93. expect(await count('button')).toBe(7)
  94. expect(await count('input[type="range"]')).toBe(6)
  95. await assertPolygon(6)
  96. await assertLabels(6)
  97. await assertStats([100, 100, 100, 100, 100, 100])
  98. await setValue(nthRange(1), '10')
  99. await assertPolygon(6)
  100. await assertLabels(6)
  101. await assertStats([10, 100, 100, 100, 100, 100])
  102. await setValue(nthRange(2), '20')
  103. await assertPolygon(6)
  104. await assertLabels(6)
  105. await assertStats([10, 20, 100, 100, 100, 100])
  106. await setValue(nthRange(6), '60')
  107. await assertPolygon(6)
  108. await assertLabels(6)
  109. await assertStats([10, 20, 100, 100, 100, 60])
  110. await click('button.remove')
  111. await assertPolygon(5)
  112. await assertLabels(5)
  113. await assertStats([20, 100, 100, 100, 60])
  114. await setValue(nthRange(1), '10')
  115. await assertPolygon(5)
  116. await assertLabels(5)
  117. await assertStats([10, 100, 100, 100, 60])
  118. }
  119. test(
  120. 'classic',
  121. async () => {
  122. await testSvg('classic')
  123. },
  124. E2E_TIMEOUT
  125. )
  126. test(
  127. 'composition',
  128. async () => {
  129. await testSvg('composition')
  130. },
  131. E2E_TIMEOUT
  132. )
  133. })