This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/tests/acceptance/plugin-keyboard-shortcut-test.js
Jarek Radosz f0b2e77abb
FIX: Isolate modal and global key-binds (#12477)
This change makes is so that when a time-picking modal (e.g.  "Add bookmark" modal) is visible, **all** global key bindings are paused.

1. Fixes an issue where opening and closing a time-picking modal would break global single-key keybinds, so for example, <kbd>L</kbd> would no longer like posts, but <kbd>L</kbd> <kbd>L</kbd> would
2. Fixes a related issue, where doing the above would also override custom keybinds provided by plugins (e.g. <kbd>L</kbd> shortcut that discourse-reactions uses)

Included:

* DEV: Reset Mousetraps instead of unbinding
* FIX: Make unbind use unbind
* DEV: Don't check for keyTrapper twice
* DEV: Use an instance of Mousetrap
* DEV: Remove an invalid `for` attribute (`set_reminder` doesn't exist)
* DEV: Add ability to pause all KeyboardShortcuts
* FIX: Pause all keybinds when in a time-picking modal
* DEV: Move bookmark keybind resets to willDestroyElement
* DEV: Fix shortcuts-related tests
2021-03-29 13:58:03 +02:00

52 lines
1.6 KiB
JavaScript

import { triggerKeyEvent, visit } from "@ember/test-helpers";
import KeyboardShortcuts from "discourse/lib/keyboard-shortcuts";
import { acceptance } from "discourse/tests/helpers/qunit-helpers";
import sinon from "sinon";
import { test } from "qunit";
import { withPluginApi } from "discourse/lib/plugin-api";
acceptance("Plugin Keyboard Shortcuts - Logged In", function (needs) {
needs.user();
test("a plugin can add a keyboard shortcut", async function (assert) {
// Initialize the app (required in the legacy testing env)
await visit("/");
withPluginApi("0.8.38", (api) => {
api.addKeyboardShortcut("]", () => {
$("#qunit-fixture").html(
"<div id='added-element'>Test adding plugin shortcut</div>"
);
});
});
await visit("/t/this-is-a-test-topic/9");
await triggerKeyEvent(document, "keypress", "]".charCodeAt(0));
assert.equal(
$("#added-element").length,
1,
"the keyboard shortcut callback fires successfully"
);
});
});
acceptance("Plugin Keyboard Shortcuts - Anonymous", function () {
test("a plugin can add a keyboard shortcut with an option", async function (assert) {
// Initialize the app (required in the legacy testing env)
await visit("/");
let spy = sinon.spy(KeyboardShortcuts, "_bindToPath");
withPluginApi("0.8.38", (api) => {
api.addKeyboardShortcut("]", () => {}, {
anonymous: true,
path: "test-path",
});
});
assert.ok(
spy.calledWith("test-path", "]"),
"bindToPath is called due to options provided"
);
});
});