|
| 1 | +import type { ReactNode } from 'react' |
| 2 | +import { renderToStaticMarkup } from 'react-dom/server' |
| 3 | +import { describe, expect, it, vi } from 'vitest' |
| 4 | + |
| 5 | +vi.mock('@sim/emcn', () => ({ |
| 6 | + DropdownMenu: ({ children, open }: { children: ReactNode; open: boolean }) => |
| 7 | + open ? <>{children}</> : null, |
| 8 | + DropdownMenuContent: ({ children }: { children: ReactNode }) => <>{children}</>, |
| 9 | + DropdownMenuItem: ({ children }: { children: ReactNode }) => <span>{children}</span>, |
| 10 | + DropdownMenuSeparator: () => <hr />, |
| 11 | + DropdownMenuSub: ({ children }: { children: ReactNode }) => <>{children}</>, |
| 12 | + DropdownMenuSubContent: ({ children }: { children: ReactNode }) => <>{children}</>, |
| 13 | + DropdownMenuSubTrigger: ({ children }: { children: ReactNode }) => <span>{children}</span>, |
| 14 | + DropdownMenuTrigger: ({ children }: { children: ReactNode }) => <>{children}</>, |
| 15 | + Upload: () => null, |
| 16 | +})) |
| 17 | + |
| 18 | +vi.mock('@sim/emcn/icons', () => ({ |
| 19 | + Database: () => null, |
| 20 | + Download: () => null, |
| 21 | + Duplicate: () => null, |
| 22 | + Eye: () => null, |
| 23 | + FolderInput: () => null, |
| 24 | + Pencil: () => null, |
| 25 | + Pin: () => null, |
| 26 | + SquareArrowUpRight: () => null, |
| 27 | + TagIcon: () => null, |
| 28 | + Trash: () => null, |
| 29 | +})) |
| 30 | + |
| 31 | +vi.mock('@/app/workspace/[workspaceId]/components/folders', () => ({ |
| 32 | + renderMoveOptions: () => <span>Destination</span>, |
| 33 | +})) |
| 34 | + |
| 35 | +vi.mock('@/app/workspace/[workspaceId]/components/folders/move-options', () => ({ |
| 36 | + renderMoveOptions: () => <span>Destination</span>, |
| 37 | +})) |
| 38 | + |
| 39 | +import { FolderContextMenu } from '@/app/workspace/[workspaceId]/components/folders/folder-context-menu' |
| 40 | +import { KnowledgeBaseContextMenu } from '@/app/workspace/[workspaceId]/knowledge/components/knowledge-base-context-menu/knowledge-base-context-menu' |
| 41 | +import { TableContextMenu } from '@/app/workspace/[workspaceId]/tables/components/table-context-menu/table-context-menu' |
| 42 | + |
| 43 | +const POSITION = { x: 0, y: 0 } |
| 44 | +const MOVE_OPTIONS = [{ value: '__root__', label: 'Root', children: [] }] |
| 45 | + |
| 46 | +describe('selection-aware resource context menus', () => { |
| 47 | + it('limits a multi-table menu to actions that can target the selection', () => { |
| 48 | + const menu = renderToStaticMarkup( |
| 49 | + <TableContextMenu |
| 50 | + isOpen |
| 51 | + position={POSITION} |
| 52 | + onClose={() => {}} |
| 53 | + onCopyId={() => {}} |
| 54 | + onTogglePin={() => {}} |
| 55 | + onDelete={() => {}} |
| 56 | + onViewSchema={() => {}} |
| 57 | + onRename={() => {}} |
| 58 | + onImportCsv={() => {}} |
| 59 | + onExportCsv={() => {}} |
| 60 | + onMove={() => {}} |
| 61 | + moveOptions={MOVE_OPTIONS} |
| 62 | + selectedCount={3} |
| 63 | + /> |
| 64 | + ) |
| 65 | + |
| 66 | + expect(menu).toContain('Move 3 items') |
| 67 | + expect(menu).toContain('Delete 3 items') |
| 68 | + expect(menu).not.toContain('View Schema') |
| 69 | + expect(menu).not.toContain('Rename') |
| 70 | + expect(menu).not.toContain('Copy ID') |
| 71 | + expect(menu).not.toContain('Pin') |
| 72 | + }) |
| 73 | + |
| 74 | + it('limits a multi-base menu to actions that can target the selection', () => { |
| 75 | + const menu = renderToStaticMarkup( |
| 76 | + <KnowledgeBaseContextMenu |
| 77 | + isOpen |
| 78 | + position={POSITION} |
| 79 | + onClose={() => {}} |
| 80 | + onOpenInNewTab={() => {}} |
| 81 | + onViewTags={() => {}} |
| 82 | + onCopyId={() => {}} |
| 83 | + onTogglePin={() => {}} |
| 84 | + onEdit={() => {}} |
| 85 | + onDelete={() => {}} |
| 86 | + onMove={() => {}} |
| 87 | + moveOptions={MOVE_OPTIONS} |
| 88 | + selectedCount={2} |
| 89 | + /> |
| 90 | + ) |
| 91 | + |
| 92 | + expect(menu).toContain('Move 2 items') |
| 93 | + expect(menu).toContain('Delete 2 items') |
| 94 | + expect(menu).not.toContain('Open in new tab') |
| 95 | + expect(menu).not.toContain('View tags') |
| 96 | + expect(menu).not.toContain('Copy ID') |
| 97 | + expect(menu).not.toContain('Pin') |
| 98 | + expect(menu).not.toContain('Edit') |
| 99 | + }) |
| 100 | + |
| 101 | + it('uses the same group-action contract when a selected folder opens the menu', () => { |
| 102 | + const menu = renderToStaticMarkup( |
| 103 | + <FolderContextMenu |
| 104 | + isOpen |
| 105 | + position={POSITION} |
| 106 | + onClose={() => {}} |
| 107 | + onOpen={() => {}} |
| 108 | + onRename={() => {}} |
| 109 | + onDelete={() => {}} |
| 110 | + onCopyId={() => {}} |
| 111 | + onMove={() => {}} |
| 112 | + onTogglePin={() => {}} |
| 113 | + pinned={false} |
| 114 | + moveOptions={MOVE_OPTIONS} |
| 115 | + canEdit |
| 116 | + selectedCount={4} |
| 117 | + /> |
| 118 | + ) |
| 119 | + |
| 120 | + expect(menu).toContain('Move 4 items') |
| 121 | + expect(menu).toContain('Delete 4 items') |
| 122 | + expect(menu).not.toContain('Open') |
| 123 | + expect(menu).not.toContain('Rename') |
| 124 | + expect(menu).not.toContain('Copy ID') |
| 125 | + expect(menu).not.toContain('Pin') |
| 126 | + }) |
| 127 | +}) |
0 commit comments