configurationMenuBuilder.test.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 ConfigurationMenuBuilder from "~/services/layout/menuBuilder/configurationMenuBuilder";
  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: ConfigurationMenuBuilder
  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 ConfigurationMenuBuilder(runtimeConfig, ability, organizationProfile, accessProfile)
  20. })
  21. describe('getMenuName', () => {
  22. test('validate name', () => {
  23. expect(menuBuilder.getMenuName()).toEqual("Configuration")
  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('configuration')
  32. expect(result.icon).toEqual({name: 'fas fa-cogs'})
  33. // @ts-ignore
  34. expect(result.children.length).toEqual(12)
  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 organization_page', () => {
  41. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'organization_page')
  42. organizationProfile.id = 123
  43. expect(menuBuilder.build()).toEqual(
  44. {label: 'organization_page', icon: undefined, to: 'https://mydomain.com/#/main/organizations/123/dashboard', type: MENU_LINK_TYPE.V1, active: false}
  45. )
  46. })
  47. test('has only rights for menu cmf_licence_generate', () => {
  48. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'cmf_licence_page')
  49. expect(menuBuilder.build()).toEqual(
  50. {label: 'cmf_licence_generate', icon: undefined, to: 'https://mydomain.com/#/licence_cmf/organization', type: MENU_LINK_TYPE.V1, active: false}
  51. )
  52. })
  53. test('has only rights for menu place', () => {
  54. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'place_page')
  55. expect(menuBuilder.build()).toEqual(
  56. {label: 'place', icon: undefined, to: 'https://mydomain.com/#/places/list/', type: MENU_LINK_TYPE.V1, active: false}
  57. )
  58. })
  59. test('has only rights for menu education', () => {
  60. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'education_page')
  61. expect(menuBuilder.build()).toEqual(
  62. {label: 'education', icon: undefined, to: 'https://mydomain.com/#/educations/list/', type: MENU_LINK_TYPE.V1, active: false}
  63. )
  64. })
  65. test('has only rights for menu tag', () => {
  66. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'tag_page')
  67. expect(menuBuilder.build()).toEqual(
  68. {label: 'tag', icon: undefined, to: 'https://mydomain.com/#/taggs/list/', type: MENU_LINK_TYPE.V1, active: false}
  69. )
  70. })
  71. test('has only rights for menu activities', () => {
  72. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'activities_page')
  73. expect(menuBuilder.build()).toEqual(
  74. {label: 'activities', icon: undefined, to: 'https://mydomain.com/#/activities/list/', type: MENU_LINK_TYPE.V1, active: false}
  75. )
  76. })
  77. test('has only rights for menu course_duplication', () => {
  78. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'course_duplication_page')
  79. expect(menuBuilder.build()).toEqual(
  80. {label: 'course_duplication', icon: undefined, to: 'https://mydomain.com/#/duplicate_courses', type: MENU_LINK_TYPE.V1, active: false}
  81. )
  82. })
  83. test('has only rights for menu import', () => {
  84. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'import_page')
  85. expect(menuBuilder.build()).toEqual(
  86. {label: 'import', icon: undefined, to: 'https://mydomain.com/#/import/all', type: MENU_LINK_TYPE.V1, active: false}
  87. )
  88. })
  89. })