import { renderHook } from "@testing-library/react";
import type { ReactNode } from "react";
import assert from "node:assert/strict";
import { describe, it } from "node:test";
import { useSSGRender } from "./useSSGRender";
import { constants } from "../../constants";

describe("useSSGRender", () => {
  it("updates serialized config when props change in SSG mode", () => {
    const wrapper = ({ children }: { children: ReactNode; }) => (
      <constants.globalCtx value={{ isSSR: false, mode: "SSG" }}>
        {children}
      </constants.globalCtx>
    );

    const { result, rerender } = renderHook((props: { value: string; }) => {
      return useSSGRender("data-js-test", {
        value: props.value,
      });
    }, {
      initialProps: { value: "one" },
      wrapper,
    });

    const firstJson = result.current?.props?.["data-js-test"] as string;
    assert.notStrictEqual(firstJson, undefined);
    assert.strictEqual(JSON.parse(firstJson).value, "one");

    rerender({ value: "two" });

    const secondJson = result.current?.props?.["data-js-test"] as string;
    assert.notStrictEqual(secondJson, undefined);
    assert.strictEqual(JSON.parse(secondJson).value, "two");
  });
});
