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.

12.0 KB
Raw Blame
import type * as React from 'react'
import type { RenderResult } from 'vitest-browser-react'
import { render } from 'vitest-browser-react'
import {
  ContextMenu,
  ContextMenuContent,
  ContextMenuItem,
  ContextMenuLinkItem,
  ContextMenuSeparator,
  ContextMenuSubmenu,
  ContextMenuSubmenuTrigger,
  ContextMenuTrigger,
} from '../index'

// Leaves room above and below the trigger so the requested side is not flipped by a collision.
const renderWithRoomAround = (ui: React.ReactNode) =>
  render(<div style={{ padding: '240px 16px' }}>{ui}</div>)

// A context menu is anchored to the pointer, so it is opened with a real right-click: a controlled
// `open` has no pointer position and leaves the menu anchored to nothing.
const openContextMenu = async (screen: RenderResult) => {
  await screen
    .getByLabelText('context trigger')
    .click({ button: 'right', position: { x: 8, y: 8 } })
  await expect.element(screen.getByRole('menu').first()).toBeVisible()
}

describe('context-menu wrapper', () => {
  describe('ContextMenuContent', () => {
    it('should position content at bottom-start with default placement when props are omitted', async () => {
      const screen = await renderWithRoomAround(
        <ContextMenu>
          <ContextMenuTrigger aria-label="context trigger">Open</ContextMenuTrigger>
          <ContextMenuContent>
            <ContextMenuItem>Content action</ContextMenuItem>
          </ContextMenuContent>
        </ContextMenu>,
      )
      await openContextMenu(screen)

      await expect.element(screen.getByRole('menu')).toHaveAttribute('data-side', 'bottom')
      await expect.element(screen.getByRole('menu')).toHaveAttribute('data-align', 'start')
      const triggerRect = screen.getByLabelText('context trigger').element().getBoundingClientRect()
      await expect
        .poll(() => screen.getByRole('menu').element().getBoundingClientRect().left)
        .toBeCloseTo(triggerRect.left + 8, 0)
      await expect
        .poll(() => screen.getByRole('menu').element().getBoundingClientRect().top)
        .toBeCloseTo(triggerRect.top + 8 + 4, 0)

      await expect
        .element(screen.getByRole('menuitem', { name: 'Content action' }))
        .toBeInTheDocument()
    })

    it('should apply custom top placement and keep point-anchor alignment stable when custom positioning props are provided', async () => {
      const screen = await renderWithRoomAround(
        <ContextMenu>
          <ContextMenuTrigger aria-label="context trigger">Open</ContextMenuTrigger>
          <ContextMenuContent side="top" align="end" sideOffset={12} alignOffset={-3}>
            <ContextMenuItem>Custom content</ContextMenuItem>
          </ContextMenuContent>
        </ContextMenu>,
      )
      await openContextMenu(screen)

      await expect.element(screen.getByRole('menu')).toHaveAttribute('data-side', 'top')
      await expect.element(screen.getByRole('menu')).toHaveAttribute('data-align', 'start')
      await expect
        .element(screen.getByRole('menuitem', { name: 'Custom content' }))
        .toBeInTheDocument()
    })

    it('should forward popup attributes and handlers directly', async () => {
      const handlePopupClick = vi.fn()

      const screen = await render(
        <ContextMenu>
          <ContextMenuTrigger aria-label="context trigger">Open</ContextMenuTrigger>
          <ContextMenuContent id="context-content-popup" onClick={handlePopupClick}>
            <ContextMenuItem>Passthrough content</ContextMenuItem>
          </ContextMenuContent>
        </ContextMenu>,
      )
      await openContextMenu(screen)

      await expect.element(screen.getByRole('menu')).toHaveAttribute('id', 'context-content-popup')

      await screen.getByRole('menu').click()

      expect(handlePopupClick).toHaveBeenCalledTimes(1)
    })
  })

  describe('ContextMenuContent inside a submenu', () => {
    it('should place sub-content inline-end-start when side and align are omitted', async () => {
      const screen = await renderWithRoomAround(
        <ContextMenu>
          <ContextMenuTrigger aria-label="context trigger">Open</ContextMenuTrigger>
          <ContextMenuContent>
            <ContextMenuSubmenu open>
              <ContextMenuSubmenuTrigger>More actions</ContextMenuSubmenuTrigger>
              <ContextMenuContent>
                <ContextMenuItem>Sub action</ContextMenuItem>
              </ContextMenuContent>
            </ContextMenuSubmenu>
          </ContextMenuContent>
        </ContextMenu>,
      )
      await openContextMenu(screen)

      const submenu = screen.getByRole('menu', { name: 'More actions' })
      const submenuTrigger = screen.getByRole('menuitem', { name: 'More actions' })

      await expect.element(submenu).toHaveAttribute('data-side', 'inline-end')
      await expect.element(submenu).toHaveAttribute('data-align', 'start')
      // Left-to-right, so the submenu sits to the right of its trigger.
      await expect
        .poll(
          () =>
            submenu.element().getBoundingClientRect().left >=
            submenuTrigger.element().getBoundingClientRect().right - 1,
        )
        .toBe(true)
      await expect.element(screen.getByRole('menuitem', { name: 'Sub action' })).toBeInTheDocument()
    })
  })

  describe('variant prop behavior', () => {
    it.each(['default', 'destructive'] as const)(
      'should remain interactive and set data-variant on item when variant is %s',
      async (variant) => {
        const handleClick = vi.fn()

        const screen = await render(
          <ContextMenu>
            <ContextMenuTrigger aria-label="context trigger">Open</ContextMenuTrigger>
            <ContextMenuContent>
              <ContextMenuItem
                variant={variant}
                aria-label="menu action"
                id={`context-item-${variant}`}
                onClick={handleClick}
              >
                Item label
              </ContextMenuItem>
            </ContextMenuContent>
          </ContextMenu>,
        )
        await openContextMenu(screen)

        await expect
          .element(screen.getByRole('menuitem', { name: 'menu action' }))
          .toHaveAttribute('id', `context-item-${variant}`)
        await expect
          .element(screen.getByRole('menuitem', { name: 'menu action' }))
          .toHaveAttribute('data-variant', variant)

        await screen.getByRole('menuitem', { name: 'menu action' }).click()

        expect(handleClick).toHaveBeenCalledTimes(1)
      },
    )

    it.each(['default', 'destructive'] as const)(
      'should remain interactive and set data-variant on submenu trigger when variant is %s',
      async (variant) => {
        const handleClick = vi.fn()

        const screen = await render(
          <ContextMenu>
            <ContextMenuTrigger aria-label="context trigger">Open</ContextMenuTrigger>
            <ContextMenuContent>
              <ContextMenuSubmenu open>
                <ContextMenuSubmenuTrigger
                  variant={variant}
                  aria-label="submenu action"
                  id={`context-sub-${variant}`}
                  onClick={handleClick}
                >
                  Trigger item
                </ContextMenuSubmenuTrigger>
              </ContextMenuSubmenu>
            </ContextMenuContent>
          </ContextMenu>,
        )
        await openContextMenu(screen)

        await expect
          .element(screen.getByRole('menuitem', { name: 'submenu action' }))
          .toHaveAttribute('id', `context-sub-${variant}`)
        await expect
          .element(screen.getByRole('menuitem', { name: 'submenu action' }))
          .toHaveAttribute('data-variant', variant)

        await screen.getByRole('menuitem', { name: 'submenu action' }).click()

        expect(handleClick).toHaveBeenCalledTimes(1)
      },
    )

    it.each(['default', 'destructive'] as const)(
      'should remain interactive and set data-variant on link item when variant is %s',
      async (variant) => {
        const screen = await render(
          <ContextMenu>
            <ContextMenuTrigger aria-label="context trigger">Open</ContextMenuTrigger>
            <ContextMenuContent>
              <ContextMenuLinkItem
                variant={variant}
                href="https://example.com/docs"
                aria-label="context docs link"
                id={`context-link-${variant}`}
                target="_blank"
                rel="noopener noreferrer"
              >
                Docs
              </ContextMenuLinkItem>
            </ContextMenuContent>
          </ContextMenu>,
        )
        await openContextMenu(screen)

        const link = screen.getByRole('menuitem', { name: 'context docs link' }).element()
        expect(link.tagName.toLowerCase()).toBe('a')
        expect(link).toHaveAttribute('id', `context-link-${variant}`)
        expect(link).toHaveAttribute('data-variant', variant)
      },
    )
  })

  describe('ContextMenuLinkItem close behavior', () => {
    it('should keep link semantics and not leak closeOnClick prop when closeOnClick is false', async () => {
      const screen = await render(
        <ContextMenu>
          <ContextMenuTrigger aria-label="context trigger">Open</ContextMenuTrigger>
          <ContextMenuContent>
            <ContextMenuLinkItem
              href="https://example.com/docs"
              closeOnClick={false}
              aria-label="docs link"
            >
              Docs
            </ContextMenuLinkItem>
          </ContextMenuContent>
        </ContextMenu>,
      )
      await openContextMenu(screen)

      const link = screen.getByRole('menuitem', { name: 'docs link' }).element()
      expect(link.tagName.toLowerCase()).toBe('a')
      expect(link).toHaveAttribute('href', 'https://example.com/docs')
      expect(link).not.toHaveAttribute('closeOnClick')
    })
  })

  describe('ContextMenuTrigger interaction', () => {
    it('should open menu when right-clicking trigger area', async () => {
      const screen = await render(
        <ContextMenu>
          <ContextMenuTrigger aria-label="context trigger area">Trigger area</ContextMenuTrigger>
          <ContextMenuContent>
            <ContextMenuItem>Open on right click</ContextMenuItem>
          </ContextMenuContent>
        </ContextMenu>,
      )

      await screen.getByLabelText('context trigger area').click({ button: 'right' })

      await expect
        .element(screen.getByRole('menuitem', { name: 'Open on right click' }))
        .toBeVisible()
    })
  })

  describe('ContextMenuSeparator', () => {
    it('should render separator and keep surrounding rows when separator is between items', async () => {
      const screen = await render(
        <ContextMenu>
          <ContextMenuTrigger aria-label="context trigger">Open</ContextMenuTrigger>
          <ContextMenuContent>
            <ContextMenuItem>First action</ContextMenuItem>
            <ContextMenuSeparator />
            <ContextMenuItem>Second action</ContextMenuItem>
          </ContextMenuContent>
        </ContextMenu>,
      )
      await openContextMenu(screen)

      await expect
        .element(screen.getByRole('menuitem', { name: 'First action' }))
        .toBeInTheDocument()
      await expect
        .element(screen.getByRole('menuitem', { name: 'Second action' }))
        .toBeInTheDocument()
      expect(screen.getByRole('separator').elements()).toHaveLength(1)
    })
  })
})

it('resolves submenu popup classes with the popup state', async () => {
  const screen = await render(
    <ContextMenu>
      <ContextMenuTrigger>Open audit menu</ContextMenuTrigger>
      <ContextMenuContent>
        <ContextMenuSubmenu open>
          <ContextMenuSubmenuTrigger>More audit actions</ContextMenuSubmenuTrigger>
          <ContextMenuContent className={(state) => (state.open ? 'opacity-50' : 'opacity-100')}>
            <ContextMenuItem>Audit action</ContextMenuItem>
          </ContextMenuContent>
        </ContextMenuSubmenu>
      </ContextMenuContent>
    </ContextMenu>,
  )

  await screen.getByText('Open audit menu').click({ button: 'right' })
  await expect
    .element(screen.getByRole('menu', { name: 'More audit actions' }))
    .toHaveStyle({ opacity: '0.5' })
})