Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
import { render, screen } from '@superset-ui/core/spec';
import { render, screen, fireEvent } from '@superset-ui/core/spec';
import { renderHook } from '@testing-library/react-hooks';
import { TableInstance, useTable } from 'react-table';
import TableCollection from '.';
Expand Down Expand Up @@ -206,3 +206,35 @@ test('Bulk selection should work with pagination', () => {
const checkboxes = screen.getAllByRole('checkbox');
expect(checkboxes.length).toBeGreaterThan(0);
});

test('should call setSortBy when clicking sortable column header', () => {
const setSortBy = jest.fn();
const sortingProps = {
...defaultProps,
setSortBy,
};

render(<TableCollection {...sortingProps} />);

const columnHeaders = screen.getAllByRole('columnheader');
expect(columnHeaders.length).toBeGreaterThan(0);

const firstColumnHeader = columnHeaders[0];
expect(firstColumnHeader).toBeInTheDocument();

// Click on the column header to trigger sorting
fireEvent.click(firstColumnHeader);
Comment thread
rebenitez1802 marked this conversation as resolved.
Outdated

// Verify setSortBy was called immediately
expect(setSortBy).toHaveBeenCalled();

const sortCallArgs = setSortBy.mock.calls[0][0];
expect(Array.isArray(sortCallArgs)).toBe(true);
expect(sortCallArgs[0]).toHaveProperty('id');
expect(sortCallArgs[0]).toHaveProperty('desc');

// Verify it was called with a valid column ID (any string) and boolean desc
expect(typeof sortCallArgs[0].id).toBe('string');
expect(sortCallArgs[0].id.length).toBeGreaterThan(0);
expect(typeof sortCallArgs[0].desc).toBe('boolean');
});
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,14 @@ function TableCollection<T extends object>({
const handleTableChange = useCallback(
(_pagination: any, _filters: any, sorter: SorterResult) => {
if (sorter && sorter.field) {
// Convert array field back to dot notation for nested fields
const fieldId = Array.isArray(sorter.field)
? sorter.field.join('.')
: sorter.field;

setSortBy?.([
{
id: sorter.field,
id: fieldId,
desc: sorter.order === 'descend',
},
] as SortingRule<T>[]);
Expand Down
Loading