import { ref } from 'vue' import { mount } from '@vue/test-utils' import { describe, expect, test } from 'vitest' import TableV2 from '../src/table-v2' import type { TableV2HeaderRowCellRendererParams, TableV2RowCellRenderParam, } from '../src/components' const generateColumns = (length = 10, prefix = 'column-', props?: any) => Array.from({ length }).map((_, columnIndex) => ({ ...props, key: `${prefix}${columnIndex}`, dataKey: `${prefix}${columnIndex}`, title: `Column ${columnIndex}`, width: 150, })) const generateData = ( columns: ReturnType, length = 200, prefix = 'row-' ) => Array.from({ length }).map((_, rowIndex) => { return columns.reduce( (rowData, column, columnIndex) => { rowData[column.dataKey] = `Row ${rowIndex} - Col ${columnIndex}` return rowData }, { id: `${prefix}${rowIndex}`, parentId: null, } ) }) describe('TableV2.vue', () => { test('slots cell', async () => { const columns = ref(generateColumns(10)) const data = ref(generateData(columns.value, 20)) const customText = 'custom cell' const wrapper = mount(() => ( {customText}, }} /> )) expect(wrapper.find('.el-table-v2').exists()).toBe(true) const cell = wrapper.find('.el-table-v2__row-cell') expect(cell.exists()).toBe(true) expect(cell.find('span').text()).toBe(customText) }) test('slots header-cell', async () => { const columns = ref(generateColumns(10)) const data = ref(generateData(columns.value, 20)) const customText = 'header' const wrapper = mount(() => ( {customText}, }} /> )) expect(wrapper.find('.el-table-v2').exists()).toBe(true) const cell = wrapper.find('.el-table-v2__header-cell') expect(cell.exists()).toBe(true) expect(cell.find('span').text()).toBe(customText) }) test('slots cell scope', async () => { const columns = ref(generateColumns(10)) const data = ref(generateData(columns.value, 20)) const customText = 'custom cell' const wrapper = mount(() => ( ( {scope.rowData[scope.column.dataKey!]} {customText} ), }} /> )) expect(wrapper.find('.el-table-v2').exists()).toBe(true) const cell = wrapper.find('.el-table-v2__row-cell') expect(cell.exists()).toBe(true) expect(cell.find('span').text()).toBe( `${data.value[0][columns.value[0].dataKey]}${customText}` ) }) test('slots header-cell scope', async () => { const columns = ref(generateColumns(10)) const data = ref(generateData(columns.value, 20)) const customText = 'header' const wrapper = mount(() => ( ( {scope.column.title} {customText} ), }} /> )) expect(wrapper.find('.el-table-v2').exists()).toBe(true) const cell = wrapper.find('.el-table-v2__header-cell') expect(cell.exists()).toBe(true) expect(cell.find('span').text()).toBe( `${columns.value[0].title}${customText}` ) }) })