statsMenuBuilder.test.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { describe, test, expect, vi, beforeEach } from 'vitest'
  2. import type { RuntimeConfig } from '@nuxt/schema'
  3. //@ts-expect-error false error
  4. import type { AnyAbility } from '@casl/ability/dist/types'
  5. import type { Router } from 'vue-router'
  6. import type { AccessProfile, organizationState } from '~/types/interfaces'
  7. import StatsMenuBuilder from '~/services/layout/menuBuilder/statsMenuBuilder'
  8. import type { MenuGroup } from '~/types/layout'
  9. import { MENU_LINK_TYPE } from '~/types/enum/layout'
  10. let runtimeConfig: RuntimeConfig
  11. let ability: AnyAbility
  12. let organizationProfile: organizationState
  13. let accessProfile: AccessProfile
  14. let menuBuilder: StatsMenuBuilder
  15. let router: Router
  16. beforeEach(() => {
  17. runtimeConfig = vi.fn() as any as RuntimeConfig
  18. ability = vi.fn() as any as AnyAbility
  19. organizationProfile = vi.fn() as any as organizationState
  20. accessProfile = vi.fn() as any as AccessProfile
  21. // @ts-ignore
  22. router = vi.fn() as Router
  23. runtimeConfig.baseUrlAdminLegacy = 'https://mydomain.com/'
  24. menuBuilder = new StatsMenuBuilder(
  25. runtimeConfig,
  26. ability,
  27. organizationProfile,
  28. accessProfile,
  29. router,
  30. )
  31. })
  32. describe('getMenuName', () => {
  33. test('validate name', () => {
  34. expect(menuBuilder.getMenuName()).toEqual('Stats')
  35. })
  36. })
  37. describe('build', () => {
  38. test('has all items', () => {
  39. ability.can = vi.fn(() => true)
  40. // Should return a MenuGroup
  41. const result = menuBuilder.build() as MenuGroup
  42. expect(result.label).toEqual('stats')
  43. expect(result.icon).toEqual({ name: 'fas fa-chart-bar' })
  44. // @ts-ignore
  45. expect(result.children.length).toEqual(4)
  46. })
  47. test('has no items', () => {
  48. ability.can = vi.fn(() => false)
  49. expect(menuBuilder.build()).toEqual(null)
  50. })
  51. test('has only rights for menu report_activity', () => {
  52. ability.can = vi.fn(
  53. (action: string, subject: string) =>
  54. action === 'display' && subject === 'report_activity_page',
  55. )
  56. expect(menuBuilder.build()).toEqual({
  57. label: 'report_activity',
  58. icon: { name: 'fas fa-chart-bar' },
  59. to: 'https://mydomain.com/#/report_activity',
  60. target: '_self',
  61. type: MENU_LINK_TYPE.V1,
  62. active: false,
  63. endOfSubsection: false,
  64. })
  65. })
  66. test('has only rights for menu educations_quotas_by_education and accesses_quotas_courses_hebdos', () => {
  67. ability.can = vi.fn(
  68. (action: string, subject: string) =>
  69. action === 'display' && subject === 'education_quotas_page',
  70. )
  71. const group = menuBuilder.build() as MenuGroup
  72. expect(group.children).toEqual([
  73. {
  74. label: 'educations_quotas_by_education',
  75. icon: { name: 'fas fa-user-circle' },
  76. to: 'https://mydomain.com/#/educations_quotas_by_education_year/list/',
  77. target: '_self',
  78. type: MENU_LINK_TYPE.V1,
  79. active: false,
  80. endOfSubsection: false,
  81. },
  82. {
  83. label: 'accesses_quotas_courses_hebdos',
  84. icon: { name: 'fas fa-user-circle' },
  85. to: 'https://mydomain.com/#/accesses_quotas_courses_hebdos/list/',
  86. target: '_self',
  87. type: MENU_LINK_TYPE.V1,
  88. active: false,
  89. endOfSubsection: false,
  90. },
  91. ])
  92. })
  93. test('has only rights for menu fede_stats', () => {
  94. ability.can = vi.fn(
  95. (action: string, subject: string) =>
  96. action === 'display' && subject === 'fede_stats_page',
  97. )
  98. expect(menuBuilder.build()).toEqual({
  99. label: 'fede_stats',
  100. icon: { name: 'fas fa-chart-bar' },
  101. to: 'https://mydomain.com/#/statistic/membersfedeonly',
  102. target: '_self',
  103. type: MENU_LINK_TYPE.V1,
  104. active: false,
  105. endOfSubsection: false,
  106. })
  107. })
  108. })