Skip to content

Commit 1012f2f

Browse files
fix: made show all columns work correctly (calcom#25106)
* fix: made show all columns work correctly * test: added test for show all columns in ColumnVisibilityButton --------- Co-authored-by: Pallav <90088723+Pallava-Joshi@users.noreply.github.com>
1 parent d6f6be7 commit 1012f2f

2 files changed

Lines changed: 56 additions & 1 deletion

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { useEffect, useMemo } from "react";
2+
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
3+
import { vi } from "vitest";
4+
import { useReactTable, getCoreRowModel, type Table, type Column } from "@tanstack/react-table";
5+
6+
import { ColumnVisibilityButton } from "./ColumnVisibilityButton";
7+
8+
let tableRef: Table<{ a: number; b: number; c: number }> | null = null;
9+
10+
const TestWrapper = () => {
11+
const data = useMemo(() => [{ a: 1, b: 2, c: 3 }], []);
12+
const columns = useMemo(
13+
() => [
14+
{ accessorKey: "a", header: "A" },
15+
{ accessorKey: "b", header: "B", enableHiding: true },
16+
{ accessorKey: "c", header: "C", enableHiding: true },
17+
],
18+
[]
19+
);
20+
21+
const table = useReactTable({
22+
data,
23+
columns,
24+
getCoreRowModel: getCoreRowModel(),
25+
initialState: { columnVisibility: { b: false, c: false } },
26+
});
27+
28+
useEffect(() => {
29+
tableRef = table;
30+
}, [table]);
31+
32+
return <ColumnVisibilityButton table={table} />;
33+
}
34+
35+
describe("ColumnVisibilityButton", () => {
36+
beforeEach(() => {
37+
vi.clearAllMocks();
38+
tableRef = null;
39+
});
40+
41+
it("show_all_columns sets all columns visible", async () => {
42+
render(<TestWrapper />);
43+
44+
fireEvent.click(screen.getByText("display"));
45+
46+
const showAll = await screen.findByText("show_all_columns");
47+
fireEvent.click(showAll);
48+
49+
await waitFor(() => {
50+
expect(tableRef).not.toBeNull();
51+
const allVisible = tableRef!.getAllLeafColumns().every((col: Column<{ a: number; b: number; c: number }, unknown>) => col.getIsVisible());
52+
expect(allVisible).toBe(true);
53+
});
54+
});
55+
});

packages/features/data-table/components/filters/ColumnVisibilityButton.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function ColumnVisibilityButtonComponent<TData>(
7878
<CommandGroup>
7979
<CommandItem
8080
onSelect={() => {
81-
allColumns.forEach((column) => column.toggleVisibility(true));
81+
table.toggleAllColumnsVisible(true);
8282
}}
8383
className={classNames(
8484
buttonClasses({ color: "secondary" }),

0 commit comments

Comments
 (0)