diff --git a/src/test/app/(protected)/completion-docs/completion-template-dialog.test.tsx b/src/test/app/(protected)/completion-docs/completion-template-dialog.test.tsx index acb7a2ac..87f0276b 100644 --- a/src/test/app/(protected)/completion-docs/completion-template-dialog.test.tsx +++ b/src/test/app/(protected)/completion-docs/completion-template-dialog.test.tsx @@ -234,6 +234,39 @@ describe("Edit template", { timeout: 15_000 }, () => { await within(dialog).findByText("Created by Jane Doe ยท Updated Sep 25, 2026"), ).toBeInTheDocument(); }); + + it.each([ + [ + "Escape", + async () => { + // The disabled Save button drops focus; Escape is pressed from inside the form. + screen.getByRole("textbox", { name: /Document Title/ }).focus(); + await user.keyboard("{Escape}"); + }, + ], + ["the Close button", () => user.click(screen.getByRole("button", { name: "Close" }))], + ])("stays closed when dismissed with %s while a save is in flight", async (_, dismiss) => { + let finishSave: () => void = () => {}; + const respond = api.apiPut.getMockImplementation()!; + api.apiPut.mockImplementation( + (...args: unknown[]) => + new Promise((resolve) => { + finishSave = () => resolve(respond(...args)); + }), + ); + const dialog = await openTemplate("Backflow Test Report"); + await user.click(within(dialog).getByRole("button", { name: /Edit/ })); + await user.type(within(dialog).getByRole("textbox", { name: /Document Title/ }), " v2"); + await user.click(within(dialog).getByRole("button", { name: /Save changes/ })); + await waitFor(() => expect(api.apiPut).toHaveBeenCalled()); + + await dismiss(); + await waitFor(() => expect(screen.queryByRole("dialog")).not.toBeInTheDocument()); + finishSave(); + + await waitFor(() => expect(toast.success).toHaveBeenCalledWith("Template updated")); + expect(screen.queryByRole("dialog")).not.toBeInTheDocument(); + }); }); describe("Delete template", { timeout: 15_000 }, () => {