Skip to content

Commit

Permalink
Merge pull request #3036 from andrewbaldwin44/bugfix/allow-empty-tables
Browse files Browse the repository at this point in the history
Allow Empty Tables when Filtering
  • Loading branch information
cyberw authored Jan 21, 2025
2 parents d205db7 + 57b1c48 commit 3ddcc1d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
29 changes: 22 additions & 7 deletions locust/webui/src/hooks/tests/useSortByField.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useState } from 'react';
import { act, render } from '@testing-library/react';
import { describe, expect, test } from 'vitest';

Expand All @@ -21,13 +22,12 @@ function MockHook({
hasTotalRow?: boolean;
defaultSortKey?: keyof (typeof mockStats)[0];
}) {
const { onTableHeadClick, sortedRows, currentSortField } = useSortByField(
hasTotalRow ? mockStatsWithTotalRow : mockStats,
{
hasTotalRow,
defaultSortKey,
},
);
const [stats, setStats] = useState(hasTotalRow ? mockStatsWithTotalRow : mockStats);

const { onTableHeadClick, sortedRows, currentSortField } = useSortByField(stats, {
hasTotalRow,
defaultSortKey,
});

return (
<div>
Expand All @@ -45,6 +45,9 @@ function MockHook({
>
Sort by numFailures
</button>
<button data-testid='emptyStats' onClick={() => setStats([])}>
Empty stats
</button>
<span data-testid='sortedStats'>{JSON.stringify(sortedRows)}</span>
<span data-testid='currentSortField'>{currentSortField}</span>
</div>
Expand Down Expand Up @@ -127,4 +130,16 @@ describe('useSortByField', () => {
expect(getByTestId('sortedStats').textContent).toBe(JSON.stringify(mockStats));
expect(getByTestId('currentSortField').textContent).toBe('');
});

test('should allow the sorted rows to be set to empty', () => {
const { getByTestId } = render(<MockHook />);

expect(getByTestId('sortedStats').textContent).toBe(JSON.stringify(mockStats));

act(() => {
getByTestId('emptyStats').click();
});

expect(getByTestId('sortedStats').textContent).toBe(JSON.stringify([]));
});
});
3 changes: 3 additions & 0 deletions locust/webui/src/hooks/useSortByField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ export default function useSortByField<Row>(
useEffect(() => {
if (rows.length) {
sortStats(currentSortField.current || defaultSortKey);
} else if (sortStats.length) {
// allows tables to be empty in the case of filtering
setSortedRows(rows);
}
}, [rows]);

Expand Down

0 comments on commit 3ddcc1d

Please sign in to comment.