import { describe, it, expect, vi, beforeEach } from 'vitest'
import { mount } from '@vue/test-utils'
import { setActivePinia, createPinia } from 'pinia'
import BaseToast from '@/components/BaseToast.vue'
import { useToastStore } from '@/stores/toast'

describe('BaseToast', () => {
  beforeEach(() => {
    setActivePinia(createPinia())
  })

  it('renders nothing when there are no toasts', () => {
    const wrapper = mount(BaseToast)
    expect(wrapper.findAll('.toast')).toHaveLength(0)
  })

  it('renders an info toast pushed via the store', async () => {
    const wrapper = mount(BaseToast)
    const toast = useToastStore()
    toast.show('hello', 'info')
    await wrapper.vm.$nextTick()
    const items = wrapper.findAll('.toast')
    expect(items).toHaveLength(1)
    expect(items[0].text()).toContain('hello')
    expect(items[0].classes()).toContain('toast--info')
  })

  it('renders multiple toasts and dismisses on close button click', async () => {
    vi.useFakeTimers()
    const wrapper = mount(BaseToast)
    const toast = useToastStore()
    toast.show('first', 'success')
    toast.show('second', 'error')
    await wrapper.vm.$nextTick()
    expect(wrapper.findAll('.toast')).toHaveLength(2)

    await wrapper.findAll('.toast__close')[0].trigger('click')
    expect(toast.toasts).toHaveLength(1)
    expect(toast.toasts[0].message).toBe('second')

    vi.runAllTimers()
    vi.useRealTimers()
  })
})
