chore: test for app card and no data (#29570)

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Joel 2025-12-12 16:01:58 +08:00 committed by GitHub
parent e244856ef1
commit 336bcfbae2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 117 additions and 0 deletions

View File

@ -0,0 +1,96 @@
import React from 'react'
import { fireEvent, render, screen } from '@testing-library/react'
import AppCard, { type AppCardProps } from './index'
import type { App } from '@/models/explore'
import { AppModeEnum } from '@/types/app'
jest.mock('react-i18next', () => ({
useTranslation: () => ({
t: (key: string) => key,
}),
}))
jest.mock('@/app/components/base/app-icon', () => ({
__esModule: true,
default: ({ children }: any) => <div data-testid="app-icon">{children}</div>,
}))
jest.mock('../../app/type-selector', () => ({
AppTypeIcon: ({ type }: any) => <div data-testid="app-type-icon">{type}</div>,
}))
const createApp = (overrides?: Partial<App>): App => ({
app_id: 'app-id',
description: 'App description',
copyright: '2024',
privacy_policy: null,
custom_disclaimer: null,
category: 'Assistant',
position: 1,
is_listed: true,
install_count: 0,
installed: false,
editable: true,
is_agent: false,
...overrides,
app: {
id: 'id-1',
mode: AppModeEnum.CHAT,
icon_type: null,
icon: '🤖',
icon_background: '#fff',
icon_url: '',
name: 'Sample App',
description: 'App description',
use_icon_as_answer_icon: false,
...overrides?.app,
},
})
describe('AppCard', () => {
const onCreate = jest.fn()
const renderComponent = (props?: Partial<AppCardProps>) => {
const mergedProps: AppCardProps = {
app: createApp(),
canCreate: false,
onCreate,
isExplore: false,
...props,
}
return render(<AppCard {...mergedProps} />)
}
beforeEach(() => {
jest.clearAllMocks()
})
it('should render app info with correct mode label when mode is CHAT', () => {
renderComponent({ app: createApp({ app: { ...createApp().app, mode: AppModeEnum.CHAT } }) })
expect(screen.getByText('Sample App')).toBeInTheDocument()
expect(screen.getByText('App description')).toBeInTheDocument()
expect(screen.getByText('APP.TYPES.CHATBOT')).toBeInTheDocument()
expect(screen.getByTestId('app-type-icon')).toHaveTextContent(AppModeEnum.CHAT)
})
it('should show create button in explore mode and trigger action', () => {
renderComponent({
app: createApp({ app: { ...createApp().app, mode: AppModeEnum.WORKFLOW } }),
canCreate: true,
isExplore: true,
})
const button = screen.getByText('explore.appCard.addToWorkspace')
expect(button).toBeInTheDocument()
fireEvent.click(button)
expect(onCreate).toHaveBeenCalledTimes(1)
expect(screen.getByText('APP.TYPES.WORKFLOW')).toBeInTheDocument()
})
it('should hide create button when not allowed', () => {
renderComponent({ canCreate: false, isExplore: true })
expect(screen.queryByText('explore.appCard.addToWorkspace')).not.toBeInTheDocument()
})
})

View File

@ -0,0 +1,21 @@
import React from 'react'
import { render, screen } from '@testing-library/react'
import NoData from './index'
jest.mock('react-i18next', () => ({
useTranslation: () => ({
t: (key: string) => key,
}),
}))
describe('NoData', () => {
beforeEach(() => {
jest.clearAllMocks()
})
it('should render empty state icon and text when mounted', () => {
const { container } = render(<NoData />)
expect(container.querySelector('svg')).toBeInTheDocument()
expect(screen.getByText('share.generation.noData')).toBeInTheDocument()
})
})