import { screen } from '@testing-library/react' import userEvent from '@testing-library/user-event' import { beforeEach, describe, expect, it, vi } from 'vitest' import { PlanUpdateSidePanel } from './PlanUpdateSidePanel' import { MANAGED_BY } from '@/lib/constants/infrastructure' import { createMockOrganization } from '@/tests/helpers' import { customRender } from '@/tests/lib/custom-render' const mockSelectedOrganization = vi.hoisted(() => vi.fn()) const mockPartnerManagedResource = vi.hoisted(() => vi.fn()) const mockTrack = vi.hoisted(() => vi.fn()) const mockSubscriptionPlan = vi.hoisted(() => ({ id: 'free', name: 'Free' })) vi.mock('common', async (importOriginal) => { const original = (await importOriginal()) as typeof import('common') return { ...original, useParams: () => ({ slug: 'stripe-org' }), } }) const render = (ui: React.ReactElement) => customRender(ui, { nuqs: { searchParams: '?panel=subscriptionPlan' } }) vi.mock('shared-data/plans', () => ({ plans: [ { id: 'tier_free', planId: 'free', name: 'Free', costUnit: '/ month', features: ['Unlimited API requests'], description: 'Perfect for passion projects & simple websites.', preface: 'Get started with:', footer: 'Free projects are paused after 1 week of inactivity. Limit of 2 active projects.', }, { id: 'tier_pro', planId: 'pro', name: 'Pro', nameBadge: 'Most Popular', costUnit: '/ month', features: ['Email support'], description: 'For production applications with the power to scale.', preface: 'Everything in the Free Plan, plus:', }, ], })) vi.mock('@/hooks/misc/useSelectedOrganization', () => ({ useSelectedOrganizationQuery: () => ({ data: mockSelectedOrganization() }), })) vi.mock('@/lib/telemetry/track', () => ({ useTrack: () => mockTrack, })) vi.mock('@/hooks/misc/useCheckPermissionsV2', async (importOriginal) => ({ ...(await importOriginal()), useAsyncCheckPermissionsV2: () => ({ can: true }), })) vi.mock('@/data/projects/org-projects-infinite-query', () => ({ useOrgProjectsInfiniteQuery: () => ({ data: undefined }), })) vi.mock('@/data/organizations/free-project-limit-check-query', () => ({ useFreeProjectLimitCheckQuery: () => ({ data: [] }), })) vi.mock('@/data/organizations/organization-query', () => ({ useOrganizationQuery: () => ({ data: { has_oriole_project: false } }), })) vi.mock('@/data/subscriptions/org-subscription-query', () => ({ useOrgSubscriptionQuery: () => ({ data: { plan: { ...mockSubscriptionPlan }, billing_via_partner: false }, isSuccess: true, }), })) vi.mock('@/data/subscriptions/org-plans-query', () => ({ useOrgPlansQuery: () => ({ data: { plans: [ { id: 'free', price: 0 }, { id: 'pro', price: 25 }, ], }, isPending: false, }), })) vi.mock('@/data/organizations/organization-billing-subscription-preview', () => ({ useOrganizationBillingSubscriptionPreview: () => ({}), })) vi.mock('@/components/ui/PartnerManagedResource', () => ({ default: (props: any) => { mockPartnerManagedResource(props) return
{props.details}
}, })) vi.mock('./EnterpriseCard', () => ({ EnterpriseCard: () =>
Enterprise
, })) vi.mock('./DowngradeModal', () => ({ DowngradeModal: () => null, })) vi.mock('./ExitSurveyModal', () => ({ ExitSurveyModal: () => null, })) vi.mock('./UpgradeModal', () => ({ default: () => null, })) vi.mock('./MembersExceedLimitModal', () => ({ MembersExceedLimitModal: () => null, })) vi.mock('./SubscriptionPlanUpdateView', () => ({ SubscriptionPlanUpdateView: ({ onSuccess, }: { onSuccess: (result: { changeType: 'upgrade' }) => void }) => (
), })) vi.mock('./PlanUpgradeSuccessView', () => ({ PlanUpgradeSuccessView: ({ planId, previousPlanId, }: { planId: string previousPlanId?: string }) => (
{previousPlanId} to {planId}
), })) describe('PlanUpdateSidePanel', () => { beforeEach(() => { mockTrack.mockClear() Object.assign(mockSubscriptionPlan, { id: 'free', name: 'Free' }) mockSelectedOrganization.mockReturnValue( createMockOrganization({ slug: 'stripe-org', billing_partner: null, integration_source: 'stripe_projects', managed_by: MANAGED_BY.STRIPE_PROJECTS, }) ) }) it('shows Stripe-managed messaging without treating Stripe orgs as partner-billed', () => { render() expect(screen.getByTestId('partner-managed-resource')).toBeInTheDocument() expect(screen.getByText('stripe projects upgrade supabase/free')).toBeInTheDocument() expect(screen.getByRole('button', { name: 'Upgrade to Pro' })).toBeInTheDocument() }) it('uses the current plan in the Stripe Projects upgrade command', () => { mockSelectedOrganization.mockReturnValue( createMockOrganization({ slug: 'stripe-org', billing_partner: null, integration_source: 'stripe_projects', managed_by: MANAGED_BY.STRIPE_PROJECTS, plan: { id: 'pro', name: 'Pro' }, }) ) render() expect(screen.getByText('stripe projects upgrade supabase/pro')).toBeInTheDocument() }) it('uses the Stripe Projects downgrade command for team plans', () => { mockSelectedOrganization.mockReturnValue( createMockOrganization({ slug: 'stripe-org', billing_partner: null, integration_source: 'stripe_projects', managed_by: MANAGED_BY.STRIPE_PROJECTS, plan: { id: 'team', name: 'Team' }, }) ) render() expect(screen.getByText('stripe projects downgrade supabase/team')).toBeInTheDocument() }) it('still shows partner-managed messaging for billing-partner orgs', () => { mockSelectedOrganization.mockReturnValue( createMockOrganization({ slug: 'aws-org', billing_partner: 'aws_marketplace', managed_by: MANAGED_BY.AWS_MARKETPLACE, }) ) render() expect(screen.getByTestId('partner-managed-resource')).toBeInTheDocument() }) describe('plan presentation', () => { beforeEach(() => { mockSelectedOrganization.mockReturnValue( createMockOrganization({ slug: 'test-org', billing_partner: null }) ) }) it('renders the full-screen shell', () => { render() expect(screen.getByRole('button', { name: /Back/ })).toBeInTheDocument() expect(screen.getByRole('link', { name: /Compare plans/ })).toBeInTheDocument() }) it('renders pricing-page style cards with description and preface', () => { render() expect( screen.getByText('Perfect for passion projects & simple websites.') ).toBeInTheDocument() expect(screen.getByText('Everything in the Free Plan, plus:')).toBeInTheDocument() }) it('offers AWS Marketplace as a separate purchasing route', () => { render() expect(screen.getByText('Prefer purchasing through AWS Marketplace?')).toBeInTheDocument() expect(screen.getByRole('link', { name: 'View options' })).toHaveAttribute( 'href', 'https://supabase.com/aws-marketplace' ) }) it('does not offer AWS Marketplace for partner-billed organizations', () => { mockSelectedOrganization.mockReturnValue( createMockOrganization({ slug: 'aws-org', billing_partner: 'aws_marketplace', managed_by: MANAGED_BY.AWS_MARKETPLACE, }) ) render() expect( screen.queryByText('Prefer purchasing through AWS Marketplace?') ).not.toBeInTheDocument() }) it('does not render gap rows', () => { render() expect(screen.queryByText('Not included')).not.toBeInTheDocument() expect(screen.queryByText('Plan limits')).not.toBeInTheDocument() }) }) it('sends the panel opened event once, after the organization has loaded', () => { mockSelectedOrganization.mockReturnValue(undefined) const { rerender } = render() expect(mockTrack).not.toHaveBeenCalled() mockSelectedOrganization.mockReturnValue( createMockOrganization({ slug: 'test-org', billing_partner: null }) ) rerender() rerender() expect(mockTrack).toHaveBeenCalledTimes(1) expect(mockTrack).toHaveBeenCalledWith('studio_pricing_side_panel_opened', { currentPlan: 'Free', }) }) describe('plan change surface', () => { beforeEach(() => { mockSelectedOrganization.mockReturnValue( createMockOrganization({ slug: 'test-org', billing_partner: null }) ) }) const selectPro = async () => { render() await userEvent.click(screen.getByRole('button', { name: 'Upgrade to Pro' })) } it('shows the full-screen plan update view', async () => { await selectPro() expect(screen.getByTestId('plan-update-view')).toBeInTheDocument() }) it('remembers the plan from before the upgrade after the subscription refetches', async () => { const { rerender } = render() await userEvent.click(screen.getByRole('button', { name: 'Upgrade to Pro' })) // The upgrade refetches the subscription before the success screen opens. Object.assign(mockSubscriptionPlan, { id: 'pro', name: 'Pro' }) rerender() await userEvent.click(screen.getByRole('button', { name: 'Complete upgrade' })) expect(await screen.findByTestId('plan-upgrade-success')).toHaveTextContent('free to pro') }) it('reads the previous plan from the URL when the success screen loads fresh', async () => { Object.assign(mockSubscriptionPlan, { id: 'team', name: 'Team' }) customRender(, { nuqs: { searchParams: '?panel=planUpgraded&upgradedFrom=free' }, }) expect(await screen.findByTestId('plan-upgrade-success')).toHaveTextContent('free to team') }) }) })