table-v2.test.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { ref } from 'vue'
  2. import { mount } from '@vue/test-utils'
  3. import { describe, expect, test } from 'vitest'
  4. import TableV2 from '../src/table-v2'
  5. import type {
  6. TableV2HeaderRowCellRendererParams,
  7. TableV2RowCellRenderParam,
  8. } from '../src/components'
  9. const generateColumns = (length = 10, prefix = 'column-', props?: any) =>
  10. Array.from({ length }).map((_, columnIndex) => ({
  11. ...props,
  12. key: `${prefix}${columnIndex}`,
  13. dataKey: `${prefix}${columnIndex}`,
  14. title: `Column ${columnIndex}`,
  15. width: 150,
  16. }))
  17. const generateData = (
  18. columns: ReturnType<typeof generateColumns>,
  19. length = 200,
  20. prefix = 'row-'
  21. ) =>
  22. Array.from({ length }).map((_, rowIndex) => {
  23. return columns.reduce(
  24. (rowData, column, columnIndex) => {
  25. rowData[column.dataKey] = `Row ${rowIndex} - Col ${columnIndex}`
  26. return rowData
  27. },
  28. {
  29. id: `${prefix}${rowIndex}`,
  30. parentId: null,
  31. }
  32. )
  33. })
  34. describe('TableV2.vue', () => {
  35. test('slots cell', async () => {
  36. const columns = ref(generateColumns(10))
  37. const data = ref(generateData(columns.value, 20))
  38. const customText = 'custom cell'
  39. const wrapper = mount(() => (
  40. <TableV2
  41. columns={columns.value}
  42. data={data.value}
  43. width={700}
  44. height={400}
  45. v-slots={{
  46. cell: () => <span>{customText}</span>,
  47. }}
  48. />
  49. ))
  50. expect(wrapper.find('.el-table-v2').exists()).toBe(true)
  51. const cell = wrapper.find('.el-table-v2__row-cell')
  52. expect(cell.exists()).toBe(true)
  53. expect(cell.find('span').text()).toBe(customText)
  54. })
  55. test('slots header-cell', async () => {
  56. const columns = ref(generateColumns(10))
  57. const data = ref(generateData(columns.value, 20))
  58. const customText = 'header'
  59. const wrapper = mount(() => (
  60. <TableV2
  61. columns={columns.value}
  62. data={data.value}
  63. width={700}
  64. height={400}
  65. v-slots={{
  66. 'header-cell': () => <span>{customText}</span>,
  67. }}
  68. />
  69. ))
  70. expect(wrapper.find('.el-table-v2').exists()).toBe(true)
  71. const cell = wrapper.find('.el-table-v2__header-cell')
  72. expect(cell.exists()).toBe(true)
  73. expect(cell.find('span').text()).toBe(customText)
  74. })
  75. test('slots cell scope', async () => {
  76. const columns = ref(generateColumns(10))
  77. const data = ref(generateData(columns.value, 20))
  78. const customText = 'custom cell'
  79. const wrapper = mount(() => (
  80. <TableV2
  81. columns={columns.value}
  82. data={data.value}
  83. width={700}
  84. height={400}
  85. v-slots={{
  86. cell: (scope: TableV2RowCellRenderParam) => (
  87. <span>
  88. {scope.rowData[scope.column.dataKey!]}
  89. {customText}
  90. </span>
  91. ),
  92. }}
  93. />
  94. ))
  95. expect(wrapper.find('.el-table-v2').exists()).toBe(true)
  96. const cell = wrapper.find('.el-table-v2__row-cell')
  97. expect(cell.exists()).toBe(true)
  98. expect(cell.find('span').text()).toBe(
  99. `${data.value[0][columns.value[0].dataKey]}${customText}`
  100. )
  101. })
  102. test('slots header-cell scope', async () => {
  103. const columns = ref(generateColumns(10))
  104. const data = ref(generateData(columns.value, 20))
  105. const customText = 'header'
  106. const wrapper = mount(() => (
  107. <TableV2
  108. columns={columns.value}
  109. data={data.value}
  110. width={700}
  111. height={400}
  112. v-slots={{
  113. 'header-cell': (scope: TableV2HeaderRowCellRendererParams) => (
  114. <span>
  115. {scope.column.title}
  116. {customText}
  117. </span>
  118. ),
  119. }}
  120. />
  121. ))
  122. expect(wrapper.find('.el-table-v2').exists()).toBe(true)
  123. const cell = wrapper.find('.el-table-v2__header-cell')
  124. expect(cell.exists()).toBe(true)
  125. expect(cell.find('span').text()).toBe(
  126. `${columns.value[0].title}${customText}`
  127. )
  128. })
  129. })