statsMenuBuilder.test.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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(runtimeConfig, ability, organizationProfile, accessProfile)
  20. })
  21. describe('getMenuName', () => {
  22. test('validate name', () => {
  23. expect(menuBuilder.getMenuName()).toEqual("Stats")
  24. })
  25. })
  26. describe('build', () => {
  27. test('has all items', () => {
  28. ability.can = vi.fn(() => true)
  29. // Should return a MenuGroup
  30. const result = menuBuilder.build() as MenuGroup
  31. expect(result.label).toEqual('stats')
  32. expect(result.icon).toEqual({name: 'fas fa-chart-bar'})
  33. // @ts-ignore
  34. expect(result.children.length).toEqual(4)
  35. })
  36. test('has no items', () => {
  37. ability.can = vi.fn(() => false)
  38. expect(menuBuilder.build()).toEqual(null)
  39. })
  40. test('has only rights for menu report_activity', () => {
  41. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'report_activity_page')
  42. expect(menuBuilder.build()).toEqual(
  43. {label: 'report_activity', icon: {name: 'fas fa-chart-bar'}, to: 'https://mydomain.com/#/report_activity', type: MENU_LINK_TYPE.V1, active: false}
  44. )
  45. })
  46. test('has only rights for menu educations_quotas_by_education and accesses_quotas_courses_hebdos', () => {
  47. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'education_quotas_page')
  48. const group = menuBuilder.build() as MenuGroup
  49. expect(group.children).toEqual(
  50. [
  51. {label: 'educations_quotas_by_education', icon: {name: 'fas fa-user-circle'}, to: 'https://mydomain.com/#/educations_quotas_by_education_year/list/', type: MENU_LINK_TYPE.V1, active: false},
  52. {label: 'accesses_quotas_courses_hebdos', icon: {name: 'fas fa-user-circle'}, to: 'https://mydomain.com/#/accesses_quotas_courses_hebdos/list/', type: MENU_LINK_TYPE.V1, active: false}
  53. ]
  54. )
  55. })
  56. test('has only rights for menu fede_stats', () => {
  57. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'fede_stats_page')
  58. expect(menuBuilder.build()).toEqual(
  59. {label: 'fede_stats', icon: {name: 'fas fa-chart-bar'}, to: 'https://mydomain.com/#/statistic/membersfedeonly', type: MENU_LINK_TYPE.V1, active: false}
  60. )
  61. })
  62. })