statsMenuBuilder.test.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import {describe, expect, test} from 'vitest'
  2. import {RuntimeConfig} from "@nuxt/schema";
  3. import {AnyAbility} from "@casl/ability/dist/types";
  4. import {AccessProfile, organizationState} from "~/types/interfaces";
  5. import StatsMenuBuilder from "~/services/layout/menuBuilder/statsMenuBuilder";
  6. import {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. })