|
| 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 | + Plus: () => null, |
| 27 | + SquareArrowUpRight: () => null, |
| 28 | + TagIcon: () => null, |
| 29 | + Trash: () => null, |
| 30 | +})) |
| 31 | + |
| 32 | +vi.mock('@/app/workspace/[workspaceId]/components/folders', () => ({ |
| 33 | + renderMoveOptions: () => <span>Destination</span>, |
| 34 | +})) |
| 35 | + |
| 36 | +vi.mock('@/app/workspace/[workspaceId]/components/folders/move-options', () => ({ |
| 37 | + renderMoveOptions: () => <span>Destination</span>, |
| 38 | +})) |
| 39 | + |
| 40 | +import { FolderContextMenu } from '@/app/workspace/[workspaceId]/components/folders/folder-context-menu' |
| 41 | +import { ChunkContextMenu } from '@/app/workspace/[workspaceId]/knowledge/[id]/[documentId]/components/chunk-context-menu/chunk-context-menu' |
| 42 | +import { DocumentContextMenu } from '@/app/workspace/[workspaceId]/knowledge/[id]/components/document-context-menu/document-context-menu' |
| 43 | +import { KnowledgeBaseContextMenu } from '@/app/workspace/[workspaceId]/knowledge/components/knowledge-base-context-menu/knowledge-base-context-menu' |
| 44 | +import { TableContextMenu } from '@/app/workspace/[workspaceId]/tables/components/table-context-menu/table-context-menu' |
| 45 | + |
| 46 | +const POSITION = { x: 0, y: 0 } |
| 47 | +const MOVE_OPTIONS = [{ value: '__root__', label: 'Root', children: [] }] |
| 48 | + |
| 49 | +describe('selection-aware resource context menus', () => { |
| 50 | + it('limits a multi-table menu to actions that can target the selection', () => { |
| 51 | + const menu = renderToStaticMarkup( |
| 52 | + <TableContextMenu |
| 53 | + isOpen |
| 54 | + position={POSITION} |
| 55 | + onClose={() => {}} |
| 56 | + onCopyId={() => {}} |
| 57 | + onTogglePin={() => {}} |
| 58 | + onDelete={() => {}} |
| 59 | + onViewSchema={() => {}} |
| 60 | + onRename={() => {}} |
| 61 | + onImportCsv={() => {}} |
| 62 | + onExportCsv={() => {}} |
| 63 | + onMove={() => {}} |
| 64 | + moveOptions={MOVE_OPTIONS} |
| 65 | + selectedCount={3} |
| 66 | + /> |
| 67 | + ) |
| 68 | + |
| 69 | + expect(menu).toContain('Move 3 items') |
| 70 | + expect(menu).toContain('Delete 3 items') |
| 71 | + expect(menu).not.toContain('View Schema') |
| 72 | + expect(menu).not.toContain('Rename') |
| 73 | + expect(menu).not.toContain('Copy ID') |
| 74 | + expect(menu).not.toContain('Pin') |
| 75 | + }) |
| 76 | + |
| 77 | + it('limits a multi-base menu to actions that can target the selection', () => { |
| 78 | + const menu = renderToStaticMarkup( |
| 79 | + <KnowledgeBaseContextMenu |
| 80 | + isOpen |
| 81 | + position={POSITION} |
| 82 | + onClose={() => {}} |
| 83 | + onOpenInNewTab={() => {}} |
| 84 | + onViewTags={() => {}} |
| 85 | + onCopyId={() => {}} |
| 86 | + onTogglePin={() => {}} |
| 87 | + onEdit={() => {}} |
| 88 | + onDelete={() => {}} |
| 89 | + onMove={() => {}} |
| 90 | + moveOptions={MOVE_OPTIONS} |
| 91 | + selectedCount={2} |
| 92 | + /> |
| 93 | + ) |
| 94 | + |
| 95 | + expect(menu).toContain('Move 2 items') |
| 96 | + expect(menu).toContain('Delete 2 items') |
| 97 | + expect(menu).not.toContain('Open in new tab') |
| 98 | + expect(menu).not.toContain('View tags') |
| 99 | + expect(menu).not.toContain('Copy ID') |
| 100 | + expect(menu).not.toContain('Pin') |
| 101 | + expect(menu).not.toContain('Edit') |
| 102 | + }) |
| 103 | + |
| 104 | + it('uses the same group-action contract when a selected folder opens the menu', () => { |
| 105 | + const menu = renderToStaticMarkup( |
| 106 | + <FolderContextMenu |
| 107 | + isOpen |
| 108 | + position={POSITION} |
| 109 | + onClose={() => {}} |
| 110 | + onOpen={() => {}} |
| 111 | + onRename={() => {}} |
| 112 | + onDelete={() => {}} |
| 113 | + onCopyId={() => {}} |
| 114 | + onMove={() => {}} |
| 115 | + onTogglePin={() => {}} |
| 116 | + pinned={false} |
| 117 | + moveOptions={MOVE_OPTIONS} |
| 118 | + canEdit |
| 119 | + selectedCount={4} |
| 120 | + /> |
| 121 | + ) |
| 122 | + |
| 123 | + expect(menu).toContain('Move 4 items') |
| 124 | + expect(menu).toContain('Delete 4 items') |
| 125 | + expect(menu).not.toContain('Open') |
| 126 | + expect(menu).not.toContain('Rename') |
| 127 | + expect(menu).not.toContain('Copy ID') |
| 128 | + expect(menu).not.toContain('Pin') |
| 129 | + }) |
| 130 | + |
| 131 | + it('explains when a read-only multi-folder selection has no actions', () => { |
| 132 | + const menu = renderToStaticMarkup( |
| 133 | + <FolderContextMenu |
| 134 | + isOpen |
| 135 | + position={POSITION} |
| 136 | + onClose={() => {}} |
| 137 | + onOpen={() => {}} |
| 138 | + onRename={() => {}} |
| 139 | + onDelete={() => {}} |
| 140 | + onTogglePin={() => {}} |
| 141 | + pinned={false} |
| 142 | + canEdit={false} |
| 143 | + selectedCount={2} |
| 144 | + /> |
| 145 | + ) |
| 146 | + |
| 147 | + expect(menu).toContain('No actions available') |
| 148 | + expect(menu).not.toContain('Open') |
| 149 | + expect(menu).not.toContain('Delete') |
| 150 | + }) |
| 151 | + |
| 152 | + it('counts only the documents affected by a mixed-selection toggle', () => { |
| 153 | + const menu = renderToStaticMarkup( |
| 154 | + <DocumentContextMenu |
| 155 | + isOpen |
| 156 | + position={POSITION} |
| 157 | + onClose={() => {}} |
| 158 | + hasDocument |
| 159 | + selectedCount={25} |
| 160 | + enabledCount={7} |
| 161 | + disabledCount={18} |
| 162 | + onToggleEnabled={() => {}} |
| 163 | + onDelete={() => {}} |
| 164 | + /> |
| 165 | + ) |
| 166 | + |
| 167 | + expect(menu).toContain('Enable 18 items') |
| 168 | + expect(menu).toContain('Delete 25 items') |
| 169 | + }) |
| 170 | + |
| 171 | + it('does not overstate an unknown select-all toggle count', () => { |
| 172 | + const menu = renderToStaticMarkup( |
| 173 | + <DocumentContextMenu |
| 174 | + isOpen |
| 175 | + position={POSITION} |
| 176 | + onClose={() => {}} |
| 177 | + hasDocument |
| 178 | + selectedCount={25} |
| 179 | + enabledCount={25} |
| 180 | + disabledCount={25} |
| 181 | + hasExactToggleCount={false} |
| 182 | + onToggleEnabled={() => {}} |
| 183 | + /> |
| 184 | + ) |
| 185 | + |
| 186 | + expect(menu).toContain('Enable selected items') |
| 187 | + expect(menu).not.toContain('Enable 25 items') |
| 188 | + }) |
| 189 | + |
| 190 | + it('counts only the chunks affected by a multi-selection toggle', () => { |
| 191 | + const menu = renderToStaticMarkup( |
| 192 | + <ChunkContextMenu |
| 193 | + isOpen |
| 194 | + position={POSITION} |
| 195 | + onClose={() => {}} |
| 196 | + hasChunk |
| 197 | + selectedCount={3} |
| 198 | + enabledCount={3} |
| 199 | + onToggleEnabled={() => {}} |
| 200 | + onDelete={() => {}} |
| 201 | + /> |
| 202 | + ) |
| 203 | + |
| 204 | + expect(menu).toContain('Disable 3 items') |
| 205 | + expect(menu).toContain('Delete 3 items') |
| 206 | + }) |
| 207 | +}) |
0 commit comments