fix(editor): restore multi-column sort with one call, not a loop
Build / Build-and-ng-test (pull_request) Successful in 5m23s
Lighthouse Checks / lighthouse (pull_request) Successful in 22m1s
Build / Build-and-test-development (pull_request) Successful in 25m4s

multiColumnSorting's sort() replaces the entire sort state on every call rather than accumulating, so the restore loop after updateSettings()/editTable()/cancelEdit() silently dropped every sorted column but the last. Pass the captured configs as a single array instead. Adds a Karma integration test proving the loop
loses columns, and a Cypress test verifying cancelEdit() keeps a real shift-click multi-column sort intact end-to-end.
This commit is contained in:
YuryShkoda committed 2026-09-03 12:46:31 +03:00
1 parent 4137ebdf6b
commit 8fafeb7923
3 files changed
+149 -10

No files matched your search

+62
View File
@@ -1850,6 +1850,55 @@ context('editor tests: ', function () {
})
})
})
// multiColumnSorting's own sort() replaces its entire sort state on every
// call rather than accumulating (see its own JSDoc) - restoring a
// captured multi-column sort with a loop of single-column sort() calls
// therefore silently drops every column but the last one restored. This
// hits cancelEdit() specifically once a user has shift-clicked a second
// header to sort by two columns, then clicks Cancel: the underlying bug
// is already covered directly against the Handsontable plugin in
// sortedGridRowSync.integration.spec.ts, but only a real shift-click
// through the actual grid proves editor.component.ts's own cancelEdit()
// restores it correctly end-to-end.
it('54 | Cancelling an edit after sorting by two columns keeps both columns sorted, not just the last one clicked', () => {
openTableFromTree('testdata', 'mpe_x_formula_test')
clickOnEdit(() => {
cy.get('.btn.btn-sm.btn-icon.btn-outline-danger', {
timeout: longerCommandTimeout
}).then(() => {
sortByColumn('A_COL')
sortByColumnAdditional('B_COL')
cy.get('.ht_clone_top .htCore thead tr th').then(($ths) => {
const pkColIndex = [...$ths].findIndex(
(th) => th.innerText.trim() === 'PRIMARY_KEY_FIELD'
)
cy.get('.ht_master tbody tr').then((rows: any) => {
const beforeCancelPkOrder = [...rows].map((row: any) =>
row.childNodes[pkColIndex].innerText.trim()
)
cy.get('.btn.btn-sm.btn-icon.btn-outline-danger', {
timeout: longerCommandTimeout
}).click()
// cancelEdit() re-renders read-only with the same dataSource,
// still ordered by the restored sort - the row order itself is
// the only observable signal that both columns' sort survived.
cy.get('.ht_master tbody tr').should((rows2: any) => {
const afterCancelPkOrder = [...rows2].map((row: any) =>
row.childNodes[pkColIndex].innerText.trim()
)
expect(afterCancelPkOrder).to.deep.equal(beforeCancelPkOrder)
})
})
})
})
})
})
})
// Handsontable virtualizes columns — with 18 columns on MPE_X_NEW, only
@@ -1879,6 +1928,19 @@ const sortByColumn = (headerText: string) => {
.click({ force: true })
}
// Shift-clicks a column header's sort indicator to ADD it to whatever sort
// is already active, rather than replacing it - Handsontable's own
// multiColumnSorting UI convention for building a multi-column sort (a
// plain click, sortByColumn above, always starts a fresh single-column
// sort instead).
const sortByColumnAdditional = (headerText: string) => {
cy.get('.ht_clone_top .htCore thead tr th')
.filter((_, th) => Cypress.$(th).text().includes(headerText))
.last()
.find('.columnSorting')
.click({ force: true, shiftKey: true })
}
// Opens a column header's dropdown menu to reach its `info` item, which
// has a custom renderer showing NAME/LABEL/TYPE/LENGTH/FORMAT and, when the
// column has a HARDREGEX/SOFTREGEX rule, the applied pattern. Clicking the
+15 -9
View File
@@ -1241,9 +1241,11 @@ export class EditorComponent implements OnInit, AfterViewInit, OnDestroy {
hot.updateSettings(settings, render)
for (const sortConfig of sortConfigs) {
columnSorting.sort(sortConfig)
}
// sort() replaces the entire sort state on every call rather than
// accumulating (see its own JSDoc) - restoring a multi-column sort
// needs one call with the whole array, not a loop of single-column
// calls, or every column but the last silently loses its sort.
if (sortConfigs.length > 0) columnSorting.sort(sortConfigs)
}
editTable(previewEdit?: boolean, newRow?: boolean) {
@@ -1302,9 +1304,11 @@ export class EditorComponent implements OnInit, AfterViewInit, OnDestroy {
// same as cancelEdit() does for the read-only return path.
this.syncOverwrittenComments()
for (const sortConfig of sortConfigs) {
columnSorting.sort(sortConfig)
}
// sort() replaces the entire sort state on every call rather than
// accumulating (see its own JSDoc) - a multi-column sort needs one
// call with the whole array, not a loop of single-column calls, or
// every column but the last silently loses its sort.
if (sortConfigs.length > 0) columnSorting.sort(sortConfigs)
this.reSetCellValidationValues()
@@ -1415,9 +1419,11 @@ export class EditorComponent implements OnInit, AfterViewInit, OnDestroy {
this.modifedRowsIndexes = []
hot.validateCells()
// this.editRecordListeners();
for (const sortConfig of sortConfigs) {
columnSorting.sort(sortConfig)
}
// sort() replaces the entire sort state on every call rather than
// accumulating (see its own JSDoc) - a multi-column sort needs one
// call with the whole array, not a loop of single-column calls, or
// every column but the last silently loses its sort.
if (sortConfigs.length > 0) columnSorting.sort(sortConfigs)
this.checkRowLimit()
@@ -359,7 +359,7 @@ describe('updateSettings must not run while a sort is active - it corrupts formu
sortPlugin.clearSort()
hot.updateSettings({}, false)
hot.render()
for (const sc of sortConfigs) sortPlugin.sort(sc)
if (sortConfigs.length > 0) sortPlugin.sort(sortConfigs)
hot.render()
expect(formulaHardColValues(hot)).not.toContain('#REF!')
@@ -368,6 +368,77 @@ describe('updateSettings must not run while a sort is active - it corrupts formu
})
})
/**
* multiColumnSorting's own JSDoc: "every call of `sort` function set an
* entirely new sort order. Previous sort configs aren't preserved." A loop
* calling sort() once per captured config - the shape editor.component.ts
* used before this fix, in updateSettingsSortSafe/editTable/cancelEdit's
* restore step - therefore doesn't accumulate a multi-column sort: each
* call replaces the last, so only the FINAL config in the array survives.
* Restoring a captured multi-column sort must pass the whole array to a
* single sort() call instead.
*/
describe('restoring a multi-column sort must apply as a single call, not a loop', () => {
const columnNames = ['PRIMARY_KEY_FIELD', 'GROUP_COL', 'VALUE_COL']
const buildGrid = (): Handsontable => {
const data = [
{ PRIMARY_KEY_FIELD: 1, GROUP_COL: 'B', VALUE_COL: 2 },
{ PRIMARY_KEY_FIELD: 2, GROUP_COL: 'A', VALUE_COL: 1 },
{ PRIMARY_KEY_FIELD: 3, GROUP_COL: 'A', VALUE_COL: 3 },
{ PRIMARY_KEY_FIELD: 4, GROUP_COL: 'B', VALUE_COL: 4 }
]
const hot = new Handsontable(document.createElement('div'), {
data,
columns: columnNames.map((c) => ({ data: c })),
multiColumnSorting: true,
licenseKey: 'non-commercial-and-evaluation'
})
hot.render()
return hot
}
// Sort by GROUP_COL asc, then VALUE_COL desc within each group.
const sortConfigs = [
{ column: 1, sortOrder: 'asc' },
{ column: 2, sortOrder: 'desc' }
]
const visiblePkOrder = (hot: Handsontable): number[] =>
Array.from(
{ length: 4 },
(_, r) => hot.getDataAtRowProp(r, 'PRIMARY_KEY_FIELD') as number
)
it('demonstrates the failure mode: looping sort() per column loses every column but the last', () => {
const hot = buildGrid()
const sortPlugin: any = hot.getPlugin('multiColumnSorting')
for (const sc of sortConfigs) sortPlugin.sort(sc)
hot.render()
// GROUP_COL asc got wiped out by the second call - only VALUE_COL desc
// (the last config) survived, sorting the whole table by value alone.
expect(visiblePkOrder(hot)).toEqual([4, 3, 1, 2])
hot.destroy()
})
it('applying the full config array in one call restores the actual multi-column order', () => {
const hot = buildGrid()
const sortPlugin: any = hot.getPlugin('multiColumnSorting')
sortPlugin.sort(sortConfigs)
hot.render()
// Group A first (value desc: 3, 1), then group B (value desc: 4, 2).
expect(visiblePkOrder(hot)).toEqual([3, 2, 4, 1])
hot.destroy()
})
})
/**
* getFormulaCellsToPreserveOnCancel's rowIndex parameter is established as
* PHYSICAL (it iterates 0..dataSource.length, matching dataSource's own