Skip to content

Commit

Permalink
Trigger correct events (#8963)
Browse files Browse the repository at this point in the history
* Trigger correct events

* Lint

* Prettier

* add e2e test

---------

Co-authored-by: Eduardo <[email protected]>
  • Loading branch information
fregante and fungairino authored Aug 2, 2024
1 parent 0c48253 commit 5bb2c66
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 13 deletions.
38 changes: 38 additions & 0 deletions end-to-end-tests/tests/runtime/customEvents.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2024 PixieBrix, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { test, expect } from "../../fixtures/testBase";
import { ActivateModPage } from "../../pageObjects/extensionConsole/modsPage";
// @ts-expect-error -- https://youtrack.jetbrains.com/issue/AQUA-711/Provide-a-run-configuration-for-Playwright-tests-in-specs-with-fixture-imports-only
import { test as base } from "@playwright/test";

test("custom event brick functionality", async ({ page, extensionId }) => {
const modId = "@e2e-testing/custom-event-test";

const modActivationPage = new ActivateModPage(page, extensionId, modId);
await modActivationPage.goto();

await modActivationPage.clickActivateAndWaitForModsPageRedirect();

await page.goto("/advanced-fields/");

await page.getByRole("heading", { name: "Advanced Fields" }).click();

await expect(
page.getByRole("status").filter({ hasText: "Custom Event Triggered" }),
).toBeVisible();
});
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const esmPackages = [
"p-memoize",
"p-retry",
"p-timeout",
"proper-event",
"react-base16-styling",
"react-json-tree",
"serialize-error",
Expand Down
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
"primeicons": "^6.0.1",
"primereact": "^7.1.0",
"prop-types": "^15.7.2",
"proper-event": "^0.9.1",
"psl": "^1.8.0",
"react": "^17.0.2",
"react-ace": "^12.0.0",
Expand Down
4 changes: 2 additions & 2 deletions src/bricks/effects/customEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

import { type BrickArgs, type BrickOptions } from "@/types/runtimeTypes";
import properEvent from "proper-event";
import { type JsonObject } from "type-fest";
import { type UiSchema, type Schema } from "@/types/schemaTypes";
import { EffectABC } from "@/types/bricks/effectTypes";
Expand Down Expand Up @@ -71,8 +72,7 @@ class CustomEventEffect extends EffectABC {
}: BrickArgs<{ eventName: string; data?: JsonObject }>,
{ root = document }: BrickOptions,
): Promise<void> {
const event = new CustomEvent(eventName, { detail: data, bubbles: true });
root.dispatchEvent(event);
root.dispatchEvent(properEvent(eventName, { detail: data, bubbles: true }));
}
}

Expand Down
12 changes: 2 additions & 10 deletions src/bricks/effects/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import properEvent from "proper-event";
import { EffectABC } from "@/types/bricks/effectTypes";
import { type BrickArgs, type BrickOptions } from "@/types/runtimeTypes";
import { type Schema } from "@/types/schemaTypes";
Expand Down Expand Up @@ -82,16 +83,7 @@ export class ElementEvent extends EffectABC {
// NOTE: the event is not "trusted" as being a user action
// https://developer.mozilla.org/en-US/docs/Web/API/Event/isTrusted
for (const element of $elements) {
if (event === "click") {
if (element instanceof Document) {
logger.warn("Cannot call 'click' on document");
} else {
// Trigger a proper MouseEvent on the most common event
element.click();
}
} else {
element.dispatchEvent(new Event(event, { bubbles: true }));
}
element.dispatchEvent(properEvent(event, { bubbles: true }));
}
}
}
2 changes: 1 addition & 1 deletion src/utils/domFieldUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,6 @@ export async function setFieldValue(
}

// Browsers normally fire this on `blur` if it's a text field, otherwise immediately
field.dispatchEvent(new KeyboardEvent("change", { bubbles: true }));
field.dispatchEvent(new Event("change", { bubbles: true }));
}
}

0 comments on commit 5bb2c66

Please sign in to comment.