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.3 KB
Raw Blame
import type * as React from 'react'
import { userEvent } from 'vite-plus/test/browser'
import { render } from 'vitest-browser-react'
import { Button } from '../index'

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

describe('Button', () => {
  describe('rendering', () => {
    it('defaults to type="button"', async () => {
      const screen = await render(<Button>Click me</Button>)
      await expect.element(screen.getByRole('button')).toHaveAttribute('type', 'button')
    })

    it('allows type override to submit', async () => {
      const screen = await render(<Button type="submit">Submit</Button>)
      await expect.element(screen.getByRole('button')).toHaveAttribute('type', 'submit')
    })

    it('renders a non-native button via render prop', async () => {
      const screen = await render(
        <Button nativeButton={false} render={<div />}>
          Custom button
        </Button>,
      )
      const button = screen.getByRole('button', { name: 'Custom button' }).element()
      expect(button.tagName).toBe('DIV')
    })
  })

  describe('disabled states', () => {
    it('uses native disabled semantics for regular disabled buttons', async () => {
      const screen = await render(<Button disabled>Click me</Button>)
      const button = screen.getByRole('button').element()

      expect(button).toBeDisabled()
      expect(button).not.toHaveAttribute('aria-disabled')
    })

    it('allows loading focusability to be opted out', async () => {
      const screen = await render(
        <Button loading focusableWhenDisabled={false}>
          Loading
        </Button>,
      )
      await expect.element(screen.getByRole('button')).toBeDisabled()
    })
  })

  describe('events', () => {
    it('does not implicitly submit a form through a loading submit button', async () => {
      const onSubmit = vi.fn((event: React.FormEvent<HTMLFormElement>) => event.preventDefault())
      const screen = await render(
        <form onSubmit={onSubmit}>
          <label htmlFor="name">Name</label>
          <input id="name" />
          <Button type="submit" loading>
            Submit
          </Button>
        </form>,
      )

      asHTMLElement(screen.getByRole('textbox', { name: 'Name' }).element()).focus()
      await userEvent.keyboard('{Enter}')

      expect(onSubmit).not.toHaveBeenCalled()
    })
  })
})