import { describe, it, expect, vi } from "vitest"
import { renderHook, act } from "@testing-library/react"
import { useDebounce } from "./use-debounce"

describe("useDebounce", () => {
  it("should return initial value immediately", () => {
    const { result } = renderHook(() => useDebounce("hello", 500))
    expect(result.current).toBe("hello")
  })

  it("should debounce value changes", () => {
    vi.useFakeTimers()
    const { result, rerender } = renderHook(
      ({ value, delay }) => useDebounce(value, delay),
      { initialProps: { value: "hello", delay: 300 } }
    )

    rerender({ value: "world", delay: 300 })
    expect(result.current).toBe("hello")

    act(() => {
      vi.advanceTimersByTime(300)
    })
    expect(result.current).toBe("world")

    vi.useRealTimers()
  })
})
