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.

2.2 KB
Raw Blame
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { describe, expect, it, vi } from 'vite-plus/test'
import { ToolDatePicker } from '../tool-date-picker'

vi.mock('react-i18next', async () => {
  const actual = await vi.importActual<typeof import('react-i18next')>('react-i18next')
  const { createReactI18nextMock } = await import('@/test/i18n-mock')
  return {
    ...actual,
    ...createReactI18nextMock({
      'time.operation.pickDate': 'Select date',
      'common.operation.clear': 'Clear',
    }),
  }
})

describe('ToolDatePicker', () => {
  it('marks the trigger read only and hides clearing', () => {
    render(
      <ToolDatePicker
        aria-label="Select date"
        value="2024-05-01"
        onChange={vi.fn()}
        placeholder="Select date"
        readOnly
      />,
    )

    expect(screen.getByRole('button', { name: 'Select date May 1, 2024' })).toHaveAttribute(
      'data-readonly',
    )
    expect(screen.queryByRole('button', { name: 'Clear' })).not.toBeInTheDocument()
  })

  it('opens from the keyboard and restores focus to the date button on dismissal', async () => {
    const user = userEvent.setup()
    render(<ToolDatePicker aria-label="Select date" value="2024-05-01" onChange={vi.fn()} />)

    const trigger = screen.getByRole('button', { name: 'Select date May 1, 2024' })
    await user.tab()
    expect(trigger).toHaveFocus()
    await user.keyboard('{Enter}')
    expect(await screen.findByRole('dialog')).toBeInTheDocument()
    expect(trigger).toHaveAttribute('aria-expanded', 'true')
    await user.keyboard('{Escape}')
    expect(screen.queryByRole('dialog')).not.toBeInTheDocument()
    expect(trigger).toHaveFocus()
  })

  it('lets the keyboard reach and activate clear without opening the calendar', async () => {
    const user = userEvent.setup()
    const onChange = vi.fn()
    render(<ToolDatePicker aria-label="Select date" value="2024-05-01" onChange={onChange} />)

    await user.tab()
    await user.tab()
    expect(screen.getByRole('button', { name: 'Clear' })).toHaveFocus()
    await user.keyboard(' ')
    expect(onChange).toHaveBeenCalledExactlyOnceWith('')
    expect(screen.queryByRole('dialog')).not.toBeInTheDocument()
  })
})