grid.spec.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import path from 'node:path'
  2. import { E2E_TIMEOUT, setupPuppeteer } from './e2eUtils'
  3. interface TableData {
  4. name: string
  5. power: number
  6. }
  7. describe('e2e: grid', () => {
  8. const { page, click, text, count, typeValue, clearValue } = setupPuppeteer()
  9. const columns = ['name', 'power'] as const
  10. async function assertTable(data: TableData[]) {
  11. expect(await count('td')).toBe(data.length * columns.length)
  12. for (let i = 0; i < data.length; i++) {
  13. for (let j = 0; j < columns.length; j++) {
  14. expect(
  15. await text(`tr:nth-child(${i + 1}) td:nth-child(${j + 1})`),
  16. ).toContain(`${data[i][columns[j]]}`)
  17. }
  18. }
  19. }
  20. async function testGrid(apiType: 'classic' | 'composition') {
  21. const baseUrl = `file://${path.resolve(
  22. __dirname,
  23. `../../examples/${apiType}/grid.html`,
  24. )}`
  25. await page().goto(baseUrl)
  26. await page().waitForSelector('table')
  27. expect(await count('th')).toBe(2)
  28. expect(await count('th.active')).toBe(0)
  29. expect(await text('th:nth-child(1)')).toContain('Name')
  30. expect(await text('th:nth-child(2)')).toContain('Power')
  31. await assertTable([
  32. { name: 'Chuck Norris', power: Infinity },
  33. { name: 'Bruce Lee', power: 9000 },
  34. { name: 'Jackie Chan', power: 7000 },
  35. { name: 'Jet Li', power: 8000 },
  36. ])
  37. await click('th:nth-child(1)')
  38. expect(await count('th.active:nth-child(1)')).toBe(1)
  39. expect(await count('th.active:nth-child(2)')).toBe(0)
  40. expect(await count('th:nth-child(1) .arrow.dsc')).toBe(1)
  41. expect(await count('th:nth-child(2) .arrow.dsc')).toBe(0)
  42. await assertTable([
  43. { name: 'Jet Li', power: 8000 },
  44. { name: 'Jackie Chan', power: 7000 },
  45. { name: 'Chuck Norris', power: Infinity },
  46. { name: 'Bruce Lee', power: 9000 },
  47. ])
  48. await click('th:nth-child(2)')
  49. expect(await count('th.active:nth-child(1)')).toBe(0)
  50. expect(await count('th.active:nth-child(2)')).toBe(1)
  51. expect(await count('th:nth-child(1) .arrow.dsc')).toBe(1)
  52. expect(await count('th:nth-child(2) .arrow.dsc')).toBe(1)
  53. await assertTable([
  54. { name: 'Chuck Norris', power: Infinity },
  55. { name: 'Bruce Lee', power: 9000 },
  56. { name: 'Jet Li', power: 8000 },
  57. { name: 'Jackie Chan', power: 7000 },
  58. ])
  59. await click('th:nth-child(2)')
  60. expect(await count('th.active:nth-child(1)')).toBe(0)
  61. expect(await count('th.active:nth-child(2)')).toBe(1)
  62. expect(await count('th:nth-child(1) .arrow.dsc')).toBe(1)
  63. expect(await count('th:nth-child(2) .arrow.asc')).toBe(1)
  64. await assertTable([
  65. { name: 'Jackie Chan', power: 7000 },
  66. { name: 'Jet Li', power: 8000 },
  67. { name: 'Bruce Lee', power: 9000 },
  68. { name: 'Chuck Norris', power: Infinity },
  69. ])
  70. await click('th:nth-child(1)')
  71. expect(await count('th.active:nth-child(1)')).toBe(1)
  72. expect(await count('th.active:nth-child(2)')).toBe(0)
  73. expect(await count('th:nth-child(1) .arrow.asc')).toBe(1)
  74. expect(await count('th:nth-child(2) .arrow.asc')).toBe(1)
  75. await assertTable([
  76. { name: 'Bruce Lee', power: 9000 },
  77. { name: 'Chuck Norris', power: Infinity },
  78. { name: 'Jackie Chan', power: 7000 },
  79. { name: 'Jet Li', power: 8000 },
  80. ])
  81. await typeValue('input[name="query"]', 'j')
  82. await assertTable([
  83. { name: 'Jackie Chan', power: 7000 },
  84. { name: 'Jet Li', power: 8000 },
  85. ])
  86. await typeValue('input[name="query"]', 'infinity')
  87. await assertTable([{ name: 'Chuck Norris', power: Infinity }])
  88. await clearValue('input[name="query"]')
  89. expect(await count('p')).toBe(0)
  90. await typeValue('input[name="query"]', 'stringthatdoesnotexistanywhere')
  91. expect(await count('p')).toBe(1)
  92. }
  93. test(
  94. 'classic',
  95. async () => {
  96. await testGrid('classic')
  97. },
  98. E2E_TIMEOUT,
  99. )
  100. test(
  101. 'composition',
  102. async () => {
  103. await testGrid('composition')
  104. },
  105. E2E_TIMEOUT,
  106. )
  107. })