|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | +import { act, type ReactNode } from 'react' |
| 5 | +import { createRoot, type Root } from 'react-dom/client' |
| 6 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 7 | +import type { ColumnDefinition } from '@/lib/table' |
| 8 | + |
| 9 | +vi.mock('@sim/emcn', () => ({ |
| 10 | + cn: (...values: Array<string | false | null | undefined>) => values.filter(Boolean).join(' '), |
| 11 | + DropdownMenu: ({ children, open }: { children: ReactNode; open: boolean }) => |
| 12 | + open ? <>{children}</> : null, |
| 13 | + DropdownMenuContent: ({ children }: { children: ReactNode }) => <div>{children}</div>, |
| 14 | + DropdownMenuItem: ({ children, onSelect }: { children: ReactNode; onSelect?: () => void }) => ( |
| 15 | + <button type='button' onClick={onSelect}> |
| 16 | + {children} |
| 17 | + </button> |
| 18 | + ), |
| 19 | + DropdownMenuSeparator: () => <hr />, |
| 20 | + DropdownMenuSub: ({ children }: { children: ReactNode }) => <>{children}</>, |
| 21 | + DropdownMenuSubContent: ({ children }: { children: ReactNode }) => <>{children}</>, |
| 22 | + DropdownMenuSubTrigger: ({ children }: { children: ReactNode }) => <span>{children}</span>, |
| 23 | + DropdownMenuTrigger: ({ children }: { children: ReactNode }) => <>{children}</>, |
| 24 | +})) |
| 25 | + |
| 26 | +vi.mock('@sim/emcn/icons', () => ({ |
| 27 | + ArrowDown: () => null, |
| 28 | + ArrowLeft: () => null, |
| 29 | + ArrowRight: () => null, |
| 30 | + ArrowUp: () => null, |
| 31 | + Eye: () => null, |
| 32 | + EyeOff: () => null, |
| 33 | + Fingerprint: () => null, |
| 34 | + Pencil: () => null, |
| 35 | + Pin: () => null, |
| 36 | + PinOff: () => null, |
| 37 | + PlayOutline: () => null, |
| 38 | + Settings: () => null, |
| 39 | + SquareArrowUpRight: () => null, |
| 40 | + Trash: () => null, |
| 41 | + Workflow: () => null, |
| 42 | + X: () => null, |
| 43 | +})) |
| 44 | + |
| 45 | +vi.mock('@/lib/table/column-types', () => ({ |
| 46 | + columnTypeOf: (column: ColumnDefinition) => ({ |
| 47 | + icon: () => null, |
| 48 | + label: column.type === 'reference' ? 'Reference' : 'Text', |
| 49 | + hasConfiguration: column.type === 'reference', |
| 50 | + }), |
| 51 | +})) |
| 52 | + |
| 53 | +vi.mock('@/app/workspace/[workspaceId]/tables/[tableId]/components/column-config-sidebar', () => ({ |
| 54 | + PLAIN_COLUMN_TYPE_OPTIONS: [], |
| 55 | +})) |
| 56 | + |
| 57 | +vi.mock('@/enrichments/registry', () => ({ getEnrichment: () => undefined })) |
| 58 | + |
| 59 | +import { ColumnOptionsMenu } from '@/app/workspace/[workspaceId]/tables/[tableId]/components/table-grid/headers/workflow-group-meta-cell' |
| 60 | + |
| 61 | +let container: HTMLDivElement |
| 62 | +let root: Root |
| 63 | + |
| 64 | +beforeEach(() => { |
| 65 | + globalThis.IS_REACT_ACT_ENVIRONMENT = true |
| 66 | + container = document.createElement('div') |
| 67 | + document.body.appendChild(container) |
| 68 | + act(() => { |
| 69 | + root = createRoot(container) |
| 70 | + }) |
| 71 | +}) |
| 72 | + |
| 73 | +afterEach(() => { |
| 74 | + act(() => root.unmount()) |
| 75 | + container.remove() |
| 76 | +}) |
| 77 | + |
| 78 | +function renderMenu(column: ColumnDefinition, onGoToReferenceTable: (tableId: string) => void) { |
| 79 | + act(() => { |
| 80 | + root.render( |
| 81 | + <ColumnOptionsMenu |
| 82 | + open |
| 83 | + onOpenChange={vi.fn()} |
| 84 | + position={{ x: 0, y: 0 }} |
| 85 | + column={{ |
| 86 | + ...column, |
| 87 | + key: column.id ?? column.name, |
| 88 | + groupSize: 1, |
| 89 | + groupStartColIndex: 0, |
| 90 | + headerLabel: column.name, |
| 91 | + isGroupStart: true, |
| 92 | + }} |
| 93 | + onInsertLeft={vi.fn()} |
| 94 | + onInsertRight={vi.fn()} |
| 95 | + onDeleteColumn={vi.fn()} |
| 96 | + onGoToReferenceTable={onGoToReferenceTable} |
| 97 | + /> |
| 98 | + ) |
| 99 | + }) |
| 100 | +} |
| 101 | + |
| 102 | +function findButton(label: string): HTMLButtonElement | undefined { |
| 103 | + return Array.from(container.querySelectorAll('button')).find( |
| 104 | + (button) => button.textContent?.trim() === label |
| 105 | + ) |
| 106 | +} |
| 107 | + |
| 108 | +describe('ColumnOptionsMenu Reference navigation', () => { |
| 109 | + it('opens the table targeted by a Reference column', () => { |
| 110 | + const onGoToReferenceTable = vi.fn() |
| 111 | + renderMenu( |
| 112 | + { |
| 113 | + id: 'col-account', |
| 114 | + name: 'Account', |
| 115 | + type: 'reference', |
| 116 | + referenceTableId: 'table-accounts', |
| 117 | + }, |
| 118 | + onGoToReferenceTable |
| 119 | + ) |
| 120 | + |
| 121 | + act(() => findButton('Go to Reference Table')?.click()) |
| 122 | + |
| 123 | + expect(onGoToReferenceTable).toHaveBeenCalledWith('table-accounts') |
| 124 | + }) |
| 125 | + |
| 126 | + it('does not show the action for a non-Reference column', () => { |
| 127 | + renderMenu({ id: 'col-name', name: 'Name', type: 'string' }, vi.fn()) |
| 128 | + |
| 129 | + expect(findButton('Go to Reference Table')).toBeUndefined() |
| 130 | + }) |
| 131 | + |
| 132 | + it('does not show the action when Reference metadata has no target table', () => { |
| 133 | + renderMenu({ id: 'col-account', name: 'Account', type: 'reference' }, vi.fn()) |
| 134 | + |
| 135 | + expect(findButton('Go to Reference Table')).toBeUndefined() |
| 136 | + }) |
| 137 | +}) |
0 commit comments