|
| 1 | +/** |
| 2 | + * Copyright 2026 Redpanda Data, Inc. |
| 3 | + * |
| 4 | + * Use of this software is governed by the Business Source License |
| 5 | + * included in the file https://github.com/redpanda-data/redpanda/blob/dev/licenses/bsl.md |
| 6 | + * |
| 7 | + * As of the Change Date specified in that file, in accordance with |
| 8 | + * the Business Source License, use of this software will be governed |
| 9 | + * by the Apache License, Version 2.0 |
| 10 | + */ |
| 11 | + |
| 12 | +import { type ColumnDef, getCoreRowModel, useReactTable } from '@tanstack/react-table'; |
| 13 | +import { render, screen, waitFor } from '@testing-library/react'; |
| 14 | +import userEvent from '@testing-library/user-event'; |
| 15 | + |
| 16 | +import { DataTableViewOptions } from './data-table'; |
| 17 | + |
| 18 | +type TestRow = { id: number; name: string; status: string }; |
| 19 | + |
| 20 | +const columns: ColumnDef<TestRow>[] = [ |
| 21 | + { accessorKey: 'name', header: 'Name' }, |
| 22 | + { accessorKey: 'status', header: 'Status' }, |
| 23 | +]; |
| 24 | + |
| 25 | +const data: TestRow[] = [ |
| 26 | + { id: 1, name: 'Alpha', status: 'active' }, |
| 27 | + { id: 2, name: 'Beta', status: 'inactive' }, |
| 28 | +]; |
| 29 | + |
| 30 | +function Harness() { |
| 31 | + const table = useReactTable({ |
| 32 | + data, |
| 33 | + columns, |
| 34 | + getCoreRowModel: getCoreRowModel(), |
| 35 | + }); |
| 36 | + |
| 37 | + return <DataTableViewOptions table={table} testId="view-options" />; |
| 38 | +} |
| 39 | + |
| 40 | +describe('DataTableViewOptions', () => { |
| 41 | + it('opens dropdown, toggles a column off, and reopens the dropdown', async () => { |
| 42 | + const user = userEvent.setup(); |
| 43 | + render(<Harness />); |
| 44 | + |
| 45 | + // 1. Click "View" to open the dropdown |
| 46 | + const viewButton = screen.getByRole('button', { name: /view/i }); |
| 47 | + await user.click(viewButton); |
| 48 | + |
| 49 | + // 2. Verify column checkboxes appear |
| 50 | + await waitFor(() => { |
| 51 | + expect(screen.getByText('Toggle columns')).toBeInTheDocument(); |
| 52 | + }); |
| 53 | + |
| 54 | + const nameCheckbox = screen.getByRole('menuitemcheckbox', { name: /name/i }); |
| 55 | + const statusCheckbox = screen.getByRole('menuitemcheckbox', { name: /status/i }); |
| 56 | + expect(nameCheckbox).toBeInTheDocument(); |
| 57 | + expect(statusCheckbox).toBeInTheDocument(); |
| 58 | + |
| 59 | + // Both columns should initially be checked (visible) |
| 60 | + expect(nameCheckbox).toHaveAttribute('data-state', 'checked'); |
| 61 | + expect(statusCheckbox).toHaveAttribute('data-state', 'checked'); |
| 62 | + |
| 63 | + // 3. Toggle "name" column off |
| 64 | + await user.click(nameCheckbox); |
| 65 | + |
| 66 | + // The dropdown closes after clicking a checkbox item; verify the column is now unchecked |
| 67 | + // by reopening the dropdown. |
| 68 | + |
| 69 | + // 4. Click "View" again - this is the regression scenario. |
| 70 | + // With the raw primitive trigger, this second click would fail to reopen. |
| 71 | + await user.click(viewButton); |
| 72 | + |
| 73 | + await waitFor(() => { |
| 74 | + expect(screen.getByText('Toggle columns')).toBeInTheDocument(); |
| 75 | + }); |
| 76 | + |
| 77 | + // "name" should now be unchecked, "status" should still be checked |
| 78 | + const nameCheckboxAfter = screen.getByRole('menuitemcheckbox', { name: /name/i }); |
| 79 | + const statusCheckboxAfter = screen.getByRole('menuitemcheckbox', { name: /status/i }); |
| 80 | + expect(nameCheckboxAfter).toHaveAttribute('data-state', 'unchecked'); |
| 81 | + expect(statusCheckboxAfter).toHaveAttribute('data-state', 'checked'); |
| 82 | + }); |
| 83 | +}); |
0 commit comments