import { describe, test, expect, vi } from 'vitest'
import { render, screen, fireEvent } from '@testing-library/react'
import { CopyButton } from './copy-button'

vi.mock('@carto/ps-utils', () => ({
  copy: vi.fn(() => Promise.resolve()),
}))

describe('CopyButton', () => {
  test('renders copy button', () => {
    render(
      <CopyButton copyText='hello' onSuccess={vi.fn()} onError={vi.fn()} />,
    )
    expect(screen.getByRole('button')).toBeTruthy()
  })

  test('renders with custom aria-label', () => {
    render(
      <CopyButton
        copyText='hello'
        onSuccess={vi.fn()}
        onError={vi.fn()}
        aria-label='Copy response'
      />,
    )
    expect(screen.getByLabelText('Copy response')).toBeTruthy()
  })

  test('calls copy utility when clicked', async () => {
    const { copy } = await import('@carto/ps-utils')
    render(
      <CopyButton
        copyText='hello world'
        onSuccess={vi.fn()}
        onError={vi.fn()}
      />,
    )
    fireEvent.click(screen.getByRole('button'))
    expect(copy).toHaveBeenCalledWith('hello world')
  })
})
