From 5098338a9649ee8830c8a0781e0eb538e8206eac Mon Sep 17 00:00:00 2001 From: David Taylor Date: Tue, 19 Mar 2024 17:32:52 +0000 Subject: [PATCH] FIX: Ensure custom composer button 'condition' is run correctly (#26245) Previously we were only running the `condition` function once, and then overwriting it with a static boolean value. Future changes to composer attributes would not affect button visibility. This commit fixes the issue and adds an acceptance test for the behavior. --- .../discourse/app/services/composer.js | 2 +- .../tests/acceptance/composer-test.js | 45 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/discourse/app/services/composer.js b/app/assets/javascripts/discourse/app/services/composer.js index d52dc30b38b..55861ebcba2 100644 --- a/app/assets/javascripts/discourse/app/services/composer.js +++ b/app/assets/javascripts/discourse/app/services/composer.js @@ -435,7 +435,7 @@ export default class ComposerService extends Service { return options.concat( customPopupMenuOptions - .map((option) => this._setupPopupMenuOption(option)) + .map((option) => this._setupPopupMenuOption({ ...option })) .filter((o) => o) ); } diff --git a/app/assets/javascripts/discourse/tests/acceptance/composer-test.js b/app/assets/javascripts/discourse/tests/acceptance/composer-test.js index 19e185f3015..3f81b4b619c 100644 --- a/app/assets/javascripts/discourse/tests/acceptance/composer-test.js +++ b/app/assets/javascripts/discourse/tests/acceptance/composer-test.js @@ -1394,3 +1394,48 @@ acceptance("Composer - current time", function (needs) { ); }); }); + +acceptance("composer buttons API", function (needs) { + needs.user(); + needs.settings({ + allow_uncategorized_topics: true, + }); + + test("buttons can be added conditionally", async function (assert) { + withPluginApi("0", (api) => { + api.addComposerToolbarPopupMenuOption({ + action: (toolbarEvent) => { + toolbarEvent.applySurround("**", "**"); + }, + icon: "far-bold", + label: "some_label", + condition: (composer) => { + return composer.model.creatingTopic; + }, + }); + }); + + await visit("/t/internationalization-localization/280"); + + await click(".post-controls button.reply"); + assert.dom(".d-editor-input").exists("the composer input is visible"); + + const expectedName = "[en.some_label]"; + const dropdown = selectKit(".toolbar-popup-menu-options"); + await dropdown.expand(); + + assert.false( + dropdown.rowByName(expectedName).exists(), + "custom button is not displayed for reply" + ); + + await visit("/latest"); + await click("#create-topic"); + + await dropdown.expand(); + assert.true( + dropdown.rowByName(expectedName).exists(), + "custom button is displayed for new topic" + ); + }); +});