Skip to content

Commit

Permalink
fix: PR changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Amogh Sahasrabhojanee committed Oct 9, 2020
1 parent af1ebe7 commit 5275f61
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 34 deletions.
16 changes: 4 additions & 12 deletions src/molecules/navbar/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import get from "lodash.get";
* if featureConfig isn't passed, check if menuGroup with slug "amp-sidebar-menu-${domainSlug}" is present and return those items
* if that's not set, check if menuGroup with slug "sidebar-menu-${domainSlug}" is present and return those items
* if that's not set either, return default menu group items
* return default menu group items in case there are no items in any of the above menuGroups
*
* @param {Object} config config object
* @category Helpers
* @param {Object} config object
* @returns {Array} Menu Group Items that should be shown inside the hamburger menu
*/

Expand All @@ -20,26 +20,18 @@ export const getDomainSpecificHamburgerMenuItems = (config: Config) => {

const domainSlug = config.opts?.domainSlug;
const defaultMenuGroupItems = get(config, ["ampConfig", "menu-groups", "default", "items"], []);
const allMenuGroupsArr: MenuGroupItemsTypes[] = objToArr(ampConfig["menu-groups"]);
const allMenuGroupsArr: MenuGroupItemsTypes[] = Object.values(ampConfig["menu-groups"]);

if (allMenuGroupsArr.length) {
const sidebarMenuGroupSlug = getSidebarMenuSlug(allMenuGroupsArr, config, domainSlug);
if (sidebarMenuGroupSlug) {
const menuGroupToReturn = allMenuGroupsArr.find((menuGroup) => menuGroup?.slug === sidebarMenuGroupSlug);
const menuGroupToReturn = allMenuGroupsArr.find((menuGroup) => menuGroup.slug === sidebarMenuGroupSlug);
if (menuGroupToReturn && menuGroupToReturn.items.length) return menuGroupToReturn.items;
}
}
return defaultMenuGroupItems;
};

// "menu-groups" in api/v1/amp/config should've been an array but it's an obj. Therefore doing this hack
export const objToArr = (obj: object) => {
const keysArr = Object.keys(obj);
const arr: MenuGroupItemsTypes[] = [];
keysArr.forEach((key) => obj[key] && arr.push(obj[key]));
return arr;
};

const getSidebarMenuSlug = (arr, config, domainSlug) => {
// returns slug of the menu group to take
// returns menuGroupSlug from featureConfig OR "amp-sidebar-menu-<domainSlug>" OR "sidebar-menu-<domainSlug>" OR null in this order
Expand Down
23 changes: 1 addition & 22 deletions src/molecules/navbar/navbar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { configWithMenuDisabled, configWithNoHamburgerMenuItems, configWithTextD
import { Layout, HamburgerMenu } from "../../atoms";
import { textStory, config } from "../../__fixtures__";
import { Hamburger } from "../../atoms/icons/hamburger";
import { getDomainSpecificHamburgerMenuItems, objToArr } from "./helpers";
import { getDomainSpecificHamburgerMenuItems } from "./helpers";
import {
defaultMenuItems,
dummyConfig1,
Expand Down Expand Up @@ -85,27 +85,6 @@ describe("Navbar", () => {
});

// tests for helper functions
describe("objToArr helper function", () => {
it("should work", () => {
const obj = {
foo: {
a: 1,
b: "two"
},
bar: 42,
baz: ["a", "b"]
};
expect(objToArr(obj)).toMatchObject([
{
a: 1,
b: "two"
},
42,
["a", "b"]
]);
});
});

describe("getDomainSpecificHamburgerMenuItems helper function", () => {
it("Should return default menu group items for publisher having no associated subdomains", () => {
const menuItems = getDomainSpecificHamburgerMenuItems(dummyConfig1);
Expand Down

0 comments on commit 5275f61

Please sign in to comment.