Files
Joshen Lim 64eafa5058 Replace valtio store with URL state for organization billing panels (#53353)
## Context

Realised that opening the organization billing subscription + spend cap
panels do not actually append the `?panel` URL param, but those panels
will be opened if you land on `/billing?panel=xxx` directly.

There's a discrepancy between how this state is being handled - we've
got a valtio store to handle the panel open state, and also URL handlers
as well that look out for the `panel` query param in a `useEffect` which
then updates the valtio store.

Opting to streamline these by replacing the valtio store with a simple
`useQueryState` instead.

## To test

- [ ] Verify that clicking "Change subscription plan" or "Change spend
cap" in org billing settings opens their respective panels with the
right URL param appended, and closing them removes the URL param
- [ ] Verify that landing directly on `/billing?panel=subscriptionPlan`
or `/billing?panel=costControl` opens the respective panels too
2026-11-13 17:50:39 +07:00

166 lines
5.2 KiB
TypeScript

import { fireEvent, screen, waitFor } from '@testing-library/react'
import type { ReactNode } from 'react'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import Subscription from './Subscription'
import { customRender as render } from '@/tests/lib/custom-render'
const { mockSubscription, mockSelectedOrganization, mockPermissions } = vi.hoisted(() => ({
mockSubscription: vi.fn(),
mockSelectedOrganization: vi.fn(),
mockPermissions: vi.fn(),
}))
vi.mock('common', async (importOriginal) => {
const original = (await importOriginal()) as typeof import('common')
return {
...original,
useParams: () => ({ slug: 'stripe-org' }),
useFlag: () => false,
}
})
vi.mock('@/data/subscriptions/org-subscription-query', () => ({
useOrgSubscriptionQuery: () => mockSubscription(),
}))
vi.mock('@/hooks/misc/useCheckPermissionsV2', async (importOriginal) => ({
...(await importOriginal<typeof import('@/hooks/misc/useCheckPermissionsV2')>()),
useAsyncCheckPermissionsV2: () => mockPermissions(),
}))
vi.mock('@/hooks/misc/useSelectedOrganization', () => ({
useSelectedOrganizationQuery: () => ({
data: mockSelectedOrganization(),
}),
}))
vi.mock('../Restriction', () => ({
Restriction: () => null,
}))
vi.mock('../ProjectUpdateDisabledTooltip', () => ({
ProjectUpdateDisabledTooltip: ({ children }: { children: ReactNode }) => children,
}))
vi.mock('./PlanUpdateSidePanel', () => ({
PlanUpdateSidePanel: () => null,
}))
describe('Subscription', () => {
beforeEach(() => {
mockSelectedOrganization.mockReturnValue({ managed_by: 'supabase' })
mockPermissions.mockReturnValue({ can: true, isSuccess: true })
mockSubscription.mockReturnValue({
data: {
plan: { id: 'free', name: 'Free' },
usage_billing_enabled: true,
billing_via_partner: false,
},
error: null,
isPending: false,
isError: false,
isSuccess: true,
})
})
it('shows the plan-change CTA and opens the side panel when clicked', async () => {
const onUrlUpdate = vi.fn()
render(<Subscription />, { nuqs: { onUrlUpdate } })
const button = screen.getByRole('button', { name: 'Change subscription plan' })
expect(button).toBeInTheDocument()
expect(onUrlUpdate).not.toHaveBeenCalled()
fireEvent.click(button)
await waitFor(() => expect(onUrlUpdate).toHaveBeenCalledTimes(1))
expect(onUrlUpdate.mock.calls[0][0].searchParams.get('panel')).toBe('subscriptionPlan')
})
it('links to Compare plans and AWS Marketplace under More information', () => {
render(<Subscription />)
expect(screen.getByText('More information')).toBeInTheDocument()
expect(screen.getByRole('link', { name: /Compare plans/ })).toHaveAttribute(
'href',
'https://supabase.com/pricing#compare-plans'
)
expect(screen.getByRole('link', { name: /AWS Marketplace/ })).toHaveAttribute(
'href',
'https://supabase.com/aws-marketplace'
)
})
it('shows the support fallback when plan changes are not available', () => {
mockSubscription.mockReturnValue({
data: {
plan: { id: 'enterprise', name: 'Enterprise' },
usage_billing_enabled: true,
billing_via_partner: false,
},
error: null,
isPending: false,
isError: false,
isSuccess: true,
})
render(<Subscription />)
expect(
screen.queryByRole('button', { name: 'Change subscription plan' })
).not.toBeInTheDocument()
expect(screen.getByText('Unable to update plan from Enterprise')).toBeInTheDocument()
expect(screen.getByRole('link', { name: /Compare plans/ })).toBeInTheDocument()
expect(screen.queryByRole('link', { name: /AWS Marketplace/ })).not.toBeInTheDocument()
})
it('hides the AWS Marketplace link while subscription is loading', () => {
mockSubscription.mockReturnValue({
data: undefined,
error: null,
isPending: true,
isError: false,
isSuccess: false,
})
render(<Subscription />)
expect(screen.queryByRole('link', { name: /AWS Marketplace/ })).not.toBeInTheDocument()
})
it('hides the AWS Marketplace link for partner-billed organizations', () => {
mockSubscription.mockReturnValue({
data: {
plan: { id: 'free', name: 'Free' },
usage_billing_enabled: true,
billing_via_partner: true,
},
error: null,
isPending: false,
isError: false,
isSuccess: true,
})
render(<Subscription />)
expect(screen.queryByRole('link', { name: /AWS Marketplace/ })).not.toBeInTheDocument()
})
it('hides the AWS Marketplace link when the organization is not Supabase-managed', () => {
mockSelectedOrganization.mockReturnValue({ managed_by: 'aws_marketplace' })
render(<Subscription />)
expect(screen.queryByRole('link', { name: /AWS Marketplace/ })).not.toBeInTheDocument()
})
it('hides the AWS Marketplace link when billing-read is denied even if subscription is cached', () => {
mockPermissions.mockReturnValue({ can: false, isSuccess: true })
render(<Subscription />)
expect(screen.getByText(/you need additional permissions/i)).toBeInTheDocument()
expect(screen.queryByRole('link', { name: /AWS Marketplace/ })).not.toBeInTheDocument()
})
})