Files
dify/web/app/components/develop/ApiServer.spec.tsx
qiuqiua 9ef6b90843 feat: sync main branch (#31938)
Signed-off-by: majiayu000 <1835304752@qq.com>
Signed-off-by: dependabot[bot] <support@github.com>
Signed-off-by: NeatGuyCoding <15627489+NeatGuyCoding@users.noreply.github.com>
Signed-off-by: -LAN- <laipz8200@outlook.com>
Signed-off-by: yihong0618 <zouzou0208@gmail.com>
Co-authored-by: QuantumGhost <obelisk.reg+git@gmail.com>
Co-authored-by: 盐粒 Yanli <yanli@dify.ai>
Co-authored-by: wangxiaolei <fatelei@gmail.com>
Co-authored-by: Stephen Zhou <38493346+hyoban@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: Cursx <33718736+Cursx@users.noreply.github.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: lif <1835304752@qq.com>
Co-authored-by: 非法操作 <hjlarry@163.com>
Co-authored-by: Asuka Minato <i@asukaminato.eu.org>
Co-authored-by: fenglin <790872612@qq.com>
Co-authored-by: qiaofenglin <qiaofenglin@baidu.com>
Co-authored-by: -LAN- <laipz8200@outlook.com>
Co-authored-by: TomoOkuyama <49631611+TomoOkuyama@users.noreply.github.com>
Co-authored-by: Tomo Okuyama <tomo.okuyama@intersystems.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: zyssyz123 <916125788@qq.com>
Co-authored-by: hj24 <mambahj24@gmail.com>
Co-authored-by: Coding On Star <447357187@qq.com>
Co-authored-by: CodingOnStar <hanxujiang@dify.ai>
Co-authored-by: yyh <92089059+lyzno1@users.noreply.github.com>
Co-authored-by: Xiangxuan Qu <fghpdf@outlook.com>
Co-authored-by: fghpdf <fghpdf@users.noreply.github.com>
Co-authored-by: coopercoder <whitetiger0127@163.com>
Co-authored-by: zhaiguangpeng <zhaiguangpeng@didiglobal.com>
Co-authored-by: Junyan Qin (Chin) <rockchinq@gmail.com>
Co-authored-by: E.G <146701565+GlobalStar117@users.noreply.github.com>
Co-authored-by: GlobalStar117 <GlobalStar117@users.noreply.github.com>
Co-authored-by: Claude Haiku 4.5 <noreply@anthropic.com>
Co-authored-by: CodingOnStar <hanxujiang@dify.com>
Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com>
Co-authored-by: heyszt <270985384@qq.com>
Co-authored-by: NeatGuyCoding <15627489+NeatGuyCoding@users.noreply.github.com>
Co-authored-by: Yeuoly <45712896+Yeuoly@users.noreply.github.com>
Co-authored-by: zxhlyh <jasonapring2015@outlook.com>
Co-authored-by: moonpanda <chuanzegao@163.com>
Co-authored-by: warlocgao <warlocgao@tencent.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: claude[bot] <41898282+claude[bot]@users.noreply.github.com>
Co-authored-by: KVOJJJin <jzongcode@gmail.com>
Co-authored-by: eux <euxx@users.noreply.github.com>
Co-authored-by: bangjiehan <bangjiehan@gmail.com>
Co-authored-by: FFXN <31929997+FFXN@users.noreply.github.com>
Co-authored-by: Jyong <76649700+JohnJyong@users.noreply.github.com>
Co-authored-by: Nie Ronghua <nieronghua@sf-express.com>
Co-authored-by: JQSevenMiao <141806521+JQSevenMiao@users.noreply.github.com>
Co-authored-by: jiasiqi <jiasiqi3@tal.com>
Co-authored-by: Seokrin Taron Sung <sungsjade@gmail.com>
Co-authored-by: CrabSAMA <40541269+CrabSAMA@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: yihong <zouzou0208@gmail.com>
Co-authored-by: Joel <iamjoel007@gmail.com>
Co-authored-by: Wu Tianwei <30284043+WTW0313@users.noreply.github.com>
Co-authored-by: yessenia <yessenia.contact@gmail.com>
Co-authored-by: Jax <anobaka@qq.com>
Co-authored-by: niveshdandyan <155956228+niveshdandyan@users.noreply.github.com>
Co-authored-by: OSS Contributor <oss-contributor@example.com>
Co-authored-by: niveshdandyan <niveshdandyan@users.noreply.github.com>
Co-authored-by: Sean Kenneth Doherty <Smaster7772@gmail.com>
2026-02-04 19:04:24 +08:00

221 lines
7.9 KiB
TypeScript

import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { act } from 'react'
import ApiServer from './ApiServer'
// Mock the secret-key-modal since it involves complex API interactions
vi.mock('@/app/components/develop/secret-key/secret-key-modal', () => ({
default: ({ isShow, onClose }: { isShow: boolean, onClose: () => void }) => (
isShow ? <div data-testid="secret-key-modal"><button onClick={onClose}>Close Modal</button></div> : null
),
}))
describe('ApiServer', () => {
const defaultProps = {
apiBaseUrl: 'https://api.example.com',
}
describe('rendering', () => {
it('should render the API server label', () => {
render(<ApiServer {...defaultProps} />)
expect(screen.getByText('appApi.apiServer')).toBeInTheDocument()
})
it('should render the API base URL', () => {
render(<ApiServer {...defaultProps} />)
expect(screen.getByText('https://api.example.com')).toBeInTheDocument()
})
it('should render the OK status badge', () => {
render(<ApiServer {...defaultProps} />)
expect(screen.getByText('appApi.ok')).toBeInTheDocument()
})
it('should render the API key button', () => {
render(<ApiServer {...defaultProps} />)
expect(screen.getByText('appApi.apiKey')).toBeInTheDocument()
})
it('should render CopyFeedback component', () => {
render(<ApiServer {...defaultProps} />)
// CopyFeedback renders a button for copying
const copyButtons = screen.getAllByRole('button')
expect(copyButtons.length).toBeGreaterThan(0)
})
})
describe('with different API URLs', () => {
it('should render localhost URL', () => {
render(<ApiServer apiBaseUrl="http://localhost:3000/api" />)
expect(screen.getByText('http://localhost:3000/api')).toBeInTheDocument()
})
it('should render production URL', () => {
render(<ApiServer apiBaseUrl="https://api.dify.ai/v1" />)
expect(screen.getByText('https://api.dify.ai/v1')).toBeInTheDocument()
})
it('should render URL with path', () => {
render(<ApiServer apiBaseUrl="https://api.example.com/v1/chat" />)
expect(screen.getByText('https://api.example.com/v1/chat')).toBeInTheDocument()
})
})
describe('with appId prop', () => {
it('should render without appId', () => {
render(<ApiServer apiBaseUrl="https://api.example.com" />)
expect(screen.getByText('https://api.example.com')).toBeInTheDocument()
})
it('should render with appId', () => {
render(<ApiServer apiBaseUrl="https://api.example.com" appId="app-123" />)
expect(screen.getByText('https://api.example.com')).toBeInTheDocument()
})
})
describe('SecretKeyButton interaction', () => {
it('should open modal when API key button is clicked', async () => {
const user = userEvent.setup()
render(<ApiServer {...defaultProps} appId="app-123" />)
const apiKeyButton = screen.getByText('appApi.apiKey')
await act(async () => {
await user.click(apiKeyButton)
})
expect(screen.getByTestId('secret-key-modal')).toBeInTheDocument()
})
it('should close modal when close button is clicked', async () => {
const user = userEvent.setup()
render(<ApiServer {...defaultProps} appId="app-123" />)
// Open modal
const apiKeyButton = screen.getByText('appApi.apiKey')
await act(async () => {
await user.click(apiKeyButton)
})
expect(screen.getByTestId('secret-key-modal')).toBeInTheDocument()
// Close modal
const closeButton = screen.getByText('Close Modal')
await act(async () => {
await user.click(closeButton)
})
expect(screen.queryByTestId('secret-key-modal')).not.toBeInTheDocument()
})
})
describe('styling', () => {
it('should have flex layout with wrap', () => {
const { container } = render(<ApiServer {...defaultProps} />)
const wrapper = container.firstChild as HTMLElement
expect(wrapper.className).toContain('flex')
expect(wrapper.className).toContain('flex-wrap')
})
it('should have items-center alignment', () => {
const { container } = render(<ApiServer {...defaultProps} />)
const wrapper = container.firstChild as HTMLElement
expect(wrapper.className).toContain('items-center')
})
it('should have gap-y-2 for vertical spacing', () => {
const { container } = render(<ApiServer {...defaultProps} />)
const wrapper = container.firstChild as HTMLElement
expect(wrapper.className).toContain('gap-y-2')
})
it('should apply green styling to OK badge', () => {
render(<ApiServer {...defaultProps} />)
const okBadge = screen.getByText('appApi.ok')
expect(okBadge.className).toContain('bg-[#ECFDF3]')
expect(okBadge.className).toContain('text-[#039855]')
})
it('should have border styling on URL container', () => {
render(<ApiServer {...defaultProps} />)
const urlText = screen.getByText('https://api.example.com')
const urlContainer = urlText.closest('div[class*="rounded-lg"]')
expect(urlContainer).toBeInTheDocument()
})
})
describe('API server label', () => {
it('should have correct styling for label', () => {
render(<ApiServer {...defaultProps} />)
const label = screen.getByText('appApi.apiServer')
expect(label.className).toContain('rounded-md')
expect(label.className).toContain('border')
})
it('should have tertiary text color on label', () => {
render(<ApiServer {...defaultProps} />)
const label = screen.getByText('appApi.apiServer')
expect(label.className).toContain('text-text-tertiary')
})
})
describe('URL display', () => {
it('should have truncate class for long URLs', () => {
render(<ApiServer {...defaultProps} />)
const urlText = screen.getByText('https://api.example.com')
expect(urlText.className).toContain('truncate')
})
it('should have font-medium class on URL', () => {
render(<ApiServer {...defaultProps} />)
const urlText = screen.getByText('https://api.example.com')
expect(urlText.className).toContain('font-medium')
})
it('should have secondary text color on URL', () => {
render(<ApiServer {...defaultProps} />)
const urlText = screen.getByText('https://api.example.com')
expect(urlText.className).toContain('text-text-secondary')
})
})
describe('divider', () => {
it('should render vertical divider between URL and copy button', () => {
const { container } = render(<ApiServer {...defaultProps} />)
const divider = container.querySelector('.bg-divider-regular')
expect(divider).toBeInTheDocument()
})
it('should have correct divider dimensions', () => {
const { container } = render(<ApiServer {...defaultProps} />)
const divider = container.querySelector('.bg-divider-regular')
expect(divider?.className).toContain('h-[14px]')
expect(divider?.className).toContain('w-[1px]')
})
})
describe('SecretKeyButton styling', () => {
it('should have shrink-0 class to prevent shrinking', () => {
render(<ApiServer {...defaultProps} appId="app-123" />)
// The SecretKeyButton wraps a Button component
const button = screen.getByRole('button', { name: /apiKey/i })
// Check parent container has shrink-0
const buttonContainer = button.closest('.shrink-0')
expect(buttonContainer).toBeInTheDocument()
})
})
describe('accessibility', () => {
it('should have accessible button for API key', () => {
render(<ApiServer {...defaultProps} />)
const button = screen.getByRole('button', { name: /apiKey/i })
expect(button).toBeInTheDocument()
})
it('should have multiple buttons (copy + API key)', () => {
render(<ApiServer {...defaultProps} />)
const buttons = screen.getAllByRole('button')
expect(buttons.length).toBeGreaterThanOrEqual(2)
})
})
})