// @vitest-environment jsdom
import { renderHook } from "@testing-library/react";
import type { ReactNode } from "react";
import { AnalyticsContext, useAnalytics } from "./provider.js";

describe("useAnalytics", () => {
  it("returns NoopAnalytics when no provider is present", () => {
    const { result } = renderHook(() => useAnalytics());
    // Should return the noop — calling track/page should not throw
    expect(result.current).toBeDefined();
    expect(() => result.current.track("test")).not.toThrow();
    expect(() => result.current.page("/")).not.toThrow();
  });

  it("returns the provided Analytics instance", () => {
    const mockAnalytics = { track: vi.fn(), page: vi.fn() };

    function wrapper({ children }: { children: ReactNode }) {
      return (
        <AnalyticsContext.Provider value={mockAnalytics}>
          {children}
        </AnalyticsContext.Provider>
      );
    }

    const { result } = renderHook(() => useAnalytics(), { wrapper });
    expect(result.current).toBe(mockAnalytics);
  });
});
