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.

7.8 KB
Raw Blame
import type * as React from 'react'
import { userEvent } from 'vite-plus/test/browser'
import { render } from 'vitest-browser-react'
import { Field, FieldDescription, FieldError, FieldLabel } from '../../field'
import { Form } from '../../form'
import { Textarea } from '../index'

const asHTMLElement = (element: HTMLElement | SVGElement) => element as HTMLElement

describe('Textarea', () => {
  it('should show keyboard focus when read-only', async () => {
    const screen = await render(<Textarea aria-label="Notes" defaultValue="Saved notes" readOnly />)
    const textarea = screen.getByRole('textbox', { name: 'Notes' })
    const restingBoxShadow = getComputedStyle(textarea.element()).boxShadow

    await userEvent.keyboard('{Tab}')

    await expect.element(textarea).toHaveFocus()
    await expect
      .poll(() => getComputedStyle(textarea.element()).boxShadow)
      .not.toBe(restingBoxShadow)
  })

  it('should render a labelled textarea through Base UI Field.Control', async () => {
    const screen = await render(
      <Field name="description">
        <FieldLabel>Description</FieldLabel>
        <Textarea defaultValue="A workspace for support automation." />
        <FieldDescription>Shown to workspace members.</FieldDescription>
      </Field>,
    )

    const textarea = screen.getByRole('textbox', { name: 'Description' })

    await expect.element(textarea).toHaveValue('A workspace for support automation.')
    await expect.element(textarea).toHaveAccessibleDescription('Shown to workspace members.')
    expect(asHTMLElement(textarea.element()).tagName).toBe('TEXTAREA')
  })

  it('should update callback styles from field state while preserving textarea variants', async () => {
    const screen = await render(
      <Field>
        <FieldLabel>Prompt</FieldLabel>
        <Textarea
          size="large"
          // Exercise Dify's callback merging across the Field.Control textarea adapter.
          className={(state) => (state.filled ? 'resize-none' : undefined)}
          style={(state) => ({ opacity: state.filled ? 0.5 : 1 })}
        />
      </Field>,
    )
    const textarea = screen.getByRole('textbox', { name: 'Prompt' })
    const initialPadding = getComputedStyle(textarea.element()).padding

    await expect.element(textarea).toHaveStyle({ opacity: '1' })
    await textarea.fill('Draft')
    await expect.poll(() => getComputedStyle(textarea.element()).resize).toBe('none')
    await expect.element(textarea).toHaveStyle({ opacity: '0.5' })
    expect(getComputedStyle(textarea.element()).padding).toBe(initialPadding)

    await textarea.fill('')
    await expect.element(textarea).toHaveStyle({ opacity: '1' })
    await expect.poll(() => getComputedStyle(textarea.element()).resize).not.toBe('none')
  })

  it('should call onValueChange and stay controlled until value changes', async () => {
    const onValueChange = vi.fn()
    const screen = await render(
      <label>
        Notes
        <Textarea value="" onValueChange={onValueChange} />
      </label>,
    )

    const textarea = screen.getByRole('textbox', { name: 'Notes' })
    await textarea.fill('a')

    expect(onValueChange).toHaveBeenCalledWith('a', expect.any(Object))
    await expect.element(textarea).toHaveValue('')

    await screen.rerender(
      <label>
        Notes
        <Textarea value="a" onValueChange={onValueChange} />
      </label>,
    )
    await expect.element(screen.getByRole('textbox', { name: 'Notes' })).toHaveValue('a')
  })

  it('should submit valid values and show validation errors through Base UI Form', async () => {
    const onFormSubmit = vi.fn()
    const screen = await render(
      <Form aria-label="dataset form" onFormSubmit={onFormSubmit}>
        <Field name="summary">
          <FieldLabel>Summary</FieldLabel>
          <Textarea required minLength={10} />
          <FieldError match="valueMissing">Summary is required.</FieldError>
          <FieldError match="tooShort">Summary is too short.</FieldError>
        </Field>
        <button type="submit">Save</button>
      </Form>,
    )

    await screen.getByRole('button', { name: 'Save' }).click()

    await vi.waitFor(async () => {
      await expect.element(screen.getByText('Summary is required.')).toBeInTheDocument()
      await expect
        .element(screen.getByRole('textbox', { name: 'Summary' }))
        .toHaveAttribute('aria-invalid', 'true')
    })
    expect(onFormSubmit).not.toHaveBeenCalled()

    await screen.rerender(
      <Form aria-label="dataset form" onFormSubmit={onFormSubmit}>
        <Field name="summary">
          <FieldLabel>Summary</FieldLabel>
          <Textarea
            key="valid-summary"
            required
            minLength={10}
            defaultValue="Long enough summary"
          />
          <FieldError match="valueMissing">Summary is required.</FieldError>
          <FieldError match="tooShort">Summary is too short.</FieldError>
        </Field>
        <button type="submit">Save</button>
      </Form>,
    )

    await screen.getByRole('button', { name: 'Save' }).click()
    expect(onFormSubmit).toHaveBeenCalledTimes(1)
    expect(onFormSubmit.mock.calls[0]?.[0]).toMatchObject({ summary: 'Long enough summary' })
  })

  it('should pass maxLength to the textarea without rendering a counter', async () => {
    const screen = await render(
      <label>
        Release notes
        <Textarea defaultValue="Draft" maxLength={20} />
      </label>,
    )

    const textarea = screen.getByRole('textbox', { name: 'Release notes' })
    await expect.element(textarea).toHaveAttribute('maxLength', '20')
    expect(screen.container.textContent).not.toContain('5/20')
  })

  it('should route field props through Base UI Field.Control and textarea-only props to textarea', async () => {
    const onFormSubmit = vi.fn()
    const onBlur = vi.fn((event: React.FocusEvent<HTMLTextAreaElement>) => {
      expect(event.currentTarget.tagName).toBe('TEXTAREA')
    })
    const screen = await render(
      <Form aria-label="profile form" onFormSubmit={onFormSubmit}>
        <Field name="profileSummary">
          <FieldLabel>Profile summary</FieldLabel>
          <Textarea
            id="profile-summary"
            name="ignoredControlName"
            defaultValue="Long enough summary"
            rows={6}
            cols={40}
            wrap="soft"
            maxLength={80}
            onBlur={onBlur}
          />
        </Field>
        <Field disabled>
          <FieldLabel>Disabled note</FieldLabel>
          <Textarea name="disabledNote" defaultValue="Disabled value" />
        </Field>
        <button type="submit">Save</button>
      </Form>,
    )

    const profileSummary = screen.getByRole('textbox', { name: 'Profile summary' })
    expect(asHTMLElement(screen.getByText('Profile summary').element()).getAttribute('for')).toBe(
      'profile-summary',
    )
    await expect.element(profileSummary).toHaveAttribute('id', 'profile-summary')
    await expect.element(profileSummary).toHaveAttribute('name', 'profileSummary')
    await expect.element(profileSummary).toHaveAttribute('rows', '6')
    await expect.element(profileSummary).toHaveAttribute('cols', '40')
    await expect.element(profileSummary).toHaveAttribute('wrap', 'soft')
    await expect.element(profileSummary).toHaveAttribute('maxLength', '80')

    await expect.element(screen.getByRole('textbox', { name: 'Disabled note' })).toBeDisabled()

    asHTMLElement(profileSummary.element()).focus()
    const saveButton = screen.getByRole('button', { name: 'Save' })
    asHTMLElement(saveButton.element()).focus()
    expect(onBlur).toHaveBeenCalledTimes(1)

    await saveButton.click()

    expect(onFormSubmit).toHaveBeenCalledTimes(1)
    expect(onFormSubmit.mock.calls[0]?.[0]).toMatchObject({
      profileSummary: 'Long enough summary',
    })
    expect(onFormSubmit.mock.calls[0]?.[0]).not.toHaveProperty('ignoredControlName')
    expect(onFormSubmit.mock.calls[0]?.[0]).not.toHaveProperty('disabledNote')
  })
})