mirror of
https://github.com/discourse/discourse.git
synced 2025-06-06 03:06:53 +08:00
DEV: Make sure header hidden buttons are valid (#27818)
Followup 0434112aa71fc091583d8356a84fbf958f7228fb, we introduced HideApplicationHeaderButtons there but didn't validate the buttons passed to it. With this commit we do, and send an error to the browser console if an invalid one is used.
This commit is contained in:
@ -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);
|
||||
|
@ -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"));
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user