import { expect, test } from "vitest";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { renderHook, waitFor } from "@testing-library/react";
import { http, HttpResponse } from "msw";
import { server } from "../test/server";
import { useCreateFieldDefinition } from "./queries";

function wrapper({ children }: { children: React.ReactNode }) {
  const qc = new QueryClient({ defaultOptions: { queries: { retry: false }, mutations: { retry: false } } });

  return <QueryClientProvider client={qc}>{children}</QueryClientProvider>;
}

test("useCreateFieldDefinition POSTs the request body", async () => {
  let body: unknown;

  server.use(
    http.post("/api/admin/field-definitions", async ({ request }) => {
      body = await request.json();

      return HttpResponse.json({ key: "technique" }, { status: 201 });
    }),
  );

  const { result } = renderHook(() => useCreateFieldDefinition(), { wrapper });

  result.current.mutate({
    key: "technique",
    data_type: "term",
    vocabulary_id: "v-technique",
    authority_kind: null,
    required: false,
    group: "Provenance",
    labels: [{ lang: "en", label: "Technique" }],
  });

  await waitFor(() => expect(result.current.isSuccess).toBe(true));
  expect((body as { key: string }).key).toBe("technique");
  expect((body as { data_type: string }).data_type).toBe("term");
});
