import { describe, it, expect, vi } from 'vitest'
import { render, screen, fireEvent } from '@testing-library/svelte'
import Card from './Card.svelte'

describe('Card', () => {
  it('renders card title, description, and image', () => {
    const card = {
      title: 'My Deck',
      description: 'A cool flashcard deck.',
      imageUrl: 'https://example.com/img.png',
    }
    render(Card, { props: { card } })
    expect(screen.getByText('My Deck')).toBeInTheDocument()
    expect(screen.getByText('A cool flashcard deck.')).toBeInTheDocument()
    const img = screen.getByRole('img', { name: 'My Deck' })
    expect(img).toBeInTheDocument()
    expect(img).toHaveAttribute('src', 'https://example.com/img.png')
  })

  it('calls useCard when Use button is clicked', async () => {
    const card = {
      title: 'Test Deck',
      description: 'Desc',
      imageUrl: '/img.png',
    }
    const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
    render(Card, { props: { card } })
    await fireEvent.click(screen.getByRole('button', { name: 'Use' }))
    expect(logSpy).toHaveBeenCalledWith('Test Deck')
    logSpy.mockRestore()
  })
})
