statsMenuBuilder.test.ts 3.4 KB

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