import { describe, it, expect } from 'vitest'
import { render, screen, fireEvent, waitFor } from '@testing-library/react'
import { WysiwygBuilderPage } from '@/components/wysiwyg/WysiwygBuilderPage'

// minimal mock for URL.createObjectURL used by download
const createObjectURL = global.URL.createObjectURL
const revokeObjectURL = global.URL.revokeObjectURL

describe('WysiwygBuilderPage', () => {
  beforeEach(() => {
    // @ts-ignore
    global.URL.createObjectURL = () => 'blob:mock'
    // @ts-ignore
    global.URL.revokeObjectURL = () => {}
  })
  afterEach(() => {
    global.URL.createObjectURL = createObjectURL
    global.URL.revokeObjectURL = revokeObjectURL
  })

  it('adds a text object to the canvas', async () => {
    render(<WysiwygBuilderPage />)
    const addText = screen.getByRole('button', { name: /Add Text/i })
    fireEvent.click(addText)
    await waitFor(() => {
      expect(screen.getByTestId('obj-text-1')).toBeInTheDocument()
    })
  })

  it('has an Export button', () => {
    render(<WysiwygBuilderPage />)
    expect(screen.getByRole('button', { name: /Export/i })).toBeInTheDocument()
  })
})
