generate-menus.test.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import { describe, expect, it, vi } from 'vitest';
  2. import { generateMenus } from './generate-menus'; // 替换为您的实际路径
  3. import {
  4. type RouteRecordRaw,
  5. type Router,
  6. createRouter,
  7. createWebHistory,
  8. } from 'vue-router';
  9. // Nested route setup to test child inclusion and hideChildrenInMenu functionality
  10. describe('generateMenus', () => {
  11. // 模拟路由数据
  12. const mockRoutes = [
  13. {
  14. meta: { icon: 'home-icon', title: '首页' },
  15. name: 'home',
  16. path: '/home',
  17. },
  18. {
  19. meta: { hideChildrenInMenu: true, icon: 'about-icon', title: '关于' },
  20. name: 'about',
  21. path: '/about',
  22. children: [
  23. {
  24. path: 'team',
  25. name: 'team',
  26. meta: { icon: 'team-icon', title: '团队' },
  27. },
  28. ],
  29. },
  30. ] as RouteRecordRaw[];
  31. // 模拟 Vue 路由器实例
  32. const mockRouter = {
  33. getRoutes: vi.fn(() => [
  34. { name: 'home', path: '/home' },
  35. { name: 'about', path: '/about' },
  36. { name: 'team', path: '/about/team' },
  37. ]),
  38. };
  39. it('the correct menu list should be generated according to the route', async () => {
  40. const expectedMenus = [
  41. {
  42. badge: undefined,
  43. badgeType: undefined,
  44. badgeVariants: undefined,
  45. icon: 'home-icon',
  46. name: '首页',
  47. order: undefined,
  48. parent: undefined,
  49. parents: undefined,
  50. path: '/home',
  51. children: [],
  52. },
  53. {
  54. badge: undefined,
  55. badgeType: undefined,
  56. badgeVariants: undefined,
  57. icon: 'about-icon',
  58. name: '关于',
  59. order: undefined,
  60. parent: undefined,
  61. parents: undefined,
  62. path: '/about',
  63. children: [],
  64. },
  65. ];
  66. const menus = await generateMenus(mockRoutes, mockRouter as any);
  67. expect(menus).toEqual(expectedMenus);
  68. });
  69. it('includes additional meta properties in menu items', async () => {
  70. const mockRoutesWithMeta = [
  71. {
  72. meta: { icon: 'user-icon', order: 1, title: 'Profile' },
  73. name: 'profile',
  74. path: '/profile',
  75. },
  76. ] as RouteRecordRaw[];
  77. const menus = await generateMenus(mockRoutesWithMeta, mockRouter as any);
  78. expect(menus).toEqual([
  79. {
  80. badge: undefined,
  81. badgeType: undefined,
  82. badgeVariants: undefined,
  83. icon: 'user-icon',
  84. name: 'Profile',
  85. order: 1,
  86. parent: undefined,
  87. parents: undefined,
  88. path: '/profile',
  89. children: [],
  90. },
  91. ]);
  92. });
  93. it('handles dynamic route parameters correctly', async () => {
  94. const mockRoutesWithParams = [
  95. {
  96. meta: { icon: 'details-icon', title: 'User Details' },
  97. name: 'userDetails',
  98. path: '/users/:userId',
  99. },
  100. ] as RouteRecordRaw[];
  101. const menus = await generateMenus(mockRoutesWithParams, mockRouter as any);
  102. expect(menus).toEqual([
  103. {
  104. badge: undefined,
  105. badgeType: undefined,
  106. badgeVariants: undefined,
  107. icon: 'details-icon',
  108. name: 'User Details',
  109. order: undefined,
  110. parent: undefined,
  111. parents: undefined,
  112. path: '/users/:userId',
  113. children: [],
  114. },
  115. ]);
  116. });
  117. it('processes routes with redirects correctly', async () => {
  118. const mockRoutesWithRedirect = [
  119. {
  120. name: 'redirectedRoute',
  121. path: '/old-path',
  122. redirect: '/new-path',
  123. },
  124. {
  125. meta: { icon: 'path-icon', title: 'New Path' },
  126. name: 'newPath',
  127. path: '/new-path',
  128. },
  129. ] as RouteRecordRaw[];
  130. const menus = await generateMenus(
  131. mockRoutesWithRedirect,
  132. mockRouter as any,
  133. );
  134. expect(menus).toEqual([
  135. // Assuming your generateMenus function excludes redirect routes from the menu
  136. {
  137. badge: undefined,
  138. badgeType: undefined,
  139. badgeVariants: undefined,
  140. icon: undefined,
  141. name: 'redirectedRoute',
  142. order: undefined,
  143. parent: undefined,
  144. parents: undefined,
  145. path: '/old-path',
  146. children: [],
  147. },
  148. {
  149. badge: undefined,
  150. badgeType: undefined,
  151. badgeVariants: undefined,
  152. icon: 'path-icon',
  153. name: 'New Path',
  154. order: undefined,
  155. parent: undefined,
  156. parents: undefined,
  157. path: '/new-path',
  158. children: [],
  159. },
  160. ]);
  161. });
  162. const routes: any = [
  163. {
  164. meta: { order: 2, title: 'Home' },
  165. name: 'home',
  166. path: '/',
  167. },
  168. {
  169. meta: { order: 1, title: 'About' },
  170. name: 'about',
  171. path: '/about',
  172. },
  173. ];
  174. const router: Router = createRouter({
  175. history: createWebHistory(),
  176. routes,
  177. });
  178. it('should generate menu list with correct order', async () => {
  179. const menus = await generateMenus(routes, router);
  180. const expectedMenus = [
  181. {
  182. badge: undefined,
  183. badgeType: undefined,
  184. badgeVariants: undefined,
  185. icon: undefined,
  186. name: 'About',
  187. order: 1,
  188. parent: undefined,
  189. parents: undefined,
  190. path: '/about',
  191. children: [],
  192. },
  193. {
  194. badge: undefined,
  195. badgeType: undefined,
  196. badgeVariants: undefined,
  197. icon: undefined,
  198. name: 'Home',
  199. order: 2,
  200. parent: undefined,
  201. parents: undefined,
  202. path: '/',
  203. children: [],
  204. },
  205. ];
  206. expect(menus).toEqual(expectedMenus);
  207. });
  208. it('should handle empty routes', async () => {
  209. const emptyRoutes: any[] = [];
  210. const menus = await generateMenus(emptyRoutes, router);
  211. expect(menus).toEqual([]);
  212. });
  213. });