Skip to content

Commit

Permalink
reset selection when resetting all columns (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
phndiaye authored Apr 15, 2024
1 parent f3af460 commit 888947c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
1 change: 1 addition & 0 deletions addon/components/hyper-table/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ export default Component.extend({

resetAllFilters() {
this._columns.setEach('filters', []);
this.manager.resetSelection();

if (this.manager.hooks.onColumnsChange) {
this.manager.hooks.onColumnsChange('columns:change');
Expand Down
6 changes: 5 additions & 1 deletion addon/core/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ export default class TableHandler {
/**
* Reset columns' filters and order attributes
*
* @param {Column[]} columns — The columns we want to reset the state for.
* @param {Column[]} columnsToReset — The columns we want to reset the state for.
* @returns {Promise<void>}
*/
async resetColumns(columnsToReset: Column[]): Promise<void> {
Expand All @@ -303,6 +303,10 @@ export default class TableHandler {
set(column, 'order', undefined);
});

if (this.columns.length === columnsToReset.length) {
this.clearSelection();
}

return this.tableManager.upsertColumns({ columns: this.columns }).then(({ columns }) => {
this._reinitColumnsAndRows(columns);
this.triggerEvent('reset-columns', columnsToReset);
Expand Down
52 changes: 43 additions & 9 deletions tests/unit/core/handler-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,18 +184,52 @@ module('Unit | core/handler', function (hooks) {
});
});

test('Handler#resetColumns', async function (assert: Assert) {
const handler = new TableHandler(getContext(), this.tableManager, this.rowsFetcher);
await handler.fetchColumns();
module('Handler#resetColumns', function () {
test('the columns filters are order are properly reset', async function (assert: Assert) {
const handler = new TableHandler(getContext(), this.tableManager, this.rowsFetcher);
await handler.fetchColumns();

handler.applyFilters(handler.columns[0], [{ key: 'foo', value: 'bar' }]);
handler.applyOrder(handler.columns[1], 'asc');

handler.resetColumns(handler.columns);

assert.equal(handler.columns.filter((column) => column.filters.length > 0 || column.order).length, 0);
assert.equal(handler.columns[0].filters.length, 0);
assert.equal(handler.columns[1].order, undefined);
});

test('if all items where globally selected, the selection is properly reset', async function (assert: Assert) {
const handler = new TableHandler(getContext(), this.tableManager, this.rowsFetcher);
await handler.fetchColumns();
await handler.fetchRows();

handler.applyFilters(handler.columns[0], [{ key: 'foo', value: 'bar' }]);
handler.applyOrder(handler.columns[1], 'asc');
handler.selectAllGlobal();

assert.equal(handler.selection, 'all');

await handler.resetColumns(handler.columns);

assert.deepEqual(handler.selection, []);
});

test('if a precise selection where done, the selection is properly reset', async function (assert: Assert) {
const handler = new TableHandler(getContext(), this.tableManager, this.rowsFetcher);
await handler.fetchColumns();
await handler.fetchRows();

handler.applyFilters(handler.columns[0], [{ key: 'foo', value: 'bar' }]);
handler.applyOrder(handler.columns[1], 'asc');
handler.toggleSelectAll(true);

handler.applyFilters(handler.columns[0], [{ key: 'foo', value: 'bar' }]);
handler.applyOrder(handler.columns[1], 'asc');
assert.equal(handler.selection.length, 2);

handler.resetColumns(handler.columns);
handler.resetColumns(handler.columns);

assert.equal(handler.columns.filter((column) => column.filters.length > 0 || column.order).length, 0);
assert.equal(handler.columns[0].filters.length, 0);
assert.equal(handler.columns[1].order, undefined);
assert.equal(handler.selection.length, 0);
});
});

test('Handler#resetRows', async function (assert: Assert) {
Expand Down

0 comments on commit 888947c

Please sign in to comment.