generate-menus.test.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. show: true,
  52. children: [],
  53. },
  54. {
  55. badge: undefined,
  56. badgeType: undefined,
  57. badgeVariants: undefined,
  58. icon: 'about-icon',
  59. name: '关于',
  60. order: undefined,
  61. parent: undefined,
  62. parents: undefined,
  63. path: '/about',
  64. show: true,
  65. children: [],
  66. },
  67. ];
  68. const menus = await generateMenus(mockRoutes, mockRouter as any);
  69. expect(menus).toEqual(expectedMenus);
  70. });
  71. it('includes additional meta properties in menu items', async () => {
  72. const mockRoutesWithMeta = [
  73. {
  74. meta: { icon: 'user-icon', order: 1, title: 'Profile' },
  75. name: 'profile',
  76. path: '/profile',
  77. },
  78. ] as RouteRecordRaw[];
  79. const menus = await generateMenus(mockRoutesWithMeta, mockRouter as any);
  80. expect(menus).toEqual([
  81. {
  82. badge: undefined,
  83. badgeType: undefined,
  84. badgeVariants: undefined,
  85. icon: 'user-icon',
  86. name: 'Profile',
  87. order: 1,
  88. parent: undefined,
  89. parents: undefined,
  90. path: '/profile',
  91. show: true,
  92. children: [],
  93. },
  94. ]);
  95. });
  96. it('handles dynamic route parameters correctly', async () => {
  97. const mockRoutesWithParams = [
  98. {
  99. meta: { icon: 'details-icon', title: 'User Details' },
  100. name: 'userDetails',
  101. path: '/users/:userId',
  102. },
  103. ] as RouteRecordRaw[];
  104. const menus = await generateMenus(mockRoutesWithParams, mockRouter as any);
  105. expect(menus).toEqual([
  106. {
  107. badge: undefined,
  108. badgeType: undefined,
  109. badgeVariants: undefined,
  110. icon: 'details-icon',
  111. name: 'User Details',
  112. order: undefined,
  113. parent: undefined,
  114. parents: undefined,
  115. path: '/users/:userId',
  116. show: true,
  117. children: [],
  118. },
  119. ]);
  120. });
  121. it('processes routes with redirects correctly', async () => {
  122. const mockRoutesWithRedirect = [
  123. {
  124. name: 'redirectedRoute',
  125. path: '/old-path',
  126. redirect: '/new-path',
  127. },
  128. {
  129. meta: { icon: 'path-icon', title: 'New Path' },
  130. name: 'newPath',
  131. path: '/new-path',
  132. },
  133. ] as RouteRecordRaw[];
  134. const menus = await generateMenus(
  135. mockRoutesWithRedirect,
  136. mockRouter as any,
  137. );
  138. expect(menus).toEqual([
  139. // Assuming your generateMenus function excludes redirect routes from the menu
  140. {
  141. badge: undefined,
  142. badgeType: undefined,
  143. badgeVariants: undefined,
  144. icon: undefined,
  145. name: 'redirectedRoute',
  146. order: undefined,
  147. parent: undefined,
  148. parents: undefined,
  149. path: '/old-path',
  150. show: true,
  151. children: [],
  152. },
  153. {
  154. badge: undefined,
  155. badgeType: undefined,
  156. badgeVariants: undefined,
  157. icon: 'path-icon',
  158. name: 'New Path',
  159. order: undefined,
  160. parent: undefined,
  161. parents: undefined,
  162. path: '/new-path',
  163. show: true,
  164. children: [],
  165. },
  166. ]);
  167. });
  168. const routes: any = [
  169. {
  170. meta: { order: 2, title: 'Home' },
  171. name: 'home',
  172. path: '/',
  173. },
  174. {
  175. meta: { order: 1, title: 'About' },
  176. name: 'about',
  177. path: '/about',
  178. },
  179. ];
  180. const router: Router = createRouter({
  181. history: createWebHistory(),
  182. routes,
  183. });
  184. it('should generate menu list with correct order', async () => {
  185. const menus = await generateMenus(routes, router);
  186. const expectedMenus = [
  187. {
  188. badge: undefined,
  189. badgeType: undefined,
  190. badgeVariants: undefined,
  191. icon: undefined,
  192. name: 'About',
  193. order: 1,
  194. parent: undefined,
  195. parents: undefined,
  196. path: '/about',
  197. show: true,
  198. children: [],
  199. },
  200. {
  201. badge: undefined,
  202. badgeType: undefined,
  203. badgeVariants: undefined,
  204. icon: undefined,
  205. name: 'Home',
  206. order: 2,
  207. parent: undefined,
  208. parents: undefined,
  209. path: '/',
  210. show: true,
  211. children: [],
  212. },
  213. ];
  214. expect(menus).toEqual(expectedMenus);
  215. });
  216. it('should handle empty routes', async () => {
  217. const emptyRoutes: any[] = [];
  218. const menus = await generateMenus(emptyRoutes, router);
  219. expect(menus).toEqual([]);
  220. });
  221. });