mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 07:53:49 +08:00
DEV: Added chat api to remove secondary actions (#21982)
In some cases, plugins may want to hide some of these actions at all times, overriding the rules for canX with hiding these buttons. To achieve this, a plugin can call the API `removeChatComposerSecondaryButtons` and pass the list of button IDs that should be removed as argument, like the example below: ``` withPluginApi("1.2.0", (api) => { api.removeChatComposerSecondaryActions("copyLink", "select"); }); ``` --------- Co-authored-by: Martin Brennan <martin@discourse.org>
This commit is contained in:
@ -1,7 +1,14 @@
|
||||
import { module, test } from "qunit";
|
||||
import { withPluginApi } from "discourse/lib/plugin-api";
|
||||
import { setupTest } from "ember-qunit";
|
||||
import { module, test } from "qunit";
|
||||
import { getOwner } from "discourse-common/lib/get-owner";
|
||||
import { withPluginApi } from "discourse/lib/plugin-api";
|
||||
import User from "discourse/models/user";
|
||||
import ChatMessageInteractor, {
|
||||
resetRemovedChatComposerSecondaryActions,
|
||||
} from "discourse/plugins/chat/discourse/lib/chat-message-interactor";
|
||||
import fabricators from "discourse/plugins/chat/discourse/lib/fabricators";
|
||||
import pretender from "discourse/tests/helpers/create-pretender";
|
||||
import { logIn } from "discourse/tests/helpers/qunit-helpers";
|
||||
|
||||
module("Chat | Unit | Utility | plugin-api", function (hooks) {
|
||||
setupTest(hooks);
|
||||
@ -20,4 +27,52 @@ module("Chat | Unit | Utility | plugin-api", function (hooks) {
|
||||
await api.sendChatMessage(1, { message: "hello", threadId: 2 });
|
||||
});
|
||||
});
|
||||
|
||||
test("#removeChatComposerSecondaryActions", async function (assert) {
|
||||
withPluginApi("1.1.0", async (api) => {
|
||||
// assert that the api method is defined
|
||||
assert.equal(typeof api.removeChatComposerSecondaryActions, "function");
|
||||
|
||||
logIn();
|
||||
const currentUser = User.current();
|
||||
getOwner(this).unregister("service:current-user");
|
||||
getOwner(this).register("service:current-user", currentUser, {
|
||||
instantiate: false,
|
||||
});
|
||||
|
||||
const message = fabricators.message({ user: currentUser });
|
||||
const context = "channel";
|
||||
const interactor = new ChatMessageInteractor(
|
||||
getOwner(this),
|
||||
message,
|
||||
context
|
||||
);
|
||||
|
||||
// assert that the initial secondary actions are present
|
||||
const secondaryActions = interactor.secondaryActions;
|
||||
assert.ok(secondaryActions.length > 0);
|
||||
|
||||
try {
|
||||
// remove the first secondary action listed
|
||||
api.removeChatComposerSecondaryActions(secondaryActions[0].id);
|
||||
|
||||
const updatedSecondaryActions = interactor.secondaryActions;
|
||||
|
||||
// assert that the secondary action was removed
|
||||
assert.ok(
|
||||
updatedSecondaryActions.length < secondaryActions.length,
|
||||
"the updated secondary actions must contain less items than the original"
|
||||
);
|
||||
assert.notOk(
|
||||
updatedSecondaryActions
|
||||
.map((v) => v.id)
|
||||
.includes(secondaryActions[0]),
|
||||
"the updated secondary actions must not include the removed action"
|
||||
);
|
||||
} finally {
|
||||
// reset the secondary actions removed to prevent leakage to other tests
|
||||
resetRemovedChatComposerSecondaryActions();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user