statsMenuBuilder.test.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { describe, test, it, expect } 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 fede_stats', () => {
  47. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'fede_stats_page')
  48. expect(menuBuilder.build()).toEqual(
  49. {label: 'fede_stats', icon: {name: 'fas fa-chart-bar'}, to: 'https://mydomain.com/#/statistic/membersfedeonly', type: MENU_LINK_TYPE.V1, active: false}
  50. )
  51. })
  52. test('has only rights for menu structure_stats', () => {
  53. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'structure_stats_page')
  54. expect(menuBuilder.build()).toEqual(
  55. {label: 'structure_stats', icon: {name: 'fas fa-chart-bar'}, to: 'https://mydomain.com/#/statistic/membersfedeassos', type: MENU_LINK_TYPE.V1, active: false}
  56. )
  57. })
  58. })