import { describe, it, expect, beforeEach } from "vitest"
import { renderHook, act } from "@testing-library/react"
import { useLocalStorage } from "./use-local-storage"

describe("useLocalStorage", () => {
  beforeEach(() => {
    localStorage.clear()
  })

  it("should return initial value when localStorage is empty", () => {
    const { result } = renderHook(() => useLocalStorage("test-key", "default"))
    expect(result.current[0]).toBe("default")
  })

  it("should persist value to localStorage", () => {
    const { result } = renderHook(() => useLocalStorage("test-key", "default"))

    act(() => {
      result.current[1]("updated")
    })

    expect(result.current[0]).toBe("updated")
    expect(localStorage.getItem("test-key")).toBe(JSON.stringify("updated"))
  })

  it("should support functional updates", () => {
    const { result } = renderHook(() => useLocalStorage("test-key", 0))

    act(() => {
      result.current[1]((prev) => prev + 1)
    })

    expect(result.current[0]).toBe(1)
  })
})
