Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix(automations): keep the filters popover open between choices (#18026)

Engel Nyst0ab2137ad529
diff --git a/__tests__/components/features/chat/components/chat-input-actions.test.tsx b/__tests__/components/features/chat/components/chat-input-actions.test.tsx
index 9ec998b159..6e4ebeaac2 100644
--- a/__tests__/components/features/chat/components/chat-input-actions.test.tsx
+++ b/__tests__/components/features/chat/components/chat-input-actions.test.tsx
@@ -402,7 +402,8 @@ describe("ChatInputActions β€” More input actions overflow menu (#17925)", () =>
// browser drains React's microtask flush between listeners: the outside-click
// listener sees the opening click only after the menu has mounted. RTL roots
// React below `document` and dispatches synchronously, so capture the
- // document click listeners and deliver the trigger click to them afterwards.
+ // document click listeners and deliver the trigger click to them afterwards,
+ // as they would see it mid-dispatch: with its target and its composedPath().
let documentClickListeners: EventListener[] = [];
beforeEach(() => {
@@ -445,10 +446,21 @@ describe("ChatInputActions β€” More input actions overflow menu (#17925)", () =>
const clickTrigger = () => {
const icon = getTrigger().querySelector("svg") ?? getTrigger();
+ // An event has a path only while it is being dispatched, and a click that
+ // is never dispatched has none, so record the path of the real click.
+ let path: EventTarget[] = [];
+ window.addEventListener(
+ "click",
+ (event) => {
+ path = event.composedPath();
+ },
+ { capture: true, once: true },
+ );
fireEvent.click(icon);
act(() => {
const click = new MouseEvent("click", { bubbles: true });
Object.defineProperty(click, "target", { value: icon });
+ Object.defineProperty(click, "composedPath", { value: () => path });
documentClickListeners.forEach((listener) => listener(click));
});
};
diff --git a/__tests__/hooks/use-click-outside-element.test.tsx b/__tests__/hooks/use-click-outside-element.test.tsx
index b60077bda1..8983c30e7b 100644
--- a/__tests__/hooks/use-click-outside-element.test.tsx
+++ b/__tests__/hooks/use-click-outside-element.test.tsx
@@ -1,4 +1,5 @@
import React from "react";
+import { flushSync } from "react-dom";
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { expect, test, vi } from "vitest";
@@ -68,3 +69,34 @@ test("does not call the callback when clicking the ignored trigger", async () =>
await user.click(screen.getByTestId("outside-element"));
expect(callback).toHaveBeenCalledTimes(1);
});
+
+test("does not call the callback when the clicked inside element unmounts during the click", async () => {
+ // A menu option that closes its own menu is removed from the DOM by the
+ // time the document listener runs; it was still inside when clicked.
+ const user = userEvent.setup();
+ const callback = vi.fn();
+
+ function Harness() {
+ const ref = useClickOutsideElement<HTMLDivElement>(callback);
+ const [optionVisible, setOptionVisible] = React.useState(true);
+
+ return (
+ <div ref={ref}>
+ {optionVisible ? (
+ <button
+ type="button"
+ data-testid="self-closing-option"
+ onClick={() => flushSync(() => setOptionVisible(false))}
+ />
+ ) : null}
+ </div>
+ );
+ }
+
+ render(<Harness />);
+
+ await user.click(screen.getByTestId("self-closing-option"));
+
+ expect(screen.queryByTestId("self-closing-option")).not.toBeInTheDocument();
+ expect(callback).not.toHaveBeenCalled();
+});
diff --git a/src/hooks/use-click-outside-element.ts b/src/hooks/use-click-outside-element.ts
index 5bb69be83f..3ecba16ec5 100644
--- a/src/hooks/use-click-outside-element.ts
+++ b/src/hooks/use-click-outside-element.ts
@@ -18,10 +18,15 @@ export const useClickOutsideElement = <T extends HTMLElement>(
React.useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
- const target = event.target as Node;
if (!ref.current) return;
- if (ref.current.contains(target)) return;
- if (ignoreOutsideClickRef?.current?.contains(target)) return;
+ // Check the path recorded when the click was dispatched rather than
+ // `contains(event.target)`: an inside element that unmounts in its own
+ // click handler (a menu option closing its menu) is already detached
+ // when this document listener runs, but was clicked inside.
+ const path = event.composedPath();
+ if (path.includes(ref.current)) return;
+ const ignoredElement = ignoreOutsideClickRef?.current;
+ if (ignoredElement && path.includes(ignoredElement)) return;
callbackRef.current();
};
diff --git a/tests/e2e/automations-filters-popover.spec.ts b/tests/e2e/automations-filters-popover.spec.ts
new file mode 100644
index 0000000000..2bd7414d65
--- /dev/null
+++ b/tests/e2e/automations-filters-popover.spec.ts
@@ -0,0 +1,71 @@
+import { expect, test } from "@playwright/test";
+
+// Regression for #17902: in a real browser React removes a clicked menu
+// option before the document click listener runs, so the Filters popover
+// used to treat every option click as an outside click and close.
+test("keeps the automations Filters popover open while combining filters", async ({
+ page,
+}) => {
+ await page.addInitScript(() => {
+ window.localStorage.setItem("analytics-consent", "false");
+ window.localStorage.setItem("openhands-telemetry-consent", "denied");
+ window.localStorage.setItem("openhands-telemetry-first-use", "true");
+ window.localStorage.setItem("openhands-onboarded", "1");
+ window.localStorage.setItem(
+ "openhands-backends",
+ JSON.stringify([
+ {
+ id: "default-local",
+ name: "Local",
+ host: window.location.origin,
+ apiKey: "",
+ kind: "local",
+ },
+ ]),
+ );
+ window.localStorage.setItem(
+ "openhands-active-backend",
+ JSON.stringify({ backendId: "default-local", orgId: null }),
+ );
+ });
+ await page.goto("/automations");
+ // Mock settings have no analytics choice yet, so the local consent prompt
+ // covers the page until it is answered.
+ await page.getByTestId("confirm-telemetry-preferences").click();
+ await expect(page.getByTestId("telemetry-consent-form")).toHaveCount(0);
+
+ const filtersTrigger = page
+ .getByTestId("automations-filters")
+ .getByTestId("dropdown-trigger")
+ .first();
+ const filtersMenu = page.getByTestId("automations-filters-menu");
+
+ await filtersTrigger.click();
+ await page
+ .getByTestId("automations-filter-status")
+ .getByTestId("dropdown-trigger")
+ .click();
+ await page.getByTestId("automations-filter-status-disabled").click();
+
+ await expect(filtersMenu).toBeVisible();
+ await expect(filtersTrigger).toHaveAttribute("aria-expanded", "true");
+
+ await page
+ .getByTestId("automations-filter-trigger")
+ .getByTestId("dropdown-trigger")
+ .click();
+ await page.getByTestId("automations-filter-trigger-schedule").click();
+
+ await expect(filtersMenu).toBeVisible();
+ await expect(filtersTrigger).toContainText("2");
+
+ await page.getByTestId("automations-filters-reset").click();
+
+ await expect(filtersMenu).toBeVisible();
+ await expect(page.getByTestId("automations-filters-reset")).toHaveCount(0);
+
+ await page.getByRole("heading", { name: "Dashboard" }).click();
+
+ await expect(filtersMenu).toHaveCount(0);
+ await expect(filtersTrigger).toHaveAttribute("aria-expanded", "false");
+});