Files
Charis 19aa27384a feat(studio): guard branch creation with FGA grants (#52375)
Part 4 of 7 in a stack that splits #52331 into reviewable pieces.
Resolves part of
[FE-4693](https://linear.app/supabase/issue/FE-4693/pilot-migrate-branches-to-grants-8-endpoints-createbranchmodal).
There are no functional changes beyond #52331, and the top of the stack
is identical to it. See #52331 for the full rationale.

## Stack
1. #52372 Grant infra for branch variants
2. #52373 Branches list query
3. #52374 Page gates on the list grant
4. 👉 #52375 Branch creation
5. #52376 Branch read, delete, reset, and restore
6. #52377 Branch update and push
7. #52378 Branch diff and merge

## Changes
The create mutation takes a grant. `CreateBranchButton`,
`CreateBranchModal`, and `Branch.Commands` (legacy `CREATE
preview_branches`) switch to `useGrant('POST
/v1/projects/{ref}/branches')`. "Include data" is disabled without
production create; previously it failed after you submitted. The GitHub
integration form gets a minimal production-create guard, which ##52377
replaces.

## Testing
New `CreateBranchButton` test.
2026-10-28 14:16:03 -04:00

73 lines
2.5 KiB
TypeScript

import { screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { describe, expect, test, vi } from 'vitest'
import { CreateBranchButton } from './CreateBranchButton'
import { FGA_PERMISSIONS } from '@/hooks/misc/useCheckPermissionsV2'
import {
BRANCH_TEST_SCOPE,
mockBranchProject,
mockParentProjectPermissions,
} from '@/tests/lib/branch-test-utils'
import { customRender } from '@/tests/lib/custom-render'
import { mockPermissions, mockPermissionsError } from '@/tests/lib/msw-permissions'
import { NO_PERMISSIONS } from '@/tests/lib/permissions-fixtures'
import { createMockProfileContext } from '@/tests/lib/profile-helpers'
vi.mock('common', async (importOriginal) => {
const actual = await importOriginal<typeof import('common')>()
return {
...actual,
useIsLoggedIn: () => true,
useParams: () => ({ ref: BRANCH_TEST_SCOPE.branchRef }),
}
})
vi.mock('@/lib/constants', async (importOriginal) => {
const actual = await importOriginal<typeof import('@/lib/constants')>()
return { ...actual, IS_PLATFORM: true }
})
const renderButton = () =>
customRender(<CreateBranchButton />, { profileContext: createMockProfileContext() })
const getCreateButton = () => screen.findByRole('button', { name: 'Create branch' })
describe('CreateBranchButton', () => {
test('is enabled when the user can create branches on the parent project', async () => {
mockBranchProject()
mockParentProjectPermissions([FGA_PERMISSIONS.PROJECT.BRANCHING_DEVELOPMENT_CREATE])
renderButton()
const createButton = await getCreateButton()
await waitFor(() => expect(createButton).toBeEnabled())
})
test('is disabled with an explanation when the user lacks the permission', async () => {
mockBranchProject()
mockPermissions(NO_PERMISSIONS)
renderButton()
const createButton = await getCreateButton()
await userEvent.hover(createButton)
expect(
await screen.findAllByText('You need additional permissions to create branches')
).not.toHaveLength(0)
expect(createButton).toHaveAttribute('aria-disabled', 'true')
})
test('is disabled with the error when permissions fail to load', async () => {
mockBranchProject()
mockPermissionsError({ message: 'Permissions service unavailable' })
renderButton()
const createButton = await getCreateButton()
await userEvent.hover(createButton)
expect(await screen.findAllByText('Failed to check permissions')).not.toHaveLength(0)
expect(createButton).toHaveAttribute('aria-disabled', 'true')
})
})