svg.spec.ts 4.2 KB

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