generate-routes-frontend.test.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import type { RouteRecordRaw } from 'vue-router';
  2. import { describe, expect, it } from 'vitest';
  3. import {
  4. generateRoutesByFrontend,
  5. hasAuthority,
  6. } from '../generate-routes-frontend';
  7. // Mock 路由数据
  8. const mockRoutes = [
  9. {
  10. meta: {
  11. authority: ['admin', 'user'],
  12. hideInMenu: false,
  13. },
  14. path: '/dashboard',
  15. children: [
  16. {
  17. path: '/dashboard/overview',
  18. meta: { authority: ['admin'], hideInMenu: false },
  19. },
  20. {
  21. path: '/dashboard/stats',
  22. meta: { authority: ['user'], hideInMenu: true },
  23. },
  24. ],
  25. },
  26. {
  27. meta: { authority: ['admin'], hideInMenu: false },
  28. path: '/settings',
  29. },
  30. {
  31. meta: { hideInMenu: false },
  32. path: '/profile',
  33. },
  34. ] as RouteRecordRaw[];
  35. describe('hasAuthority', () => {
  36. it('should return true if there is no authority defined', () => {
  37. expect(hasAuthority(mockRoutes[2], ['admin'])).toBe(true);
  38. });
  39. it('should return true if the user has the required authority', () => {
  40. expect(hasAuthority(mockRoutes[0], ['admin'])).toBe(true);
  41. });
  42. it('should return false if the user does not have the required authority', () => {
  43. expect(hasAuthority(mockRoutes[1], ['user'])).toBe(false);
  44. });
  45. });
  46. describe('generateRoutesByFrontend', () => {
  47. it('should handle routes without children', async () => {
  48. const generatedRoutes = await generateRoutesByFrontend(mockRoutes, [
  49. 'user',
  50. ]);
  51. expect(generatedRoutes).toEqual(
  52. expect.arrayContaining([
  53. expect.objectContaining({
  54. path: '/profile', // This route has no children and should be included
  55. }),
  56. ]),
  57. );
  58. });
  59. it('should handle empty roles array', async () => {
  60. const generatedRoutes = await generateRoutesByFrontend(mockRoutes, []);
  61. expect(generatedRoutes).toEqual(
  62. expect.arrayContaining([
  63. // Only routes without authority should be included
  64. expect.objectContaining({
  65. path: '/profile',
  66. }),
  67. ]),
  68. );
  69. expect(generatedRoutes).not.toEqual(
  70. expect.arrayContaining([
  71. expect.objectContaining({
  72. path: '/dashboard',
  73. }),
  74. expect.objectContaining({
  75. path: '/settings',
  76. }),
  77. ]),
  78. );
  79. });
  80. it('should handle missing meta fields', async () => {
  81. const routesWithMissingMeta = [
  82. { path: '/path1' }, // No meta
  83. { meta: {}, path: '/path2' }, // Empty meta
  84. { meta: { authority: ['admin'] }, path: '/path3' }, // Only authority
  85. ];
  86. const generatedRoutes = await generateRoutesByFrontend(
  87. routesWithMissingMeta as RouteRecordRaw[],
  88. ['admin'],
  89. );
  90. expect(generatedRoutes).toEqual([
  91. { path: '/path1' },
  92. { meta: {}, path: '/path2' },
  93. { meta: { authority: ['admin'] }, path: '/path3' },
  94. ]);
  95. });
  96. });