diff --git a/app/assets/javascripts/discourse/app/services/header.js b/app/assets/javascripts/discourse/app/services/header.js index 35689aa0887..42276d8b0ec 100644 --- a/app/assets/javascripts/discourse/app/services/header.js +++ b/app/assets/javascripts/discourse/app/services/header.js @@ -4,6 +4,8 @@ import Service, { service } from "@ember/service"; import { TrackedMap } from "@ember-compat/tracked-built-ins"; import { disableImplicitInjections } from "discourse/lib/implicit-injections"; +const VALID_HEADER_BUTTONS_TO_HIDE = ["search", "login", "signup"]; + @disableImplicitInjections export default class Header extends Service { @service siteSettings; @@ -35,7 +37,26 @@ export default class Header extends Service { } registerHider(ref, buttons) { - this.#hiders.set(ref, buttons); + const validButtons = buttons + .map((button) => { + if (!VALID_HEADER_BUTTONS_TO_HIDE.includes(button)) { + // eslint-disable-next-line no-console + console.error( + `Invalid button to hide: ${button}, valid buttons are: ${VALID_HEADER_BUTTONS_TO_HIDE.join( + "," + )}` + ); + } else { + return button; + } + }) + .filter(Boolean); + + if (!validButtons.length) { + return; + } + + this.#hiders.set(ref, validButtons); registerDestructor(ref, () => { this.#hiders.delete(ref); diff --git a/app/assets/javascripts/discourse/tests/unit/services/header-test.js b/app/assets/javascripts/discourse/tests/unit/services/header-test.js new file mode 100644 index 00000000000..30ea6cceed1 --- /dev/null +++ b/app/assets/javascripts/discourse/tests/unit/services/header-test.js @@ -0,0 +1,22 @@ +import { getOwner } from "@ember/application"; +import { setupTest } from "ember-qunit"; +import { module, test } from "qunit"; + +module("Unit | Service | header", function (hooks) { + setupTest(hooks); + + hooks.beforeEach(function () { + this.header = getOwner(this).lookup("service:header"); + }); + + test("it registers hiders", function (assert) { + this.header.registerHider(this, ["search", "login"]); + assert.ok(this.header.headerButtonsHidden.includes("search")); + assert.ok(this.header.headerButtonsHidden.includes("login")); + }); + + test("it does not register invalid buttons for hiders", function (assert) { + this.header.registerHider(this, ["search", "blahblah"]); + assert.notOk(this.header.headerButtonsHidden.includes("blah")); + }); +});