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((
child
)) expect(screen.getByText('child')).toBeInTheDocument() }) }) describe('Effects', () => { it('should redirect dataset operators to /datasets', async () => { ;(useAppContext as Mock).mockReturnValue({ isCurrentWorkspaceDatasetOperator: true, }) render((
child
)) await waitFor(() => { expect(mockReplace).toHaveBeenCalledWith('/datasets') }) }) it('should not redirect non dataset operators', () => { render((
child
)) expect(mockReplace).not.toHaveBeenCalled() }) }) })