import { render, screen } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { SchemaLinkingGateWidget } from "./SchemaLinkingGateWidget"; import type { WidgetDescriptor } from "../api/types"; const descriptor: WidgetDescriptor = { id: "u7", widget: "schema-linking", title: "F4 — Schema linking", tables: [ { id: "t-pat", name: "dim_patient", kind: "promote", recommended: true, description: "Anagrafica", rationale: "Output paziente", columns: [ { name: "cod_paz", description: "Codice", suggested: true }, { name: "nome", description: "Nome", suggested: false }, ], }, { id: "x-sub", name: "fact_sostituzione", kind: "exclude", recommended: true, description: "Sostituzione", rationale: "Non impianto iniziale", columns: [{ name: "cod_paz" }], }, ], reserved: ["back", "exit", "other"], }; test("starts include rows checked and exclude rows unchecked even for legacy payloads", () => { render(); expect(screen.getByRole("checkbox", { name: "dim_patient" })).toBeChecked(); expect(screen.getByRole("checkbox", { name: "fact_sostituzione" })).not.toBeChecked(); }); test("Confirm emits enacted tables with suggested columns (catalog order)", async () => { const onRespond = vi.fn(); render(); await userEvent.click(screen.getByRole("button", { name: /confirm/i })); expect(onRespond).toHaveBeenCalledWith({ id: "u7", kind: "schema-linking", tables: [ { id: "t-pat", enacted: true, columns: ["cod_paz"] }, { id: "x-sub", enacted: false }, ], }); }); test("selecting a column in the modal adds it to the response", async () => { const onRespond = vi.fn(); render(); // dim_patient starts with 1 suggested of 2 -> its button reads "Columns 1/2". await userEvent.click(screen.getByRole("button", { name: /columns 1\/2/i })); await userEvent.click(screen.getByRole("checkbox", { name: "nome" })); await userEvent.click(screen.getByRole("button", { name: /close/i })); // dialog X button await userEvent.click(screen.getByRole("button", { name: /confirm/i })); expect(onRespond).toHaveBeenCalledWith({ id: "u7", kind: "schema-linking", tables: [ { id: "t-pat", enacted: true, columns: ["cod_paz", "nome"] }, { id: "x-sub", enacted: false }, ], }); }); test("declining a table's enact checkbox drops its columns", async () => { const onRespond = vi.fn(); render(); await userEvent.click(screen.getByRole("checkbox", { name: /dim_patient/i })); await userEvent.click(screen.getByRole("button", { name: /confirm/i })); expect(onRespond).toHaveBeenCalledWith({ id: "u7", kind: "schema-linking", tables: [ { id: "t-pat", enacted: false }, { id: "x-sub", enacted: false }, ], }); }); test("reserved control emits control", async () => { const onRespond = vi.fn(); render(); await userEvent.click(screen.getByRole("button", { name: /go back/i })); expect(onRespond).toHaveBeenCalledWith({ id: "u7", control: "back" }); });