fix: isolate preset requests and confirm parameter saves
This commit is contained in:
1 parent
7a3eac4088
commit
1066adb10b
24 files changed
+322
-28
No files matched your search
@@ -0,0 +1,53 @@
|
||||
import { act, cleanup, fireEvent, render, screen, within } from "@testing-library/react";
|
||||
import { afterEach, beforeEach, expect, it, vi } from "vitest";
|
||||
import { ProviderParameterFields } from "@/components/settings/models/ProviderParameterFields";
|
||||
import i18n from "@/i18n";
|
||||
|
||||
beforeEach(async () => { await i18n.changeLanguage("en"); });
|
||||
afterEach(cleanup);
|
||||
|
||||
const form = { extraHeaders: "", extraQuery: "", extraBody: "{}" };
|
||||
|
||||
it("distinguishes a new preset draft from persisted settings and validates before applying", async () => {
|
||||
const change = vi.fn();
|
||||
render(<ProviderParameterFields inherit draft fields={["extra_headers"]} form={form} onChange={change} />);
|
||||
fireEvent.click(screen.getByRole("button", { name: "Headers Use provider settings Edit" }));
|
||||
const dialog = screen.getByRole("dialog", { name: "Headers" });
|
||||
expect(dialog).toHaveAccessibleDescription("Save the new preset to apply these settings.");
|
||||
const editor = within(dialog).getByRole("textbox");
|
||||
for (const value of ["broken", "[]", '{"X-Route":123}']) {
|
||||
fireEvent.change(editor, { target: { value } });
|
||||
fireEvent.click(within(dialog).getByRole("button", { name: "Use in preset" }));
|
||||
expect(await within(dialog).findByRole("alert")).toBeVisible();
|
||||
expect(change).not.toHaveBeenCalled();
|
||||
expect(editor).toHaveFocus();
|
||||
}
|
||||
fireEvent.change(editor, { target: { value: '{"X-Route":"demo"}' } });
|
||||
await act(async () => { fireEvent.click(within(dialog).getByRole("button", { name: "Use in preset" })); });
|
||||
expect(change).toHaveBeenCalledTimes(1);
|
||||
expect(change).toHaveBeenCalledWith({ extraHeaders: '{"X-Route":"demo"}' });
|
||||
});
|
||||
|
||||
it("waits for persistence, keeps errors and drafts in place, and lets users cancel a failed save", async () => {
|
||||
let rejectSave!: (reason: Error) => void;
|
||||
const save = vi.fn(() => new Promise<void>((_resolve, reject) => { rejectSave = reject; }));
|
||||
const change = vi.fn();
|
||||
render(<ProviderParameterFields inherit fields={["extra_body"]} form={form} onChange={change} onSave={save} />);
|
||||
fireEvent.click(screen.getByRole("button", { name: "Body parameters Use provider settings Edit" }));
|
||||
const dialog = screen.getByRole("dialog", { name: "Body parameters" });
|
||||
const editor = within(dialog).getByRole("textbox");
|
||||
fireEvent.change(editor, { target: { value: '{"watermark":false}' } });
|
||||
fireEvent.click(within(dialog).getByRole("button", { name: "Save", exact: true }));
|
||||
expect(within(dialog).getByRole("button", { name: i18n.t("settings.actions.saving") })).toBeDisabled();
|
||||
expect(within(dialog).getByRole("button", { name: "Cancel" })).toBeDisabled();
|
||||
fireEvent.keyDown(editor, { key: "Escape" });
|
||||
expect(dialog).toBeVisible();
|
||||
await act(async () => rejectSave(new Error("Connection lost. Try again.")));
|
||||
expect(within(dialog).getByRole("alert")).toHaveTextContent("Connection lost. Try again.");
|
||||
expect(editor).toHaveValue('{"watermark":false}');
|
||||
expect(editor).toHaveFocus();
|
||||
fireEvent.click(within(dialog).getByRole("button", { name: "Cancel" }));
|
||||
expect(change).not.toHaveBeenCalled();
|
||||
fireEvent.click(screen.getByRole("button", { name: "Body parameters Use provider settings Edit" }));
|
||||
expect(screen.getByRole("textbox")).toHaveValue("{}");
|
||||
});
|
||||
Reference in new issue
Block a user