fix(table): use controlled sorting in table test (#5904)

This commit is contained in:
Hayato Hasegawa 2025-11-23 02:16:09 +09:00 committed by GitHub
parent 5d9a05be01
commit 8b2ec9fa46
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -256,32 +256,49 @@ describe("Table", () => {
});
it("should set the proper aria-sort on an ascending sorted column header", async () => {
const wrapper = render(
<Table aria-label="Static Table">
<TableHeader>
<TableColumn allowsSorting data-testid="test-sort-column">
Foo
</TableColumn>
<TableColumn>Bar</TableColumn>
<TableColumn>Baz</TableColumn>
</TableHeader>
<TableBody>
<TableRow>
<TableCell>Foo 1</TableCell>
<TableCell>Bar 1</TableCell>
<TableCell>Baz 1</TableCell>
</TableRow>
</TableBody>
</Table>,
);
const TestTable = () => {
const [sortDescriptor, setSortDescriptor] = React.useState<
| {
column: React.Key;
direction: "ascending" | "descending";
}
| undefined
>(undefined);
return (
<Table
aria-label="Static Table"
sortDescriptor={sortDescriptor}
onSortChange={(descriptor) => setSortDescriptor(descriptor)}
>
<TableHeader>
<TableColumn allowsSorting data-testid="test-sort-column">
Foo
</TableColumn>
<TableColumn>Bar</TableColumn>
<TableColumn>Baz</TableColumn>
</TableHeader>
<TableBody>
<TableRow>
<TableCell>Foo 1</TableCell>
<TableCell>Bar 1</TableCell>
<TableCell>Baz 1</TableCell>
</TableRow>
</TableBody>
</Table>
);
};
const wrapper = render(<TestTable />);
const column = wrapper.getByTestId("test-sort-column");
expect(column).toHaveAttribute("aria-sort", "none");
act(async () => {
await userEvent.click(column);
expect(column).toHaveAttribute("aria-sort", "ascending");
await act(async () => {
await user.click(column);
});
expect(column).toHaveAttribute("aria-sort", "ascending");
});
});