Files
dify/web/app/components/explore/__tests__/index.spec.tsx
yyh d32e23f877 refactor(web): remove ExploreContext and migrate to derived state with oRPC contracts
Replace ExploreContext with derived permission checks using useAppContext
and useMembers, eliminating redundant state synchronization. Add oRPC
contract for explore endpoints, extract TryAppSelection type, and migrate
app-publisher icons from @remixicon/react components to CSS icon classes.
Update all related tests to reflect the new context-free architecture.
2026-02-14 13:24:10 +08:00

93 lines
2.0 KiB
TypeScript

import type { Mock } from 'vitest'
import { render, screen, waitFor } from '@testing-library/react'
import { useAppContext } from '@/context/app-context'
import { MediaType } from '@/hooks/use-breakpoints'
import Explore from '../index'
const mockReplace = vi.fn()
const mockPush = vi.fn()
const mockInstalledAppsData = { installed_apps: [] as const }
vi.mock('next/navigation', () => ({
useRouter: () => ({
replace: mockReplace,
push: mockPush,
}),
useSelectedLayoutSegments: () => ['apps'],
}))
vi.mock('@/hooks/use-breakpoints', () => ({
default: () => MediaType.pc,
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(),
}),
}))
vi.mock('@/context/app-context', () => ({
useAppContext: vi.fn(),
}))
describe('Explore', () => {
beforeEach(() => {
vi.clearAllMocks()
;(useAppContext as Mock).mockReturnValue({
isCurrentWorkspaceDatasetOperator: false,
})
})
describe('Rendering', () => {
it('should render children', () => {
render((
<Explore>
<div>child</div>
</Explore>
))
expect(screen.getByText('child')).toBeInTheDocument()
})
})
describe('Effects', () => {
it('should redirect dataset operators to /datasets', async () => {
;(useAppContext as Mock).mockReturnValue({
isCurrentWorkspaceDatasetOperator: true,
})
render((
<Explore>
<div>child</div>
</Explore>
))
await waitFor(() => {
expect(mockReplace).toHaveBeenCalledWith('/datasets')
})
})
it('should not redirect non dataset operators', () => {
render((
<Explore>
<div>child</div>
</Explore>
))
expect(mockReplace).not.toHaveBeenCalled()
})
})
})