|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | +import { act } from 'react' |
| 5 | +import { createRoot, type Root } from 'react-dom/client' |
| 6 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 7 | +import type { WorkflowGroup } from '@/lib/table' |
| 8 | +import type { DisplayColumn } from '@/app/workspace/[workspaceId]/tables/[tableId]/components/table-grid/types' |
| 9 | + |
| 10 | +vi.mock('@sim/emcn', () => ({ |
| 11 | + cn: (...values: Array<string | false | null | undefined>) => values.filter(Boolean).join(' '), |
| 12 | +})) |
| 13 | + |
| 14 | +vi.mock('@sim/emcn/icons', () => ({ |
| 15 | + ChevronDown: () => null, |
| 16 | +})) |
| 17 | + |
| 18 | +vi.mock( |
| 19 | + '@/app/workspace/[workspaceId]/tables/[tableId]/components/table-grid/headers/column-type-icon', |
| 20 | + () => ({ ColumnTypeIcon: () => null }) |
| 21 | +) |
| 22 | + |
| 23 | +vi.mock( |
| 24 | + '@/app/workspace/[workspaceId]/tables/[tableId]/components/table-grid/headers/header-label', |
| 25 | + () => ({ HeaderLabel: ({ label }: { label: string }) => <span>{label}</span> }) |
| 26 | +) |
| 27 | + |
| 28 | +vi.mock( |
| 29 | + '@/app/workspace/[workspaceId]/tables/[tableId]/components/table-grid/headers/workflow-group-meta-cell', |
| 30 | + () => ({ ColumnOptionsMenu: () => null }) |
| 31 | +) |
| 32 | + |
| 33 | +import { ColumnHeaderMenu } from '@/app/workspace/[workspaceId]/tables/[tableId]/components/table-grid/headers/column-header-menu' |
| 34 | + |
| 35 | +let container: HTMLDivElement |
| 36 | +let root: Root |
| 37 | + |
| 38 | +const DEFAULT_COLUMN: DisplayColumn = { |
| 39 | + id: 'col-name', |
| 40 | + key: 'col-name', |
| 41 | + name: 'Name', |
| 42 | + type: 'string', |
| 43 | + groupSize: 1, |
| 44 | + groupStartColIndex: 0, |
| 45 | + headerLabel: 'Name', |
| 46 | + isGroupStart: true, |
| 47 | +} |
| 48 | + |
| 49 | +beforeEach(() => { |
| 50 | + globalThis.IS_REACT_ACT_ENVIRONMENT = true |
| 51 | + container = document.createElement('div') |
| 52 | + document.body.appendChild(container) |
| 53 | + act(() => { |
| 54 | + root = createRoot(container) |
| 55 | + }) |
| 56 | +}) |
| 57 | + |
| 58 | +afterEach(() => { |
| 59 | + act(() => root.unmount()) |
| 60 | + container.remove() |
| 61 | +}) |
| 62 | + |
| 63 | +function renderHeader({ |
| 64 | + column = DEFAULT_COLUMN, |
| 65 | + workflowGroups, |
| 66 | + onColumnSelect = vi.fn(), |
| 67 | + onOpenConfig = vi.fn(), |
| 68 | + onRenameColumn = vi.fn(), |
| 69 | +}: { |
| 70 | + column?: DisplayColumn |
| 71 | + workflowGroups?: WorkflowGroup[] |
| 72 | + onColumnSelect?: (colIndex: number, shiftKey: boolean) => void |
| 73 | + onOpenConfig?: (columnName: string) => void |
| 74 | + onRenameColumn?: (columnName: string) => void |
| 75 | +} = {}) { |
| 76 | + act(() => { |
| 77 | + root.render( |
| 78 | + <table> |
| 79 | + <thead> |
| 80 | + <tr> |
| 81 | + <ColumnHeaderMenu |
| 82 | + column={column} |
| 83 | + colIndex={2} |
| 84 | + isRenaming={false} |
| 85 | + isColumnSelected={false} |
| 86 | + renameValue='' |
| 87 | + onRenameValueChange={vi.fn()} |
| 88 | + onRenameSubmit={vi.fn()} |
| 89 | + onRenameCancel={vi.fn()} |
| 90 | + onColumnSelect={onColumnSelect} |
| 91 | + onInsertLeft={vi.fn()} |
| 92 | + onInsertRight={vi.fn()} |
| 93 | + onRenameColumn={onRenameColumn} |
| 94 | + onDeleteColumn={vi.fn()} |
| 95 | + onResizeStart={vi.fn()} |
| 96 | + onResize={vi.fn()} |
| 97 | + onResizeEnd={vi.fn()} |
| 98 | + onAutoResize={vi.fn()} |
| 99 | + onOpenConfig={onOpenConfig} |
| 100 | + workflowGroups={workflowGroups} |
| 101 | + /> |
| 102 | + </tr> |
| 103 | + </thead> |
| 104 | + </table> |
| 105 | + ) |
| 106 | + }) |
| 107 | + |
| 108 | + const headerButton = Array.from(container.querySelectorAll('button')).find((button) => |
| 109 | + button.textContent?.includes(column.workflowGroupId ? column.headerLabel : column.name) |
| 110 | + ) |
| 111 | + if (!headerButton) throw new Error('Column header button was not rendered') |
| 112 | + return headerButton |
| 113 | +} |
| 114 | + |
| 115 | +describe('ColumnHeaderMenu interactions', () => { |
| 116 | + it('selects the column without opening configuration on a single click', () => { |
| 117 | + const onColumnSelect = vi.fn() |
| 118 | + const onOpenConfig = vi.fn() |
| 119 | + const onRenameColumn = vi.fn() |
| 120 | + const headerButton = renderHeader({ onColumnSelect, onOpenConfig, onRenameColumn }) |
| 121 | + |
| 122 | + act(() => headerButton.click()) |
| 123 | + |
| 124 | + expect(onColumnSelect).toHaveBeenCalledWith(2, false) |
| 125 | + expect(onOpenConfig).not.toHaveBeenCalled() |
| 126 | + expect(onRenameColumn).not.toHaveBeenCalled() |
| 127 | + }) |
| 128 | + |
| 129 | + it('selects before starting inline rename on a double click', () => { |
| 130 | + const onColumnSelect = vi.fn() |
| 131 | + const onRenameColumn = vi.fn() |
| 132 | + const headerButton = renderHeader({ onColumnSelect, onRenameColumn }) |
| 133 | + |
| 134 | + act(() => { |
| 135 | + headerButton.click() |
| 136 | + headerButton.click() |
| 137 | + headerButton.dispatchEvent(new MouseEvent('dblclick', { bubbles: true })) |
| 138 | + }) |
| 139 | + |
| 140 | + expect(onColumnSelect).toHaveBeenCalledTimes(2) |
| 141 | + expect(onRenameColumn).toHaveBeenCalledWith('col-name') |
| 142 | + }) |
| 143 | + |
| 144 | + it('does not rename a workflow-output column on double click', () => { |
| 145 | + const onRenameColumn = vi.fn() |
| 146 | + const headerButton = renderHeader({ |
| 147 | + column: { ...DEFAULT_COLUMN, workflowGroupId: 'workflow-group' }, |
| 148 | + workflowGroups: [ |
| 149 | + { |
| 150 | + id: 'workflow-group', |
| 151 | + workflowId: 'workflow-1', |
| 152 | + type: 'manual', |
| 153 | + outputs: [{ blockId: 'block-1', path: 'result', columnName: 'col-name' }], |
| 154 | + }, |
| 155 | + ], |
| 156 | + onRenameColumn, |
| 157 | + }) |
| 158 | + |
| 159 | + act(() => { |
| 160 | + headerButton.dispatchEvent(new MouseEvent('dblclick', { bubbles: true })) |
| 161 | + }) |
| 162 | + |
| 163 | + expect(onRenameColumn).not.toHaveBeenCalled() |
| 164 | + }) |
| 165 | + |
| 166 | + it('renames an enrichment column on double click', () => { |
| 167 | + const onRenameColumn = vi.fn() |
| 168 | + const headerButton = renderHeader({ |
| 169 | + column: { ...DEFAULT_COLUMN, workflowGroupId: 'enrichment-group' }, |
| 170 | + workflowGroups: [ |
| 171 | + { |
| 172 | + id: 'enrichment-group', |
| 173 | + workflowId: '', |
| 174 | + enrichmentId: 'company-domain', |
| 175 | + type: 'enrichment', |
| 176 | + outputs: [{ blockId: '', path: '', outputId: 'domain', columnName: 'col-name' }], |
| 177 | + }, |
| 178 | + ], |
| 179 | + onRenameColumn, |
| 180 | + }) |
| 181 | + |
| 182 | + act(() => { |
| 183 | + headerButton.dispatchEvent(new MouseEvent('dblclick', { bubbles: true })) |
| 184 | + }) |
| 185 | + |
| 186 | + expect(onRenameColumn).toHaveBeenCalledWith('col-name') |
| 187 | + }) |
| 188 | +}) |
0 commit comments