dify/web/app/components/explore/__tests__/index.spec.tsx
Stephen Zhou a84c2d36a3
style: format with vp fmt (#38803)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2026-07-12 15:57:46 +00:00

107 lines
2.3 KiB
TypeScript

import { render, screen } from '@testing-library/react'
import { MediaType } from '@/hooks/use-breakpoints'
import Explore from '../index'
const mockReplace = vi.fn()
const mockPush = vi.fn()
const mockInstalledAppsData = { installed_apps: [] as const }
type MediaTypeValue = (typeof MediaType)[keyof typeof MediaType]
let mockMediaType: MediaTypeValue = MediaType.pc
vi.mock('@/next/navigation', () => ({
usePathname: () => '/explore',
useRouter: () => ({
replace: mockReplace,
push: mockPush,
}),
useSelectedLayoutSegments: () => ['apps'],
}))
vi.mock('@/hooks/use-breakpoints', () => ({
default: () => mockMediaType,
MediaType: {
mobile: 'mobile',
tablet: 'tablet',
pc: 'pc',
},
}))
vi.mock('@/service/use-explore', () => ({
useGetInstalledApps: () => ({
isPending: false,
data: mockInstalledAppsData,
}),
useUninstallApp: () => ({
mutateAsync: vi.fn(),
}),
useUpdateAppPinStatus: () => ({
mutateAsync: vi.fn(),
}),
}))
describe('Explore', () => {
beforeEach(() => {
vi.clearAllMocks()
mockMediaType = MediaType.pc
})
describe('Rendering', () => {
it('should render children', () => {
render(
<Explore>
<div>child</div>
</Explore>,
)
expect(screen.getByText('child')).toBeInTheDocument()
})
it('should not render the legacy explore sidebar on desktop', () => {
render(
<Explore>
<div>child</div>
</Explore>,
)
expect(screen.queryByText('explore.sidebar.title')).not.toBeInTheDocument()
})
it('should keep the legacy explore sidebar on mobile', () => {
mockMediaType = MediaType.mobile
render(
<Explore>
<div>child</div>
</Explore>,
)
expect(screen.getByRole('link', { name: 'explore.sidebar.title' })).toBeInTheDocument()
})
})
describe('Effects', () => {
it('should not redirect at component level', () => {
render(
<Explore>
<div>child</div>
</Explore>,
)
expect(mockReplace).not.toHaveBeenCalled()
})
it('should not redirect on mobile', () => {
mockMediaType = MediaType.mobile
render(
<Explore>
<div>child</div>
</Explore>,
)
expect(mockReplace).not.toHaveBeenCalled()
})
})
})