|
| 1 | +import { |
| 2 | + afterEach, beforeEach, describe, expect, it, |
| 3 | +} from '@jest/globals'; |
| 4 | + |
| 5 | +import { |
| 6 | + afterTest, |
| 7 | + beforeTest, |
| 8 | + createDataGrid, |
| 9 | +} from '../__tests__/__mock__/helpers/utils'; |
| 10 | + |
| 11 | +const dataSource = [ |
| 12 | + { id: 1, name: 'Item 1' }, |
| 13 | + { id: 2, name: 'Item 2' }, |
| 14 | + { id: 3, name: 'Item 3' }, |
| 15 | +]; |
| 16 | + |
| 17 | +describe('Column Controller', () => { |
| 18 | + beforeEach(beforeTest); |
| 19 | + afterEach(afterTest); |
| 20 | + |
| 21 | + describe('Expand columns in band columns layout', () => { |
| 22 | + it('detail expand column header should have rowspan equal to header row count when band columns are used', async () => { |
| 23 | + const { instance } = await createDataGrid({ |
| 24 | + dataSource, |
| 25 | + columns: [ |
| 26 | + { caption: 'Band column 1', columns: ['id', 'name'] }, |
| 27 | + { dataField: 'name', caption: 'Column 3', name: 'Column3' }, |
| 28 | + ], |
| 29 | + masterDetail: { |
| 30 | + enabled: true, |
| 31 | + }, |
| 32 | + }); |
| 33 | + |
| 34 | + const columnsController = instance.getController('columns'); |
| 35 | + const rowCount = columnsController.getRowCount(); |
| 36 | + const firstRowColumns = columnsController.getVisibleColumns(0); |
| 37 | + |
| 38 | + expect(rowCount).toBe(2); |
| 39 | + |
| 40 | + const expandColumn = firstRowColumns.find((col) => col.command === 'expand'); |
| 41 | + expect(expandColumn).toBeDefined(); |
| 42 | + expect(expandColumn.rowspan).toBe(2); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should place expand columns only in the first header row with grouped columns', async () => { |
| 46 | + const { instance } = await createDataGrid({ |
| 47 | + dataSource: [ |
| 48 | + { |
| 49 | + TestField1: 'group1', TestField2: 'group2', TestField3: 'val3', TestField4: 'val4', |
| 50 | + }, |
| 51 | + ], |
| 52 | + columns: [ |
| 53 | + { dataField: 'TestField1', caption: 'Column 1', groupIndex: 0 }, |
| 54 | + { |
| 55 | + caption: 'Band Column 1', |
| 56 | + columns: [ |
| 57 | + { dataField: 'TestField2', caption: 'Column 2', groupIndex: 1 }, |
| 58 | + { dataField: 'TestField3', caption: 'Column 3' }, |
| 59 | + { caption: 'Band Column 2', columns: [{ dataField: 'TestField4', caption: 'Column 4' }] }, |
| 60 | + ], |
| 61 | + }, |
| 62 | + ], |
| 63 | + }); |
| 64 | + |
| 65 | + const columnsController = instance.getController('columns'); |
| 66 | + const rowCount = columnsController.getRowCount(); |
| 67 | + |
| 68 | + // Row 0: expand columns + Band Column 1 |
| 69 | + const firstRowColumns = columnsController.getVisibleColumns(0); |
| 70 | + const expandColumnsInFirstRow = firstRowColumns.filter((col) => col.command === 'expand'); |
| 71 | + |
| 72 | + expect(rowCount).toBe(3); |
| 73 | + expect(firstRowColumns.length).toBe(3); |
| 74 | + expect(expandColumnsInFirstRow.length).toBe(2); |
| 75 | + |
| 76 | + expandColumnsInFirstRow.forEach((col) => { |
| 77 | + expect(col.rowspan).toBe(3); |
| 78 | + }); |
| 79 | + |
| 80 | + const bandColumn = firstRowColumns.find((col) => col.caption === 'Band Column 1'); |
| 81 | + |
| 82 | + expect(bandColumn).toBeDefined(); |
| 83 | + expect(bandColumn.isBand).toBe(true); |
| 84 | + expect(bandColumn.rowspan).toBeUndefined(); |
| 85 | + |
| 86 | + // Row 1: Column 3 + Band Column 2 |
| 87 | + const secondRowColumns = columnsController.getVisibleColumns(1); |
| 88 | + const expandColumnsInSecondRow = secondRowColumns.filter((col) => col.command === 'expand'); |
| 89 | + |
| 90 | + expect(secondRowColumns.length).toBe(2); |
| 91 | + expect(expandColumnsInSecondRow.length).toBe(0); |
| 92 | + |
| 93 | + const column3 = secondRowColumns.find((col) => col.caption === 'Column 3'); |
| 94 | + |
| 95 | + expect(column3).toBeDefined(); |
| 96 | + expect(column3.rowspan).toBe(2); |
| 97 | + |
| 98 | + const bandColumn2 = secondRowColumns.find((col) => col.caption === 'Band Column 2'); |
| 99 | + |
| 100 | + expect(bandColumn2).toBeDefined(); |
| 101 | + expect(bandColumn2.isBand).toBe(true); |
| 102 | + expect(bandColumn2.rowspan).toBeUndefined(); |
| 103 | + |
| 104 | + // Row 2: Column 4 |
| 105 | + const thirdRowColumns = columnsController.getVisibleColumns(2); |
| 106 | + const expandColumnsInThirdRow = thirdRowColumns.filter((col) => col.command === 'expand'); |
| 107 | + const column4 = thirdRowColumns.find((col) => col.caption === 'Column 4'); |
| 108 | + |
| 109 | + expect(expandColumnsInThirdRow.length).toBe(0); |
| 110 | + expect(column4).toBeDefined(); |
| 111 | + expect(column4.rowspan).toBeUndefined(); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should place expand columns only in the first header row with showWhenGrouped', async () => { |
| 115 | + const { instance } = await createDataGrid({ |
| 116 | + dataSource: [ |
| 117 | + { field1: 'g1', field2: 'g2', field3: 'g3' }, |
| 118 | + ], |
| 119 | + columns: [{ |
| 120 | + dataField: 'field1', |
| 121 | + showWhenGrouped: true, |
| 122 | + groupIndex: 0, |
| 123 | + }, { |
| 124 | + caption: 'band2', |
| 125 | + columns: [{ |
| 126 | + dataField: 'field2', |
| 127 | + showWhenGrouped: true, |
| 128 | + groupIndex: 1, |
| 129 | + }, { |
| 130 | + caption: 'band3', |
| 131 | + columns: [{ |
| 132 | + dataField: 'field3', |
| 133 | + showWhenGrouped: true, |
| 134 | + groupIndex: 2, |
| 135 | + }], |
| 136 | + }], |
| 137 | + }], |
| 138 | + }); |
| 139 | + |
| 140 | + const columnsController = instance.getController('columns'); |
| 141 | + const rowCount = columnsController.getRowCount(); |
| 142 | + |
| 143 | + expect(rowCount).toBe(3); |
| 144 | + |
| 145 | + // Row 0: expand columns with rowspan=3, data columns with rowspan=3, band column |
| 146 | + const firstRowColumns = columnsController.getVisibleColumns(0); |
| 147 | + const expandColumnsRow0 = firstRowColumns.filter((col) => col.command === 'expand'); |
| 148 | + |
| 149 | + expect(expandColumnsRow0.length).toBe(3); |
| 150 | + expandColumnsRow0.forEach((col) => { |
| 151 | + expect(col.rowspan).toBe(3); |
| 152 | + }); |
| 153 | + |
| 154 | + // showWhenGrouped data columns should be in the first row with rowspan=3 |
| 155 | + const field1Col = firstRowColumns.find((col) => col.caption === 'Field 1' && !col.command); |
| 156 | + expect(field1Col).toBeDefined(); |
| 157 | + expect(field1Col.rowspan).toBe(3); |
| 158 | + |
| 159 | + // band2 should be in the first row without rowspan (it has children) |
| 160 | + const band2Col = firstRowColumns.find((col) => col.caption === 'band2'); |
| 161 | + expect(band2Col).toBeDefined(); |
| 162 | + expect(band2Col.rowspan).toBeUndefined(); |
| 163 | + |
| 164 | + // Row 1: no expand columns |
| 165 | + const secondRowColumns = columnsController.getVisibleColumns(1); |
| 166 | + const expandColumnsRow1 = secondRowColumns.filter((col) => col.command === 'expand'); |
| 167 | + |
| 168 | + expect(expandColumnsRow1.length).toBe(0); |
| 169 | + |
| 170 | + // band3 should be in the second row |
| 171 | + const band3Col = secondRowColumns.find((col) => col.caption === 'band3'); |
| 172 | + expect(band3Col).toBeDefined(); |
| 173 | + |
| 174 | + // Row 2: no expand columns |
| 175 | + const thirdRowColumns = columnsController.getVisibleColumns(2); |
| 176 | + const expandColumnsRow2 = thirdRowColumns.filter((col) => col.command === 'expand'); |
| 177 | + |
| 178 | + expect(expandColumnsRow2.length).toBe(0); |
| 179 | + }); |
| 180 | + |
| 181 | + it('should not set rowspan on expand columns when there is only one header row with grouped showWhenGrouped columns', async () => { |
| 182 | + const { instance } = await createDataGrid({ |
| 183 | + dataSource: [ |
| 184 | + { |
| 185 | + field1: 'val1', field2: 'val2', field3: 'g1', field4: 'val4', |
| 186 | + }, |
| 187 | + ], |
| 188 | + columns: [ |
| 189 | + 'field1', |
| 190 | + 'field2', |
| 191 | + { dataField: 'field3', showWhenGrouped: true, groupIndex: 0 }, |
| 192 | + ], |
| 193 | + }); |
| 194 | + |
| 195 | + const columnsController = instance.getController('columns'); |
| 196 | + const rowCount = columnsController.getRowCount(); |
| 197 | + |
| 198 | + expect(rowCount).toBe(1); |
| 199 | + |
| 200 | + const visibleColumns = columnsController.getVisibleColumns(0); |
| 201 | + const expandColumn = visibleColumns.find((col) => col.command === 'expand' || col.type === 'groupExpand'); |
| 202 | + |
| 203 | + expect(expandColumn).toBeDefined(); |
| 204 | + expect(expandColumn.rowspan).toBeUndefined(); |
| 205 | + }); |
| 206 | + |
| 207 | + it('should not set rowspan on expand column when there is a single header row', async () => { |
| 208 | + const { instance } = await createDataGrid({ |
| 209 | + dataSource, |
| 210 | + columns: ['id', 'name'], |
| 211 | + masterDetail: { |
| 212 | + enabled: true, |
| 213 | + }, |
| 214 | + }); |
| 215 | + |
| 216 | + const columnsController = instance.getController('columns'); |
| 217 | + const rowCount = columnsController.getRowCount(); |
| 218 | + const firstRowColumns = columnsController.getVisibleColumns(0); |
| 219 | + |
| 220 | + const expandColumn = firstRowColumns.find((col) => col.command === 'expand'); |
| 221 | + |
| 222 | + expect(rowCount).toBe(1); |
| 223 | + expect(expandColumn).toBeDefined(); |
| 224 | + expect(expandColumn.rowspan).toBeUndefined(); |
| 225 | + }); |
| 226 | + }); |
| 227 | +}); |
0 commit comments