Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

3.0 KB
Raw Blame
import type { InstalledAppResponse } from '@dify/contracts/api/console/installed-apps/types.gen'
import type { AnchorHTMLAttributes, ReactNode } from 'react'
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import AppNavItem from '../app-nav-item'

vi.mock('@/next/link', () => ({
  default: ({
    children,
    href,
    prefetch,
    ...props
  }: AnchorHTMLAttributes<HTMLAnchorElement> & {
    children?: ReactNode
    href: string
    prefetch?: boolean | null
  }) => (
    <a href={href} data-prefetch={prefetch === null ? 'auto' : prefetch} {...props}>
      {children}
    </a>
  ),
}))

const baseProps = {
  app: {
    id: 'app-123',
    app_owner_tenant_id: 'tenant-1',
    editable: true,
    is_pinned: false,
    last_used_at: null,
    app: {
      id: 'source-app-123',
      name: 'My App',
      description: 'Description',
      mode: 'chat',
      icon_type: 'emoji',
      icon: '🤖',
      icon_background: '#fff',
      icon_url: null,
      use_icon_as_answer_icon: false,
    },
  } satisfies InstalledAppResponse,
  isSelected: false,
  onTogglePin: vi.fn(),
}

describe('AppNavItem', () => {
  beforeEach(() => {
    vi.clearAllMocks()
  })

  describe('User Interactions', () => {
    it('should render installed app navigation as a link', () => {
      render(<AppNavItem {...baseProps} />)

      const link = screen.getByRole('link', { name: 'My App' })

      expect(link).toHaveAttribute('href', '/installed/app-123')
      expect(link).not.toHaveAttribute('aria-current')
      expect(link).toHaveAttribute('data-prefetch', 'false')
    })

    it('should enable prefetch after pointer intent', async () => {
      const user = userEvent.setup()
      render(<AppNavItem {...baseProps} />)

      const link = screen.getByRole('link', { name: 'My App' })

      expect(link).toHaveAttribute('data-prefetch', 'false')

      await user.hover(link)

      expect(link).toHaveAttribute('data-prefetch', 'auto')
    })

    it('should enable prefetch after keyboard focus', async () => {
      const user = userEvent.setup()
      render(<AppNavItem {...baseProps} />)

      const link = screen.getByRole('link', { name: 'My App' })

      expect(link).toHaveAttribute('data-prefetch', 'false')

      await user.tab()

      expect(link).toHaveFocus()
      expect(link).toHaveAttribute('data-prefetch', 'auto')
    })

    it('should expose selected state through the current link', () => {
      render(<AppNavItem {...baseProps} isSelected />)

      const link = screen.getByRole('link', { name: 'My App' })

      expect(link).toHaveAttribute('aria-current', 'page')
    })

    it('should request the next pin state', async () => {
      const user = userEvent.setup()
      render(<AppNavItem {...baseProps} />)

      await user.click(
        screen.getByRole('button', { name: /common\.operation\.moreActionsFor.*My App/ }),
      )
      await user.click(await screen.findByText('explore.sidebar.action.pin'))

      expect(baseProps.onTogglePin).toHaveBeenCalledWith('app-123', true)
    })
  })
})