import { shallowMount } from "@vue/test-utils";
import { describe, expect, it, test } from "vitest";
import Menu from "../src/ZMenu.vue";

describe("ZMenu.vue", () => {
  // 检查是否根据所传参数正确渲染出组件和子组件
  it("renders the menu and its components correctly", async () => {
    const wrapper = shallowMount(Menu, {
      props: {
        themeColor: "#fff",
        treelists: [
          {
            resKey: "1",
            icon: "icon-xiangmu1",
            resName: "菜单1",
            children: [
              {
                resKey: "1-1",
                resName: "子菜单1-1",
                children: [],
              },
              {
                resKey: "1-2",
                resName: "子菜单1-2",
                children: [],
              },
            ],
          },
          {
            resKey: "2",
            icon: "icon-xiangmu2",
            resName: "菜单2",
            children: [
              {
                resKey: "2-1",
                resName: "子菜单2-1",
                children: [
                  {
                    resKey: "2-1-1",
                    resName: "子菜单2-1-1",
                  },
                  {
                    resKey: "2-1-2",
                    resName: "子菜单2-1-2",
                  },
                ],
              },
              {
                resKey: "2-2",
                resName: "子菜单2-2",
                children: [],
              },
            ],
          },
        ],
      },
    });
    // 面包屑默认显示首页
    expect(wrapper.find(".bread_list li a").text()).toEqual("首页");
    // 渲染出来了菜单1模块
    expect(wrapper.find(".nav_left ul:nth-child(1) b").text()).toEqual("菜单1");
    // 渲染出来了菜单2模块
    expect(wrapper.find(".nav_left ul:nth-child(2) b").text()).toEqual("菜单2");
    //模拟点击菜单展开handleToIndex事件
    wrapper.find(".nav_left ul:nth-child(1)  b").trigger("click");
    // 等待下一个事件循环
    await wrapper.vm.$nextTick();
    // 检查是否没有触发 "handleMenuClick" 事件
    expect(wrapper.emitted().handleToIndex).toBeTruthy();
    // 模拟点击菜单路由跳转handleMenuClick事件
    wrapper.find(".nav_left menu_link:nth-child(1)").trigger("click");
    //等待下一个事件循环
    await wrapper.vm.$nextTick();
    // 检查是否没有触发 "handleMenuClick" 事件
    expect(wrapper.emitted().handleMenuClick).toBeFalsy();
    wrapper.unmount();
  });
});
