diff --git a/src/core/js/webcaosdb.js b/src/core/js/webcaosdb.js index b85a007b7b06859d00dc220abf4fb3a9abe8b20a..b622a1c879f3cf8aa53378e32f0a15f666ea7689 100644 --- a/src/core/js/webcaosdb.js +++ b/src/core/js/webcaosdb.js @@ -309,14 +309,14 @@ this.navbar = new function () { /** * Hide the given element for certain roles * - * @param {string|object} name - The HTML element either as the result - * of a jquery or identified by a jquery selector - * @param {array} roles - list of roles for which the given element is + * @param {string|object} element - The HTML element to hide. May be either + * a string to be used in a jquery, or the result of a jquery. + * @param {array} roles - List of roles for which the given element is * hidden. */ - this.hideElementForRoles = function (name, roles) { - var elt = name; - if (typeof (name) === "string") { + this.hideElementForRoles = function (element, roles) { + var elt = element; + if (typeof (element) === "string") { elt = $(elt); } const userRoles = getUserRoles(); diff --git a/test/core/js/modules/webcaosdb.js.js b/test/core/js/modules/webcaosdb.js.js index deb2b1884f1ac56e75ee492748a052cb20355811..b43af58d4fef57aafd7d5d46998fc1a13725e868 100644 --- a/test/core/js/modules/webcaosdb.js.js +++ b/test/core/js/modules/webcaosdb.js.js @@ -1887,22 +1887,32 @@ QUnit.test("toolbox example", function (assert) { }); QUnit.test("hide elements for roles", function (assert) { - navbar.add_button("TestButton"); + navbar.add_button("TestButton1"); + navbar.add_button("TestButton2"); // mock user role $("#top-navbar").append(`<div class="caosdb-user-role">someRole</div>`); - navbar.hideElementForRoles($("#top-navbar").find("button"), ["someOtherRole"]); - // don't have the role, so nothing is hidden: - assert.notOk($("#top-navbar").find("button").hasClass("d-none")); + navbar.hideElementForRoles($("button:contains('TestButton1')"), ["someOtherRole"]); + navbar.hideElementForRoles("button:contains('TestButton2')", ["someOtherRole"]); + // I don't have the role, so nothing is hidden: + assert.notOk($("button:contains('TestButton1')").hasClass("d-none")); + assert.notOk($("button:contains('TestButton2')").hasClass("d-none")); + + // Add second role $("#top-navbar").append(`<div class="caosdb-user-role second-role">someOtherRole</div>`); // now I do - navbar.hideElementForRoles($("#top-navbar").find("button"), ["someOtherRole"]); - assert.ok($("#top-navbar").find("button").hasClass("d-none")); - $(".second-role").remove(); - // now I don't - navbar.hideElementForRoles($("#top-navbar").find("button"), ["someOtherRole"]); - assert.notOk($("#top-navbar").find("button").hasClass("d-none")); + navbar.hideElementForRoles($("button:contains('TestButton1')"), ["someOtherRole"]); + navbar.hideElementForRoles("button:contains('TestButton2')", ["someOtherRole"]); + assert.ok($("button:contains('TestButton1')").hasClass("d-none")); + assert.ok($("button:contains('TestButton2')").hasClass("d-none")); + // Remove second role again + $(".second-role").remove(); + // now I don't any more + navbar.hideElementForRoles($("button:contains('TestButton1')"), ["someOtherRole"]); + navbar.hideElementForRoles("button:contains('TestButton2')", ["someOtherRole"]); + assert.notOk($("button:contains('TestButton1')").hasClass("d-none")); + assert.notOk($("button:contains('TestButton2')").hasClass("d-none")); }); QUnit.module("webcaosdb.js - version_history", {