Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
73 changes: 73 additions & 0 deletions client/src/webpages/dashboard/components/table/Table.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { GQLRuleStatus } from '@/graphql/generated';
import { fireEvent, render, screen, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { ReactNode } from 'react';
import { MemoryRouter } from 'react-router-dom';
import { describe, expect, it, vi } from 'vitest';
Expand Down Expand Up @@ -102,6 +103,78 @@ describe('Table behavior', () => {
).toBeTruthy();
});

it('keeps placeholder headers inert while grouped sortable headers stay accessible', () => {
const groupedColumns = [
columns[0],
{
header: 'Details',
columns: [columns[1]],
},
] satisfies TableColumnDef<TableRow>[];
renderTable(groupedColumns);

const placeholderHeader = screen
.getAllByRole('columnheader')
.find((header) => header.textContent === '');
expect(placeholderHeader).toBeTruthy();
expect(within(placeholderHeader!).queryByRole('button')).toBeNull();
expect(placeholderHeader!.hasAttribute('aria-sort')).toBe(false);

const nameHeader = screen.getByRole('columnheader', { name: /Name/ });
const nameSortButton = within(nameHeader).getByRole('button', {
name: /Name/,
});
expect(nameHeader.getAttribute('aria-sort')).toBe('none');

nameSortButton.focus();
userEvent.type(nameSortButton, '{enter}', { skipClick: true });
expect(renderedNames()).toEqual([
'Rendered Alpha',
'Rendered Alpine',
'Rendered Zulu',
]);
});

it('makes sortable headers keyboard accessible and reports their sort direction', () => {
renderTable();

const nameHeader = screen.getByRole('columnheader', { name: /Name/ });
const nameSortButton = within(nameHeader).getByRole('button', {
name: /Name/,
});
const statusHeader = screen.getByRole('columnheader', { name: 'Status' });

expect(nameHeader.getAttribute('aria-sort')).toBe('none');
expect(within(statusHeader).queryByRole('button')).toBeNull();
expect(statusHeader.hasAttribute('aria-sort')).toBe(false);

nameSortButton.focus();
expect(document.activeElement).toBe(nameSortButton);
userEvent.type(nameSortButton, '{enter}', { skipClick: true });
expect(renderedNames()).toEqual([
'Rendered Alpha',
'Rendered Alpine',
'Rendered Zulu',
]);
expect(
screen
.getByRole('columnheader', { name: /Name/ })
.getAttribute('aria-sort'),
).toBe('ascending');

userEvent.type(nameSortButton, '{space}', { skipClick: true });
expect(renderedNames()).toEqual([
'Rendered Zulu',
'Rendered Alpine',
'Rendered Alpha',
]);
expect(
screen
.getByRole('columnheader', { name: /Name/ })
.getAttribute('aria-sort'),
).toBe('descending');
});

it('renders accessor values and sorts by raw values only on sortable headers', () => {
renderTable();

Expand Down
62 changes: 44 additions & 18 deletions client/src/webpages/dashboard/components/table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,41 @@ export default function Table<TData extends Record<string, any>>(
) : (
headerGroup.headers.map((header, index) => {
const sorted = header.column.getIsSorted();
const isSortableHeader =
!header.isPlaceholder && header.column.getCanSort();
const headerContent = header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext(),
);
const sortIcon = isSortableHeader ? (
sorted === 'desc' ? (
<SortAmountDsc className="bg-[#40ace920] w-6 p-1 fill-primary rounded-full" />
) : sorted === 'asc' ? (
<SortAmountAsc className="bg-[#40ace920] w-6 p-1 fill-primary rounded-full scale-y-[-1]" />
) : (
<SortAmountDsc className="w-4 rounded-full fill-gray-500" />
)
) : null;
return (
<th
key={header.id}
colSpan={header.colSpan}
onClick={header.column.getToggleSortingHandler()}
onClick={
isSortableHeader
? header.column.getToggleSortingHandler()
: undefined
}
aria-sort={
isSortableHeader
? sorted === 'asc'
? 'ascending'
: sorted === 'desc'
? 'descending'
: 'none'
: undefined
}
className={`align-center font-bold text-gray-500 text-start text-base !p-0 ${
index === 0
? 'rounded-tl-md'
Expand All @@ -121,23 +151,19 @@ export default function Table<TData extends Record<string, any>>(
: ''
}`}
>
<div className="flex flex-row items-center p-4 flex-nowrap whitespace-nowrap gap-3">
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext(),
)}
{header.column.getCanSort() ? (
sorted === 'desc' ? (
<SortAmountDsc className="bg-[#40ace920] w-6 p-1 fill-primary rounded-full" />
) : sorted === 'asc' ? (
<SortAmountAsc className="bg-[#40ace920] w-6 p-1 fill-primary rounded-full scale-y-[-1]" />
) : (
<SortAmountDsc className="w-4 rounded-full fill-gray-500" />
)
) : null}
</div>
{isSortableHeader ? (
<button
type="button"
className="flex flex-row items-center p-4 flex-nowrap whitespace-nowrap gap-3"
>
{headerContent}
{sortIcon}
</button>
) : (
<div className="flex flex-row items-center p-4 flex-nowrap whitespace-nowrap gap-3">
{headerContent}
</div>
)}
</th>
);
})
Expand Down
Loading