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.5 KB
Raw Blame
import type { ExtraProps } from 'streamdown'
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import * as React from 'react'
import { beforeEach, describe, expect, it, vi } from 'vite-plus/test'
import { ChatContextProvider } from '@/app/components/base/chat/chat/context-provider'
import MarkdownButton from '../button'

type TestNode = NonNullable<ExtraProps['node']>

function createButtonNode(properties: TestNode['properties'], label?: string): TestNode {
  return {
    type: 'element',
    tagName: 'button',
    properties,
    children: label === undefined ? [] : [{ type: 'text', value: label }],
  }
}

describe('MarkdownButton (integration)', () => {
  const onSendSpy = vi.fn()

  beforeEach(() => {
    vi.clearAllMocks()
  })

  function renderWithCtx(node: TestNode) {
    return render(
      <ChatContextProvider chatList={[]} onSend={onSendSpy}>
        <MarkdownButton node={node} />
      </ChatContextProvider>,
    )
  }

  it('renders a native link when the URL is valid', () => {
    const node = createButtonNode(
      { dataLink: 'https://example.com', dataMessage: 'Must not send' },
      'Go',
    )

    renderWithCtx(node)
    const link = screen.getByRole('link', { name: 'Go' })

    expect(link).toHaveAttribute('href', 'https://example.com')
    expect(link).toHaveAttribute('target', '_blank')
    expect(link).toHaveAttribute('rel', 'noopener noreferrer')
    expect(onSendSpy).not.toHaveBeenCalled()
  })

  it('calls onSend when link is invalid but message exists', async () => {
    const user = userEvent.setup()

    const node = createButtonNode({ dataLink: 'not-a-url', dataMessage: 'hello!' }, 'Send')

    renderWithCtx(node)
    await user.click(screen.getByRole('button', { name: 'Send' }))

    expect(onSendSpy).toHaveBeenCalledTimes(1)
    expect(onSendSpy).toHaveBeenCalledWith('hello!')
  })

  it('does nothing when no link and no message', async () => {
    const user = userEvent.setup()

    const node = createButtonNode({}, 'Empty')
    renderWithCtx(node)
    await user.click(screen.getByRole('button', { name: 'Empty' }))

    expect(onSendSpy).not.toHaveBeenCalled()
  })

  it('calls onSend when message present and no link', async () => {
    const user = userEvent.setup()
    const node = createButtonNode({ dataMessage: 'msg-only' }, 'Msg')

    renderWithCtx(node)
    await user.click(screen.getByRole('button', { name: 'Msg' }))

    expect(onSendSpy).toHaveBeenCalledWith('msg-only')
  })
})