Subscription.test.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { fireEvent, screen } from '@testing-library/react'
  2. import type { ReactNode } from 'react'
  3. import { beforeEach, describe, expect, it, vi } from 'vitest'
  4. import Subscription from './Subscription'
  5. import { render } from '@/tests/helpers'
  6. const { mockSubscription, mockSetPanelKey } = vi.hoisted(() => ({
  7. mockSubscription: vi.fn(),
  8. mockSetPanelKey: vi.fn(),
  9. }))
  10. vi.mock('common', async (importOriginal) => {
  11. const original = (await importOriginal()) as typeof import('common')
  12. return {
  13. ...original,
  14. useParams: () => ({ slug: 'stripe-org' }),
  15. useFlag: () => false,
  16. }
  17. })
  18. vi.mock('@/data/subscriptions/org-subscription-query', () => ({
  19. useOrgSubscriptionQuery: () => mockSubscription(),
  20. }))
  21. vi.mock('@/hooks/misc/useCheckPermissions', () => ({
  22. useAsyncCheckPermissions: () => ({ can: true, isSuccess: true }),
  23. }))
  24. vi.mock('@/state/organization-settings', () => ({
  25. useOrgSettingsPageStateSnapshot: () => ({
  26. setPanelKey: mockSetPanelKey,
  27. }),
  28. }))
  29. vi.mock('../Restriction', () => ({
  30. Restriction: () => null,
  31. }))
  32. vi.mock('../ProjectUpdateDisabledTooltip', () => ({
  33. ProjectUpdateDisabledTooltip: ({ children }: { children: ReactNode }) => children,
  34. }))
  35. vi.mock('./PlanUpdateSidePanel', () => ({
  36. PlanUpdateSidePanel: () => null,
  37. }))
  38. describe('Subscription', () => {
  39. beforeEach(() => {
  40. vi.clearAllMocks()
  41. mockSubscription.mockReturnValue({
  42. data: {
  43. plan: { id: 'free', name: 'Free' },
  44. usage_billing_enabled: true,
  45. },
  46. error: null,
  47. isPending: false,
  48. isError: false,
  49. isSuccess: true,
  50. })
  51. })
  52. it('shows the plan-change CTA and opens the side panel when clicked', () => {
  53. render(<Subscription />)
  54. const button = screen.getByRole('button', { name: 'Change subscription plan' })
  55. expect(button).toBeInTheDocument()
  56. expect(mockSetPanelKey).not.toHaveBeenCalled()
  57. fireEvent.click(button)
  58. expect(mockSetPanelKey).toHaveBeenCalledWith('subscriptionPlan')
  59. })
  60. it('shows the support fallback when plan changes are not available', () => {
  61. mockSubscription.mockReturnValue({
  62. data: {
  63. plan: { id: 'enterprise', name: 'Enterprise' },
  64. usage_billing_enabled: true,
  65. },
  66. error: null,
  67. isPending: false,
  68. isError: false,
  69. isSuccess: true,
  70. })
  71. render(<Subscription />)
  72. expect(
  73. screen.queryByRole('button', { name: 'Change subscription plan' })
  74. ).not.toBeInTheDocument()
  75. expect(screen.getByText('Unable to update plan from Enterprise')).toBeInTheDocument()
  76. })
  77. })