Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -206,3 +206,67 @@ test('Bulk selection should work with pagination', () => {
const checkboxes = screen.getAllByRole('checkbox');
expect(checkboxes.length).toBeGreaterThan(0);
});

test('handleTableChange should convert array field to dot notation for nested fields', () => {
const setSortBy = jest.fn();
const sortingProps = {
...defaultProps,
setSortBy,
};

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

// Get the Table component instance
const table = component.container.querySelector('.ant-table');

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use roles wherever possible? See wiki

expect(table).toBeInTheDocument();

// Simulate the handleTableChange function behavior directly
// This tests the logic without requiring actual table interaction
const mockSorter = {
field: ['database', 'database_name'], // Array format from AntD
order: 'descend',
};

// Get the component instance to test the callback logic
Comment thread
rebenitez1802 marked this conversation as resolved.
Outdated
const handleTableChange = (_pagination: any, _filters: any, sorter: any) => {
Comment thread
rebenitez1802 marked this conversation as resolved.
Outdated
if (sorter && sorter.field) {
// This is the logic we implemented in the fix
const fieldId = Array.isArray(sorter.field)
? sorter.field.join('.')
: sorter.field;

setSortBy([
{
id: fieldId,
desc: sorter.order === 'descend',
},
]);
}
};

// Test the callback logic with array field
handleTableChange(null, null, mockSorter);

expect(setSortBy).toHaveBeenCalledWith([
{
id: 'database.database_name', // Should be converted to dot notation
desc: true,
},
]);

// Test with string field (should pass through unchanged)
setSortBy.mockClear();
const mockStringSorter = {
field: 'table_name', // String format
order: 'ascend',
};

handleTableChange(null, null, mockStringSorter);

expect(setSortBy).toHaveBeenCalledWith([
{
id: 'table_name', // Should remain as string
desc: false,
},
]);
});
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