statsMenuBuilder.test.ts 3.2 KB

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