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.

8.0 KB
Raw Blame
import { Dialog, DialogContent, DialogTitle } from '@langgenius/dify-ui/dialog'
import { DirectionProvider } from '@langgenius/dify-ui/direction-provider'
import { useQueryState } from 'nuqs'
import { page, userEvent } from 'vite-plus/test/browser'
import { render } from 'vitest-browser-react'
import {
  settingsQueryParamName,
  settingsQueryParser,
} from '@/app/components/header/account-setting/query-params'
import AccountSection from '@/app/components/main-nav/components/account-section'
import { createConsoleQueryWrapper } from '@/test/console/query-data'
import { createNuqsTestWrapper } from '@/test/nuqs-testing'
import AccountMenu from '../index'

vi.mock('@/service/use-common', () => ({ useLogout: () => ({ mutate: vi.fn() }) }))
vi.mock('next-themes', () => ({
  useTheme: () => ({ theme: 'system', setTheme: vi.fn() }),
}))

// The settings contents are outside the menu's focus contract; retain a real destination dialog.
function PreferencesDestination() {
  const [destination] = useQueryState(settingsQueryParamName, settingsQueryParser)
  return (
    <Dialog open={destination === 'preferences'}>
      <DialogContent>
        <DialogTitle>Preferences destination</DialogTitle>
        <label>
          Preference
          <input />
        </label>
      </DialogContent>
    </Dialog>
  )
}

async function renderAccount() {
  const { wrapper: ConsoleProvider } = createConsoleQueryWrapper({
    accountProfile: { name: 'Current User', email: '[email protected]' },
    features: { education: { enabled: false } },
  })
  const { wrapper: NuqsProvider } = createNuqsTestWrapper()
  return render(
    <ConsoleProvider>
      <NuqsProvider>
        <div style={{ padding: 40 }}>
          <button type="button">Before account</button>
          <AccountSection />
        </div>
        <PreferencesDestination />
      </NuqsProvider>
    </ConsoleProvider>,
  )
}

it.each(['ltr', 'rtl'] as const)(
  'opens the appearance submenu toward the reading direction in %s',
  async (direction) => {
    await page.viewport(1280, 800)
    const { wrapper: ConsoleProvider } = createConsoleQueryWrapper({
      accountProfile: { name: 'Current User', email: '[email protected]' },
      features: { education: { enabled: false } },
    })
    const { wrapper: NuqsProvider } = createNuqsTestWrapper()
    const previousDirection = document.documentElement.dir
    document.documentElement.dir = direction
    try {
      const screen = await render(
        <DirectionProvider direction={direction}>
          <ConsoleProvider>
            <NuqsProvider>
              <div style={{ marginInline: 'auto', paddingTop: 300, width: 240 }}>
                <AccountMenu
                  trigger={({ ariaLabel }) => <button aria-label={ariaLabel}>Account</button>}
                />
              </div>
            </NuqsProvider>
          </ConsoleProvider>
        </DirectionProvider>,
      )
      await screen.getByRole('button', { name: 'accountSettings.account.account' }).click()
      const appearance = screen.getByRole('menuitem', {
        name: 'common.theme.label',
      })
      await userEvent.hover(appearance)
      const submenu = screen.getByRole('menu', { name: 'common.theme.label' })
      await expect.element(submenu).toBeVisible()
      // Leave room on both sides so collision fallback cannot hide a fixed-side override.
      await expect
        .poll(() => {
          const triggerBounds = appearance.element().getBoundingClientRect()
          const submenuBounds = submenu.element().getBoundingClientRect()
          return direction === 'rtl'
            ? submenuBounds.right <= triggerBounds.left
            : submenuBounds.left >= triggerBounds.right
        })
        .toBe(true)
      await userEvent.hover(screen.getByRole('menuitemradio', { name: 'common.theme.light' }))
      await expect.element(submenu).toBeVisible()
      await userEvent.keyboard('{Escape}')
      await expect.element(appearance).toHaveFocus()
    } finally {
      document.documentElement.dir = previousDirection
    }
  },
)

// Chromium supplies the native focus-visible heuristic after menu focus restoration.
it.each(['outside', 'Escape'])(
  'does not highlight a pointer-opened account trigger after %s dismissal',
  async (dismiss) => {
    const screen = await renderAccount()
    const trigger = screen.getByRole('button', {
      name: 'Current User accountSettings.account.account',
    })
    await trigger.click()
    await expect.element(screen.getByRole('menu')).toBeVisible()
    if (dismiss === 'Escape') await userEvent.keyboard('{Escape}')
    else
      await screen
        .getByRole('presentation')
        .filter({ hasNotText: '[email protected]' })
        .click({ position: { x: 10, y: 600 } })
    await expect.element(screen.getByRole('menu')).not.toBeInTheDocument()
    if (dismiss === 'Escape') await expect.element(trigger).toHaveFocus()
    expect(trigger.element().matches(':focus-visible')).toBe(false)
  },
)

it('keeps focus in Preferences after the account menu finishes closing', async () => {
  const settings = globalThis as typeof globalThis & { BASE_UI_ANIMATIONS_DISABLED: boolean }
  const animationsDisabled = settings.BASE_UI_ANIMATIONS_DISABLED
  settings.BASE_UI_ANIMATIONS_DISABLED = false
  try {
    const screen = await renderAccount()
    await screen
      .getByRole('button', { name: 'Current User accountSettings.account.account' })
      .click()
    await screen.getByRole('menuitem', { name: 'navigation.settings.preferences' }).click()
    const input = screen.getByRole('textbox', { name: 'Preference' })
    await expect.element(input).toHaveFocus()
    await expect.element(screen.getByRole('menu')).not.toBeInTheDocument()
    await expect.element(input).toHaveFocus()
  } finally {
    settings.BASE_UI_ANIMATIONS_DISABLED = animationsDisabled
  }
})

it.each(['keyboard', 'pointer then keyboard'])(
  'keeps visible focus after %s navigation and Escape dismissal',
  async (interaction) => {
    const screen = await renderAccount()
    const trigger = screen.getByRole('button', {
      name: 'Current User accountSettings.account.account',
    })
    if (interaction === 'keyboard') {
      await screen.getByRole('button', { name: 'Before account' }).click()
      await userEvent.keyboard('{Tab}{Enter}')
    } else {
      await trigger.click()
      await userEvent.keyboard('{ArrowDown}')
    }
    await expect.element(screen.getByRole('menu')).toBeVisible()
    await userEvent.keyboard('{Escape}')
    await expect.element(trigger).toHaveFocus()
    expect(trigger.element().matches(':focus-visible')).toBe(true)
    expect(getComputedStyle(trigger.element()).boxShadow).not.toBe('none')
  },
)

it('clears the keyboard focus ring when the same account menu is reopened and dismissed with the pointer', async () => {
  const settings = globalThis as typeof globalThis & { BASE_UI_ANIMATIONS_DISABLED: boolean }
  const animationsDisabled = settings.BASE_UI_ANIMATIONS_DISABLED
  settings.BASE_UI_ANIMATIONS_DISABLED = false
  try {
    const screen = await renderAccount()
    const trigger = screen.getByRole('button', {
      name: 'Current User accountSettings.account.account',
    })
    const menu = screen.getByRole('menu')
    const dismissWithPointer = async () => {
      await trigger.click()
      await expect.element(menu).toBeVisible()
      await screen
        .getByRole('presentation')
        .filter({ hasNotText: '[email protected]' })
        .click({ position: { x: 10, y: 600 } })
      await expect.element(menu).not.toBeInTheDocument()
      expect(trigger.element().matches(':focus-visible')).toBe(false)
    }

    await dismissWithPointer()

    await screen.getByRole('button', { name: 'Before account' }).click()
    await userEvent.keyboard('{Tab}{Enter}{ArrowDown}')
    await expect.element(menu).toBeVisible()
    await userEvent.keyboard('{Escape}')
    await expect.element(menu).not.toBeInTheDocument()
    await expect.element(trigger).toHaveFocus()
    expect(trigger.element().matches(':focus-visible')).toBe(true)

    await dismissWithPointer()
  } finally {
    settings.BASE_UI_ANIMATIONS_DISABLED = animationsDisabled
  }
})