mirror of
https://github.com/discourse/discourse.git
synced 2024-11-25 21:33:43 +08:00
DEV: Make sure header hidden buttons are valid (#27818)
Followup 0434112aa7
,
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:
parent
e364ed2ad1
commit
d4c603984f
|
@ -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"));
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user