Untitled

 avatar
unknown
plain_text
2 months ago
3.1 kB
3
Indexable
import { menuStructureService } from "@components/SecondaryNavigation/common/menuStructureService";
import { MORE_BUTTON_ITEM_ID } from "@components/SecondaryNavigation/common/constants";

describe("menuStructureService", () => {
  describe("isTopLevelItem", () => {
    it("should return true for an item with no parentId", () => {
      const item = { id: "item_1", parentId: undefined };
      expect(menuStructureService.isTopLevelItem(item)).toBe(true);
    });

    it("should return true for an item whose parentId is MORE_BUTTON_ITEM_ID", () => {
      const item = { id: "item_2", parentId: MORE_BUTTON_ITEM_ID };
      expect(menuStructureService.isTopLevelItem(item)).toBe(true);
    });

    it("should return false for MORE_BUTTON_ITEM_ID itself", () => {
      const item = { id: MORE_BUTTON_ITEM_ID, parentId: undefined };
      expect(menuStructureService.isTopLevelItem(item)).toBe(false);
    });

    it("should return false for an item with a valid parentId other than MORE_BUTTON_ITEM_ID", () => {
      const item = { id: "item_3", parentId: "item_1" };
      expect(menuStructureService.isTopLevelItem(item)).toBe(false);
    });
  });

  describe("makeBackItem", () => {
    it("should add back item properties to a menu item", () => {
      const item = { id: "item_1", active: true, hasActivePath: true };
      const backItem = menuStructureService.makeBackItem(item);

      expect(backItem).toEqual({
        ...item,
        isBackItem: true,
        active: false,
        hasActivePath: false
      });
    });
  });

  describe("flattenMenu", () => {
    it("should correctly flatten a nested menu structure", () => {
      const menu = [
        {
          title: "Item 1",
          path: "/item1",
          active: false,
          items: [
            { title: "Subitem 1", path: "/item1/sub1", active: false },
            { title: "Subitem 2", path: "/item1/sub2", active: true }
          ]
        },
        { title: "Item 2", path: "/item2", active: false }
      ];

      const flatMenu = menuStructureService.flattenMenu(menu);
      
      expect(Object.keys(flatMenu)).toContain("item_1");
      expect(Object.keys(flatMenu)).toContain("item_1_1");
      expect(Object.keys(flatMenu)).toContain("item_1_2");
      expect(Object.keys(flatMenu)).toContain("item_2");
      expect(Object.keys(flatMenu)).toContain(MORE_BUTTON_ITEM_ID);
      
      expect(flatMenu["item_1"]).toEqual(
        expect.objectContaining({ title: "Item 1", path: "/item1", active: false })
      );
      expect(flatMenu["item_1_1"]).toEqual(
        expect.objectContaining({ title: "Subitem 1", path: "/item1/sub1", active: false })
      );
      expect(flatMenu["item_1_2"]).toEqual(
        expect.objectContaining({ title: "Subitem 2", path: "/item1/sub2", active: true })
      );
      expect(flatMenu["item_2"]).toEqual(
        expect.objectContaining({ title: "Item 2", path: "/item2", active: false })
      );
      expect(flatMenu[MORE_BUTTON_ITEM_ID]).toEqual(
        expect.objectContaining({ id: MORE_BUTTON_ITEM_ID, title: "More", active: false })
      );
    });
  });
});
Editor is loading...
Leave a Comment