From 2b338b36c605c28bc190bfd0e1d3d6067f25b755 Mon Sep 17 00:00:00 2001 From: Alexandre Brandizzi Date: Fri, 25 Sep 2026 17:13:20 -0300 Subject: [PATCH] test(completion-docs): keep the dialog closed when dismissed mid-save Dismissing the edit dialog with Escape or the Close button while an update is still in flight must not bring it back when the save lands. The per-call mutate callbacks that reopen it in view mode are dropped by TanStack Query once the dialog unmounts, so this pins that behaviour: the test fails if the save is moved to mutateAsync().then(), which would reopen the dialog. --- .../completion-template-dialog.test.tsx | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) 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 }, () => {