|
| 1 | +/** @vitest-environment jsdom */ |
| 2 | +import { act, createRef } from 'react' |
| 3 | +import { integrationMatcherMock } from '@sim/testing/mocks/integration-matcher.mock' |
| 4 | +import { QueryClient, QueryClientProvider } from '@tanstack/react-query' |
| 5 | +import { createRoot, type Root } from 'react-dom/client' |
| 6 | +import { afterEach, beforeEach, expect, it, vi } from 'vitest' |
| 7 | +import type { PlusMenuHandle } from '@/app/workspace/[workspaceId]/home/components/user-input/components/constants' |
| 8 | +import { PlusMenuDropdown } from '@/app/workspace/[workspaceId]/home/components/user-input/components/plus-menu-dropdown/plus-menu-dropdown' |
| 9 | +import { FeatureFlagsProvider } from '@/app/workspace/[workspaceId]/providers/feature-flags-provider' |
| 10 | +import { workspaceKeys } from '@/hooks/queries/workspace' |
| 11 | + |
| 12 | +vi.mock('@/blocks/integration-matcher', () => ({ |
| 13 | + ...integrationMatcherMock, |
| 14 | + listIntegrationsByPopularity: () => [], |
| 15 | +})) |
| 16 | + |
| 17 | +vi.mock( |
| 18 | + 'next/navigation', |
| 19 | + async () => (await import('@sim/testing/mocks/next-navigation.mock')).nextNavigationMock |
| 20 | +) |
| 21 | + |
| 22 | +let client: QueryClient |
| 23 | +let root: Root |
| 24 | +let container: HTMLDivElement |
| 25 | +const handle = createRef<PlusMenuHandle>() |
| 26 | +const selected = vi.fn() |
| 27 | +const project = { |
| 28 | + id: 'project', |
| 29 | + name: 'Gate fixture', |
| 30 | + organizationId: 'organization', |
| 31 | + ownerId: 'user', |
| 32 | + archivedAt: null, |
| 33 | + createdAt: '2026-10-08T00:00:00Z', |
| 34 | + updatedAt: '2026-10-08T00:00:00Z', |
| 35 | + environments: [], |
| 36 | + capabilities: { administer: true, issues: true }, |
| 37 | +} |
| 38 | +beforeEach(() => { |
| 39 | + vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true) |
| 40 | + vi.useFakeTimers() |
| 41 | + Element.prototype.scrollIntoView = vi.fn() |
| 42 | + selected.mockClear() |
| 43 | + client = new QueryClient({ defaultOptions: { queries: { retry: false } } }) |
| 44 | + client.setQueryData(workspaceKeys.list(), { workspaces: [] }) |
| 45 | + vi.stubGlobal('fetch', async (input: string) => |
| 46 | + Response.json( |
| 47 | + input.startsWith('/api/projects?') ? { projects: [project], nextCursor: null } : {} |
| 48 | + ) |
| 49 | + ) |
| 50 | + container = document.createElement('div') |
| 51 | + document.body.append(container) |
| 52 | + root = createRoot(container) |
| 53 | +}) |
| 54 | +afterEach(async () => { |
| 55 | + await act(async () => root.unmount()) |
| 56 | + client.clear() |
| 57 | + container.remove() |
| 58 | + vi.useRealTimers() |
| 59 | +}) |
| 60 | +async function render(projectFiles: boolean) { |
| 61 | + await act(async () => |
| 62 | + root.render( |
| 63 | + <QueryClientProvider client={client}> |
| 64 | + <FeatureFlagsProvider |
| 65 | + flags={{ |
| 66 | + projects: true, |
| 67 | + 'project-files': projectFiles, |
| 68 | + dashboards: false, |
| 69 | + 'mothership-model-selector': false, |
| 70 | + 'mothership-plan-mode': false, |
| 71 | + }} |
| 72 | + > |
| 73 | + <PlusMenuDropdown |
| 74 | + ref={handle} |
| 75 | + workspaceId='' |
| 76 | + organizationId='organization' |
| 77 | + warm |
| 78 | + onResourceSelect={() => {}} |
| 79 | + onWorkspaceSelect={() => {}} |
| 80 | + onProjectSelect={selected} |
| 81 | + onClose={() => {}} |
| 82 | + textareaRef={{ current: null }} |
| 83 | + pendingCursorRef={{ current: null }} |
| 84 | + mentionQuery={project.name} |
| 85 | + /> |
| 86 | + </FeatureFlagsProvider> |
| 87 | + </QueryClientProvider> |
| 88 | + ) |
| 89 | + ) |
| 90 | + await act(async () => vi.advanceTimersByTimeAsync(10)) |
| 91 | +} |
| 92 | +it('stops cached Project mention selection when Project files are disabled', async () => { |
| 93 | + await render(true) |
| 94 | + await act(async () => handle.current?.open({ left: 0, top: 0 }, { mention: true })) |
| 95 | + await act(async () => handle.current?.selectActive()) |
| 96 | + expect(selected).toHaveBeenCalledWith( |
| 97 | + expect.objectContaining({ id: project.id, name: project.name }) |
| 98 | + ) |
| 99 | + selected.mockClear() |
| 100 | + await render(false) |
| 101 | + await act(async () => handle.current?.open({ left: 0, top: 0 }, { mention: true })) |
| 102 | + await act(async () => handle.current?.selectActive()) |
| 103 | + expect(selected).not.toHaveBeenCalled() |
| 104 | +}) |
| 105 | +it('stops Project tagging through the browse submenu when Project files are disabled', async () => { |
| 106 | + await render(true) |
| 107 | + await act(async () => handle.current?.open({ left: 0, top: 0 })) |
| 108 | + const selectProject = async () => { |
| 109 | + const trigger = Array.from(document.querySelectorAll<HTMLElement>('[role="menuitem"]')).find( |
| 110 | + (item) => item.textContent === project.name |
| 111 | + ) |
| 112 | + if (!trigger) throw new Error('Project fixture was not available for environment browsing') |
| 113 | + await act(async () => trigger.click()) |
| 114 | + } |
| 115 | + await selectProject() |
| 116 | + expect(selected).toHaveBeenCalledWith(expect.objectContaining({ id: project.id })) |
| 117 | + selected.mockClear() |
| 118 | + await render(false) |
| 119 | + await act(async () => handle.current?.open({ left: 0, top: 0 })) |
| 120 | + await selectProject() |
| 121 | + expect(selected).not.toHaveBeenCalled() |
| 122 | +}) |
0 commit comments