dify/web/app/components/header/license-env/__tests__/index.spec.tsx
Coding On Star 6da802eb2a
refactor(custom): reorganize web app brand module and raise coverage threshold (#33531)
Co-authored-by: CodingOnStar <hanxujiang@dify.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2026-03-16 18:17:21 +08:00

93 lines
2.7 KiB
TypeScript

import { render, screen } from '@testing-library/react'
import dayjs from 'dayjs'
import { useGlobalPublicStore } from '@/context/global-public-context'
import { defaultSystemFeatures, LicenseStatus } from '@/types/feature'
import LicenseNav from '../index'
describe('LicenseNav', () => {
beforeEach(() => {
vi.clearAllMocks()
vi.useFakeTimers()
const now = new Date('2024-01-01T12:00:00Z')
vi.setSystemTime(now)
useGlobalPublicStore.setState({
systemFeatures: defaultSystemFeatures,
})
})
afterEach(() => {
vi.useRealTimers()
})
it('should render null when license status is NONE', () => {
const { container } = render(<LicenseNav />)
expect(container).toBeEmptyDOMElement()
})
it('should render Enterprise badge when license status is ACTIVE', () => {
useGlobalPublicStore.setState({
systemFeatures: {
...defaultSystemFeatures,
license: {
status: LicenseStatus.ACTIVE,
expired_at: null,
},
},
})
render(<LicenseNav />)
expect(screen.getByText('Enterprise')).toBeInTheDocument()
})
it('should render singular expiring message when license expires in 0 days', () => {
const expiredAt = dayjs().add(2, 'hours').toISOString()
useGlobalPublicStore.setState({
systemFeatures: {
...defaultSystemFeatures,
license: {
status: LicenseStatus.EXPIRING,
expired_at: expiredAt,
},
},
})
render(<LicenseNav />)
expect(screen.getByText(/license\.expiring/)).toBeInTheDocument()
expect(screen.getByText(/count":0/)).toBeInTheDocument()
})
it('should render singular expiring message when license expires in 1 day', () => {
const tomorrow = dayjs().add(1, 'day').add(1, 'hour').toISOString()
useGlobalPublicStore.setState({
systemFeatures: {
...defaultSystemFeatures,
license: {
status: LicenseStatus.EXPIRING,
expired_at: tomorrow,
},
},
})
render(<LicenseNav />)
expect(screen.getByText(/license\.expiring/)).toBeInTheDocument()
expect(screen.getByText(/count":1/)).toBeInTheDocument()
})
it('should render plural expiring message when license expires in 5 days', () => {
const fiveDaysLater = dayjs().add(5, 'day').add(1, 'hour').toISOString()
useGlobalPublicStore.setState({
systemFeatures: {
...defaultSystemFeatures,
license: {
status: LicenseStatus.EXPIRING,
expired_at: fiveDaysLater,
},
},
})
render(<LicenseNav />)
expect(screen.getByText(/license\.expiring_plural/)).toBeInTheDocument()
expect(screen.getByText(/count":5/)).toBeInTheDocument()
})
})