generate-routes-frontend.test.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import type { RouteRecordRaw } from 'vue-router';
  2. import { describe, expect, it } from 'vitest';
  3. import {
  4. generateRoutesByFrontend,
  5. hasAuthority,
  6. hasVisible,
  7. } from './generate-routes-frontend';
  8. // Mock 路由数据
  9. const mockRoutes = [
  10. {
  11. meta: {
  12. authority: ['admin', 'user'],
  13. hideInMenu: false,
  14. },
  15. path: '/dashboard',
  16. children: [
  17. {
  18. path: '/dashboard/overview',
  19. meta: { authority: ['admin'], hideInMenu: false },
  20. },
  21. {
  22. path: '/dashboard/stats',
  23. meta: { authority: ['user'], hideInMenu: true },
  24. },
  25. ],
  26. },
  27. {
  28. meta: { authority: ['admin'], hideInMenu: false },
  29. path: '/settings',
  30. },
  31. {
  32. meta: { hideInMenu: false },
  33. path: '/profile',
  34. },
  35. ] as RouteRecordRaw[];
  36. describe('hasAuthority', () => {
  37. it('should return true if there is no authority defined', () => {
  38. expect(hasAuthority(mockRoutes[2], ['admin'])).toBe(true);
  39. });
  40. it('should return true if the user has the required authority', () => {
  41. expect(hasAuthority(mockRoutes[0], ['admin'])).toBe(true);
  42. });
  43. it('should return false if the user does not have the required authority', () => {
  44. expect(hasAuthority(mockRoutes[1], ['user'])).toBe(false);
  45. });
  46. });
  47. describe('hasVisible', () => {
  48. it('should return true if hideInMenu is not set or false', () => {
  49. expect(hasVisible(mockRoutes[0])).toBe(true);
  50. expect(hasVisible(mockRoutes[2])).toBe(true);
  51. });
  52. it('should return false if hideInMenu is true', () => {
  53. expect(hasVisible(mockRoutes[0].children?.[1])).toBe(false);
  54. });
  55. });
  56. describe('generateRoutesByFrontend', () => {
  57. it('should filter routes based on authority and visibility', async () => {
  58. const generatedRoutes = await generateRoutesByFrontend(mockRoutes, [
  59. 'user',
  60. ]);
  61. // The user should have access to /dashboard/stats, but it should be filtered out because it's not visible
  62. expect(generatedRoutes).toEqual([
  63. {
  64. meta: { authority: ['admin', 'user'], hideInMenu: false },
  65. path: '/dashboard',
  66. children: [],
  67. },
  68. // Note: We expect /settings to be filtered out because the user does not have 'admin' authority
  69. {
  70. meta: { hideInMenu: false },
  71. path: '/profile',
  72. },
  73. ]);
  74. });
  75. it('should handle routes without children', async () => {
  76. const generatedRoutes = await generateRoutesByFrontend(mockRoutes, [
  77. 'user',
  78. ]);
  79. expect(generatedRoutes).toEqual(
  80. expect.arrayContaining([
  81. expect.objectContaining({
  82. path: '/profile', // This route has no children and should be included
  83. }),
  84. ]),
  85. );
  86. });
  87. it('should handle empty roles array', async () => {
  88. const generatedRoutes = await generateRoutesByFrontend(mockRoutes, []);
  89. expect(generatedRoutes).toEqual(
  90. expect.arrayContaining([
  91. // Only routes without authority should be included
  92. expect.objectContaining({
  93. path: '/profile',
  94. }),
  95. ]),
  96. );
  97. expect(generatedRoutes).not.toEqual(
  98. expect.arrayContaining([
  99. expect.objectContaining({
  100. path: '/dashboard',
  101. }),
  102. expect.objectContaining({
  103. path: '/settings',
  104. }),
  105. ]),
  106. );
  107. });
  108. it('should handle missing meta fields', async () => {
  109. const routesWithMissingMeta = [
  110. { path: '/path1' }, // No meta
  111. { meta: {}, path: '/path2' }, // Empty meta
  112. { meta: { authority: ['admin'] }, path: '/path3' }, // Only authority
  113. ];
  114. const generatedRoutes = await generateRoutesByFrontend(
  115. routesWithMissingMeta as RouteRecordRaw[],
  116. ['admin'],
  117. );
  118. expect(generatedRoutes).toEqual([
  119. { path: '/path1' },
  120. { meta: {}, path: '/path2' },
  121. { meta: { authority: ['admin'] }, path: '/path3' },
  122. ]);
  123. });
  124. });