|
| 1 | +import { describe, expect, it, vi } from 'vitest' |
| 2 | +import { generateTestTableWithData } from '../../../helpers/generateTestTable' |
| 3 | +import { |
| 4 | + column_getIndex, |
| 5 | + column_getIsFirstColumn, |
| 6 | + column_getIsLastColumn, |
| 7 | + getDefaultColumnOrderState, |
| 8 | + orderColumns, |
| 9 | + table_getOrderColumnsFn, |
| 10 | + table_resetColumnOrder, |
| 11 | + table_setColumnOrder, |
| 12 | +} from '../../../../src/features/column-ordering/columnOrderingFeature.utils' |
| 13 | +import type { TableFeatures } from '../../../../src' |
| 14 | + |
| 15 | +describe('getDefaultColumnOrderState', () => { |
| 16 | + it('should return an empty array', () => { |
| 17 | + expect(getDefaultColumnOrderState()).toEqual([]) |
| 18 | + }) |
| 19 | +}) |
| 20 | + |
| 21 | +describe('column_getIndex', () => { |
| 22 | + it('should return correct index for a column', () => { |
| 23 | + const table = generateTestTableWithData<TableFeatures>(3) |
| 24 | + const column = table.getAllLeafColumns()[1]! |
| 25 | + |
| 26 | + expect(column_getIndex(column)).toBe(1) |
| 27 | + }) |
| 28 | + |
| 29 | + it('should return -1 for non-existent column', () => { |
| 30 | + const table = generateTestTableWithData<TableFeatures>(3) |
| 31 | + const column = { |
| 32 | + ...table.getAllLeafColumns()[0], |
| 33 | + id: 'non-existent', |
| 34 | + table, |
| 35 | + } as any |
| 36 | + |
| 37 | + expect(column_getIndex(column)).toBe(-1) |
| 38 | + }) |
| 39 | +}) |
| 40 | + |
| 41 | +describe('column_getIsFirstColumn', () => { |
| 42 | + it('should return true for first column', () => { |
| 43 | + const table = generateTestTableWithData<TableFeatures>(3) |
| 44 | + const firstColumn = table.getAllLeafColumns()[0]! |
| 45 | + |
| 46 | + expect(column_getIsFirstColumn(firstColumn)).toBe(true) |
| 47 | + }) |
| 48 | + |
| 49 | + it('should return false for non-first column', () => { |
| 50 | + const table = generateTestTableWithData<TableFeatures>(3) |
| 51 | + const secondColumn = table.getAllLeafColumns()[1]! |
| 52 | + |
| 53 | + expect(column_getIsFirstColumn(secondColumn)).toBe(false) |
| 54 | + }) |
| 55 | +}) |
| 56 | + |
| 57 | +describe('column_getIsLastColumn', () => { |
| 58 | + it('should return true for last column', () => { |
| 59 | + const table = generateTestTableWithData<TableFeatures>(3) |
| 60 | + const columns = table.getAllLeafColumns() |
| 61 | + const lastColumn = columns[columns.length - 1]! |
| 62 | + |
| 63 | + expect(column_getIsLastColumn(lastColumn)).toBe(true) |
| 64 | + }) |
| 65 | + |
| 66 | + it('should return false for non-last column', () => { |
| 67 | + const table = generateTestTableWithData<TableFeatures>(3) |
| 68 | + const firstColumn = table.getAllLeafColumns()[0]! |
| 69 | + |
| 70 | + expect(column_getIsLastColumn(firstColumn)).toBe(false) |
| 71 | + }) |
| 72 | +}) |
| 73 | + |
| 74 | +describe('table_setColumnOrder', () => { |
| 75 | + it('should call onColumnOrderChange with updater', () => { |
| 76 | + const onColumnOrderChange = vi.fn() |
| 77 | + const table = generateTestTableWithData<TableFeatures>(3, { |
| 78 | + onColumnOrderChange, |
| 79 | + }) |
| 80 | + const newOrder = ['col1', 'col2'] |
| 81 | + |
| 82 | + table_setColumnOrder(table, newOrder) |
| 83 | + |
| 84 | + expect(onColumnOrderChange).toHaveBeenCalledWith(newOrder) |
| 85 | + }) |
| 86 | +}) |
| 87 | + |
| 88 | +describe('table_resetColumnOrder', () => { |
| 89 | + it('should reset to empty array when defaultState is true', () => { |
| 90 | + const onColumnOrderChange = vi.fn() |
| 91 | + const table = generateTestTableWithData<TableFeatures>(3, { |
| 92 | + onColumnOrderChange, |
| 93 | + }) |
| 94 | + |
| 95 | + table_resetColumnOrder(table, true) |
| 96 | + |
| 97 | + expect(onColumnOrderChange).toHaveBeenCalledWith([]) |
| 98 | + }) |
| 99 | + |
| 100 | + it('should reset to initialState when defaultState is false', () => { |
| 101 | + const initialColumnOrder = ['col1', 'col2'] |
| 102 | + const onColumnOrderChange = vi.fn() |
| 103 | + const table = generateTestTableWithData<TableFeatures>(3, { |
| 104 | + onColumnOrderChange, |
| 105 | + initialState: { columnOrder: initialColumnOrder }, |
| 106 | + }) |
| 107 | + |
| 108 | + table_resetColumnOrder(table, false) |
| 109 | + |
| 110 | + expect(onColumnOrderChange).toHaveBeenCalledWith(initialColumnOrder) |
| 111 | + }) |
| 112 | +}) |
| 113 | + |
| 114 | +describe('table_getOrderColumnsFn', () => { |
| 115 | + it('should return original columns when no column order is specified', () => { |
| 116 | + const table = generateTestTableWithData<TableFeatures>(3) |
| 117 | + const columns = table.getAllLeafColumns() |
| 118 | + const orderFn = table_getOrderColumnsFn(table) |
| 119 | + |
| 120 | + expect(orderFn(columns)).toEqual(columns) |
| 121 | + }) |
| 122 | + |
| 123 | + it('should reorder columns according to columnOrder', () => { |
| 124 | + const table = generateTestTableWithData<TableFeatures>(3, { |
| 125 | + state: { |
| 126 | + columnOrder: ['lastName', 'firstName'], |
| 127 | + }, |
| 128 | + }) |
| 129 | + const columns = table.getAllLeafColumns() |
| 130 | + const orderFn = table_getOrderColumnsFn(table) |
| 131 | + const orderedColumns = orderFn(columns) |
| 132 | + |
| 133 | + expect(orderedColumns[0]?.id).toBe('lastName') |
| 134 | + expect(orderedColumns[1]?.id).toBe('firstName') |
| 135 | + }) |
| 136 | +}) |
| 137 | + |
| 138 | +describe('orderColumns', () => { |
| 139 | + it('should return original columns when no grouping is present', () => { |
| 140 | + const table = generateTestTableWithData<TableFeatures>(3) |
| 141 | + const columns = table.getAllLeafColumns() |
| 142 | + |
| 143 | + expect(orderColumns(table, columns)).toEqual(columns) |
| 144 | + }) |
| 145 | + |
| 146 | + it('should remove grouped columns when groupedColumnMode is "remove"', () => { |
| 147 | + const table = generateTestTableWithData<TableFeatures>(3, { |
| 148 | + state: { |
| 149 | + grouping: ['firstName'], |
| 150 | + }, |
| 151 | + groupedColumnMode: 'remove', |
| 152 | + }) |
| 153 | + const columns = table.getAllLeafColumns() |
| 154 | + const orderedColumns = orderColumns(table, columns) |
| 155 | + |
| 156 | + expect(orderedColumns.find((col) => col.id === 'firstName')).toBeUndefined() |
| 157 | + }) |
| 158 | + |
| 159 | + it('should move grouped columns to start when groupedColumnMode is "reorder"', () => { |
| 160 | + const table = generateTestTableWithData<TableFeatures>(3, { |
| 161 | + state: { |
| 162 | + grouping: ['lastName'], |
| 163 | + }, |
| 164 | + groupedColumnMode: 'reorder', |
| 165 | + }) |
| 166 | + const columns = table.getAllLeafColumns() |
| 167 | + const orderedColumns = orderColumns(table, columns) |
| 168 | + |
| 169 | + expect(orderedColumns[0]?.id).toBe('lastName') |
| 170 | + }) |
| 171 | +}) |
0 commit comments