generate-menus.test.ts 5.7 KB

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