|
| 1 | +/** @vitest-environment jsdom */ |
| 2 | + |
| 3 | +import { act } from 'react' |
| 4 | +import { flushMacrotask } from '@sim/testing/helpers' |
| 5 | +import { jsonResponse } from '@sim/testing/helpers/http' |
| 6 | +import { QueryClient, QueryClientProvider } from '@tanstack/react-query' |
| 7 | +import { createRoot, type Root } from 'react-dom/client' |
| 8 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 9 | +import { FeatureFlagsProvider } from '@/app/workspace/[workspaceId]/providers/feature-flags-provider' |
| 10 | +import { ProjectIssueRestrictions } from '@/ee/access-control/components/project-issue-restrictions' |
| 11 | + |
| 12 | +let root: Root |
| 13 | +let container: HTMLDivElement |
| 14 | +let client: QueryClient |
| 15 | +let requests: string[] |
| 16 | + |
| 17 | +beforeEach(() => { |
| 18 | + vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true) |
| 19 | + client = new QueryClient({ defaultOptions: { queries: { retry: false } } }) |
| 20 | + requests = [] |
| 21 | + vi.stubGlobal( |
| 22 | + 'fetch', |
| 23 | + vi.fn(async (url: string) => { |
| 24 | + requests.push(url) |
| 25 | + return jsonResponse({ |
| 26 | + projects: [ |
| 27 | + { |
| 28 | + id: 'project-1', |
| 29 | + name: 'Payments', |
| 30 | + organizationId: 'org-1', |
| 31 | + ownerId: 'user-1', |
| 32 | + archivedAt: null, |
| 33 | + createdAt: '2026-01-01T00:00:00Z', |
| 34 | + updatedAt: '2026-01-01T00:00:00Z', |
| 35 | + environments: [], |
| 36 | + capabilities: { administer: true, issues: true }, |
| 37 | + }, |
| 38 | + ], |
| 39 | + nextCursor: null, |
| 40 | + }) |
| 41 | + }) |
| 42 | + ) |
| 43 | + container = document.createElement('div') |
| 44 | + document.body.appendChild(container) |
| 45 | + root = createRoot(container) |
| 46 | +}) |
| 47 | + |
| 48 | +afterEach(() => { |
| 49 | + act(() => root.unmount()) |
| 50 | + client.clear() |
| 51 | + container.remove() |
| 52 | +}) |
| 53 | + |
| 54 | +async function render(projects: boolean) { |
| 55 | + await act(async () => { |
| 56 | + root.render( |
| 57 | + <QueryClientProvider client={client}> |
| 58 | + <FeatureFlagsProvider |
| 59 | + flags={{ |
| 60 | + dashboards: false, |
| 61 | + 'workflow-tests': false, |
| 62 | + changelog: false, |
| 63 | + 'mothership-model-selector': false, |
| 64 | + 'mothership-plan-mode': false, |
| 65 | + projects, |
| 66 | + }} |
| 67 | + > |
| 68 | + <ProjectIssueRestrictions organizationId='org-1' value={[]} onChange={() => {}} /> |
| 69 | + </FeatureFlagsProvider> |
| 70 | + </QueryClientProvider> |
| 71 | + ) |
| 72 | + }) |
| 73 | + await act(() => flushMacrotask()) |
| 74 | +} |
| 75 | + |
| 76 | +describe('ProjectIssueRestrictions', () => { |
| 77 | + it('never reads the Project APIs while the projects flag is off', async () => { |
| 78 | + await render(false) |
| 79 | + expect(requests).toEqual([]) |
| 80 | + expect(container.querySelector('#project-issues-project-1')).toBeNull() |
| 81 | + }) |
| 82 | + |
| 83 | + it('offers each authorized Project once the projects flag is on', async () => { |
| 84 | + await render(true) |
| 85 | + expect(requests.some((url) => url.startsWith('/api/projects?'))).toBe(true) |
| 86 | + expect(container.querySelector('#project-issues-project-1')).not.toBeNull() |
| 87 | + }) |
| 88 | + |
| 89 | + it('hides Projects loaded before the projects flag turned off', async () => { |
| 90 | + await render(true) |
| 91 | + act(() => root.unmount()) |
| 92 | + root = createRoot(container) |
| 93 | + requests = [] |
| 94 | + await render(false) |
| 95 | + expect(requests).toEqual([]) |
| 96 | + expect(container.querySelector('#project-issues-project-1')).toBeNull() |
| 97 | + }) |
| 98 | +}) |
0 commit comments