Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

9.1 KB
Raw Blame
import { QueryClient } from '@tanstack/react-query'
import { fireEvent, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { platformComponents as components } from 'api-types'
import { mockAnimationsApi } from 'jsdom-testing-mocks'
import { HttpResponse } from 'msw'
import { beforeEach, describe, expect, test, vi } from 'vitest'

import BillingEmail from './BillingEmail'
import { organizationKeys } from '@/data/organizations/keys'
import { FGA_PERMISSIONS } from '@/hooks/misc/useCheckPermissionsV2'
import { customRender } from '@/tests/lib/custom-render'
import { addAPIMock } from '@/tests/lib/msw'

type CustomerResponse = components['schemas']['CustomerResponse_Output']

// The additional-emails control renders a Radix Popover when adding a recipient.
mockAnimationsApi()

const SLUG = 'acme-org'

const mockCheckPermissions = vi.hoisted(() => vi.fn())

vi.mock('common', async (importOriginal) => {
  const original = (await importOriginal()) as typeof import('common')
  return {
    ...original,
    // The customer-profile query is platform-only; IS_PLATFORM is false by default in tests.
    IS_PLATFORM: true,
    useParams: () => ({ slug: SLUG }),
  }
})

vi.mock('@/hooks/misc/useCheckPermissionsV2', async (importOriginal) => ({
  ...(await importOriginal<typeof import('@/hooks/misc/useCheckPermissionsV2')>()),
  useAsyncCheckPermissionsV2: (permission: string) => mockCheckPermissions(permission),
}))

vi.mock('react-intersection-observer', () => ({
  useInView: () => ({ ref: vi.fn(), inView: true }),
}))

const createCustomerProfileResponse = (
  overrides: Partial<CustomerResponse> = {}
): CustomerResponse => ({
  additional_emails: [],
  balance: 0,
  billing_via_partner: false,
  email: '[email protected]',
  tax_id: null,
  ...overrides,
})

const mockCustomerProfile = (overrides: Partial<CustomerResponse> = {}) => {
  addAPIMock({
    method: 'get',
    path: '/platform/organizations/:slug/customer',
    response: () => HttpResponse.json<CustomerResponse>(createCustomerProfileResponse(overrides)),
  })
}

const mockUpdateCustomerProfile = () => {
  const requests: Array<{ slug: string | undefined; body: unknown }> = []
  addAPIMock({
    method: 'put',
    path: '/platform/organizations/:slug/customer',
    response: async ({ request, params }) => {
      requests.push({ slug: params.slug as string | undefined, body: await request.json() })
      return HttpResponse.json({}, { status: 204 })
    },
  })
  return requests
}

const addRecipient = async (email: string) => {
  const input = screen
    .getAllByRole('combobox')
    .find((element): element is HTMLInputElement => element instanceof HTMLInputElement)

  expect(input).toBeDefined()
  if (!input) return

  await userEvent.click(input)
  await waitFor(() => expect(input).toHaveAttribute('aria-expanded', 'true'))
  // fireEvent.change (rather than userEvent.type) avoids racing the popover's open-state
  // transition character-by-character, which otherwise drops the first keystroke(s).
  fireEvent.change(input, { target: { value: email } })
  fireEvent.click(await screen.findByRole('option', { name: new RegExp(`Create "${email}"`) }))
}

const removeRecipient = (email: string) => {
  const badge = screen.getByText(email)
  fireEvent.click(badge.querySelector('svg')!.parentElement!)
}

describe('BillingEmail', () => {
  beforeEach(() => {
    mockCheckPermissions.mockImplementation(() => ({ can: true, isSuccess: true }))
  })

  test('renders the current billing email and additional recipients', async () => {
    mockCustomerProfile({
      email: '[email protected]',
      additional_emails: ['[email protected]'],
    })

    customRender(<BillingEmail />)

    expect(await screen.findByPlaceholderText('Email')).toHaveValue('[email protected]')
    expect(screen.getByText('[email protected]')).toBeInTheDocument()
  })

  test('shows a permission notice when the user cannot read billing data', async () => {
    mockCheckPermissions.mockImplementation((permission) => ({
      can: permission !== FGA_PERMISSIONS.ORGANIZATION.BILLING_READ,
      isSuccess: true,
    }))

    customRender(<BillingEmail />)

    expect(await screen.findByText(/view this organization's email recipients/)).toBeInTheDocument()
    expect(screen.queryByPlaceholderText('Email')).not.toBeInTheDocument()
  })

  test('disables the email controls when the user cannot update billing data', async () => {
    mockCheckPermissions.mockImplementation((permission) => ({
      can: permission !== FGA_PERMISSIONS.ORGANIZATION.BILLING_WRITE,
      isSuccess: true,
    }))
    mockCustomerProfile({ email: '[email protected]', additional_emails: [] })

    customRender(<BillingEmail />)

    expect(await screen.findByPlaceholderText('Email')).toBeDisabled()
    expect(await screen.findByPlaceholderText('Add additional recipients')).toBeDisabled()
    expect(
      screen.getByText('You need additional permissions to update billing emails')
    ).toBeInTheDocument()
  })

  test('saves the updated email while keeping the existing additional recipients', async () => {
    mockCustomerProfile({
      email: '[email protected]',
      additional_emails: ['[email protected]'],
    })
    const requests = mockUpdateCustomerProfile()

    customRender(<BillingEmail />)

    const emailInput = await screen.findByPlaceholderText('Email')
    await userEvent.clear(emailInput)
    await userEvent.paste('[email protected]')

    fireEvent.click(await screen.findByRole('button', { name: 'Save' }))

    await waitFor(() => expect(requests).toHaveLength(1))
    expect(requests[0]).toEqual({
      slug: SLUG,
      body: { email: '[email protected]', additional_emails: ['[email protected]'] },
    })
  })

  test('adds a recipient when there are none', async () => {
    mockCustomerProfile({ email: '[email protected]', additional_emails: [] })
    const requests = mockUpdateCustomerProfile()

    customRender(<BillingEmail />)
    await screen.findByPlaceholderText('Email')

    await addRecipient('[email protected]')
    fireEvent.click(await screen.findByRole('button', { name: 'Save' }))

    await waitFor(() => expect(requests).toHaveLength(1))
    expect(requests[0]).toEqual({
      slug: SLUG,
      body: { email: '[email protected]', additional_emails: ['[email protected]'] },
    })
  })

  test('removes a recipient when there is one', async () => {
    mockCustomerProfile({ email: '[email protected]', additional_emails: ['[email protected]'] })
    const requests = mockUpdateCustomerProfile()

    customRender(<BillingEmail />)
    await screen.findByText('[email protected]')

    removeRecipient('[email protected]')
    fireEvent.click(await screen.findByRole('button', { name: 'Save' }))

    await waitFor(() => expect(requests).toHaveLength(1))
    expect(requests[0]).toEqual({
      slug: SLUG,
      body: { email: '[email protected]', additional_emails: [] },
    })
  })

  test('appends a recipient when there is already one', async () => {
    mockCustomerProfile({
      email: '[email protected]',
      additional_emails: ['[email protected]'],
    })
    const requests = mockUpdateCustomerProfile()

    customRender(<BillingEmail />)
    await screen.findByText('[email protected]')

    await addRecipient('[email protected]')
    fireEvent.click(await screen.findByRole('button', { name: 'Save' }))

    await waitFor(() => expect(requests).toHaveLength(1))
    expect(requests[0]).toEqual({
      slug: SLUG,
      body: {
        email: '[email protected]',
        additional_emails: ['[email protected]', '[email protected]'],
      },
    })
  })

  test('keeps an in-progress edit when the profile data refetches', async () => {
    const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } })
    let getCallCount = 0
    let customerProfile = createCustomerProfileResponse({
      email: '[email protected]',
      additional_emails: ['[email protected]'],
    })
    addAPIMock({
      method: 'get',
      path: '/platform/organizations/:slug/customer',
      response: () => {
        getCallCount++
        return HttpResponse.json<CustomerResponse>(customerProfile)
      },
    })

    customRender(<BillingEmail />, { queryClient })

    const emailInput = await screen.findByPlaceholderText('Email')
    await waitFor(() => expect(emailInput).toHaveValue('[email protected]'))

    await userEvent.clear(emailInput)
    await userEvent.paste('[email protected]')

    // Simulate an unrelated refetch of the same shared customer-profile query (e.g. triggered
    // by another section, or a background refetch) returning newer server data.
    customerProfile = createCustomerProfileResponse({
      email: '[email protected]',
      additional_emails: ['[email protected]'],
    })
    await queryClient.invalidateQueries({ queryKey: organizationKeys.customerProfile(SLUG) })
    await waitFor(() => expect(getCallCount).toBeGreaterThanOrEqual(2))

    // The dirty form is left untouched - not overwritten with the newly fetched data.
    expect(emailInput).toHaveValue('[email protected]')
    expect(screen.getByText('[email protected]')).toBeInTheDocument()
    expect(screen.queryByText('[email protected]')).not.toBeInTheDocument()
  })
})