dify/web/app/components/header/plan-badge/__tests__/index.spec.tsx
yyh 66a545fc6d
refactor: centralize deployment edition in system features (#39454)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: GareArc <garethcxy@dify.ai>
2026-07-24 07:15:02 +00:00

82 lines
3.4 KiB
TypeScript

import type { DeploymentEdition } from '@dify/contracts/api/console/system-features/types.gen'
import type { ReactElement } from 'react'
import type { Mock } from 'vitest'
import { fireEvent, screen } from '@testing-library/react'
import { vi } from 'vitest'
import { createMockProviderContextValue } from '@/__mocks__/provider-context'
import { useProviderContext } from '@/context/provider-context'
import { renderWithConsoleQuery } from '@/test/console/query-data'
import { Plan } from '../../../billing/type'
import { PlanBadge } from '../index'
vi.mock('@/context/provider-context', () => ({
useProviderContext: vi.fn(),
baseProviderContextValue: {},
}))
describe('PlanBadge', () => {
const mockUseProviderContext = useProviderContext as Mock
let deploymentEdition: DeploymentEdition = 'CLOUD'
const render = (ui: ReactElement) =>
renderWithConsoleQuery(ui, { systemFeatures: { deployment_edition: deploymentEdition } })
beforeEach(() => {
vi.clearAllMocks()
deploymentEdition = 'CLOUD'
})
it('should return null if isFetchedPlan is false', () => {
mockUseProviderContext.mockReturnValue(createMockProviderContextValue({ isFetchedPlan: false }))
const { container } = render(<PlanBadge plan={Plan.sandbox} />)
expect(container.firstChild).toBeNull()
})
it('should render upgrade action as a button when onClick is provided', () => {
const handleClick = vi.fn()
mockUseProviderContext.mockReturnValue(createMockProviderContextValue({ isFetchedPlan: true }))
render(<PlanBadge plan={Plan.sandbox} sandboxAsUpgrade={true} onClick={handleClick} />)
const button = screen.getByRole('button', { name: 'billing.upgradeBtn.encourageShort' })
fireEvent.click(button)
expect(handleClick).toHaveBeenCalledTimes(1)
})
it('should render sandbox badge instead of upgrade badge in self-hosted edition', () => {
deploymentEdition = 'COMMUNITY'
mockUseProviderContext.mockReturnValue(createMockProviderContextValue({ isFetchedPlan: true }))
render(<PlanBadge plan={Plan.sandbox} sandboxAsUpgrade={true} />)
expect(screen.getByText(Plan.sandbox)).toBeInTheDocument()
expect(screen.queryByText('billing.upgradeBtn.encourageShort')).not.toBeInTheDocument()
expect(screen.queryByRole('button')).not.toBeInTheDocument()
})
it('should render professional badge when plan is professional', () => {
mockUseProviderContext.mockReturnValue(createMockProviderContextValue({ isFetchedPlan: true }))
render(<PlanBadge plan={Plan.professional} />)
expect(screen.getByText('pro')).toBeInTheDocument()
})
it('should render team badge when plan is team', () => {
mockUseProviderContext.mockReturnValue(createMockProviderContextValue({ isFetchedPlan: true }))
render(<PlanBadge plan={Plan.team} />)
expect(screen.getByText(Plan.team)).toBeInTheDocument()
})
it('should return null when plan is enterprise', () => {
mockUseProviderContext.mockReturnValue(createMockProviderContextValue({ isFetchedPlan: true }))
const { container } = render(<PlanBadge plan={Plan.enterprise} />)
expect(container.firstChild).toBeNull()
})
it('should trigger onClick when clicked', () => {
const handleClick = vi.fn()
mockUseProviderContext.mockReturnValue(createMockProviderContextValue({ isFetchedPlan: true }))
render(<PlanBadge plan={Plan.team} onClick={handleClick} />)
fireEvent.click(screen.getByRole('button', { name: Plan.team }))
expect(handleClick).toHaveBeenCalledTimes(1)
})
})