configurationMenuBuilder.test.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 ConfigurationMenuBuilder from '~/services/layout/menuBuilder/configurationMenuBuilder'
  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: 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(
  20. runtimeConfig,
  21. ability,
  22. organizationProfile,
  23. accessProfile,
  24. )
  25. })
  26. describe('getMenuName', () => {
  27. test('validate name', () => {
  28. expect(menuBuilder.getMenuName()).toEqual('Configuration')
  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('configuration')
  37. expect(result.icon).toEqual({ name: 'fas fa-cogs' })
  38. // @ts-ignore
  39. expect(result.children.length).toEqual(13)
  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 organization_page', () => {
  46. ability.can = vi.fn(
  47. (action: string, subject: string) =>
  48. action === 'display' && subject === 'organization_page',
  49. )
  50. organizationProfile.id = 123
  51. expect(menuBuilder.build()).toEqual({
  52. label: 'organization_page',
  53. icon: undefined,
  54. to: 'https://mydomain.com/#/main/organizations/123/dashboard',
  55. type: MENU_LINK_TYPE.V1,
  56. active: false,
  57. })
  58. })
  59. test('has only rights for menu cmf_licence_generate', () => {
  60. ability.can = vi.fn(
  61. (action: string, subject: string) =>
  62. action === 'display' && subject === 'cmf_licence_page',
  63. )
  64. expect(menuBuilder.build()).toEqual({
  65. label: 'cmf_licence_generate',
  66. icon: undefined,
  67. to: '/cmf_licence_structure',
  68. type: MENU_LINK_TYPE.INTERNAL,
  69. active: false,
  70. })
  71. })
  72. test('has only rights for menu parameters', () => {
  73. ability.can = vi.fn(
  74. (action: string, subject: string) =>
  75. action === 'display' && subject === 'parameters_page',
  76. )
  77. expect(menuBuilder.build()).toEqual({
  78. label: 'parameters',
  79. icon: undefined,
  80. to: '/parameters',
  81. type: MENU_LINK_TYPE.INTERNAL,
  82. active: false,
  83. })
  84. })
  85. test('has only rights for menu place', () => {
  86. ability.can = vi.fn(
  87. (action: string, subject: string) =>
  88. action === 'display' && subject === 'place_page',
  89. )
  90. expect(menuBuilder.build()).toEqual({
  91. label: 'places',
  92. icon: undefined,
  93. to: 'https://mydomain.com/#/places/list/',
  94. type: MENU_LINK_TYPE.V1,
  95. active: false,
  96. })
  97. })
  98. test('has only rights for menu education', () => {
  99. ability.can = vi.fn(
  100. (action: string, subject: string) =>
  101. action === 'display' && subject === 'education_page',
  102. )
  103. expect(menuBuilder.build()).toEqual({
  104. label: 'education',
  105. icon: undefined,
  106. to: 'https://mydomain.com/#/educations/list/',
  107. type: MENU_LINK_TYPE.V1,
  108. active: false,
  109. })
  110. })
  111. test('has only rights for menu tag', () => {
  112. ability.can = vi.fn(
  113. (action: string, subject: string) =>
  114. action === 'display' && subject === 'tag_page',
  115. )
  116. expect(menuBuilder.build()).toEqual({
  117. label: 'tags',
  118. icon: undefined,
  119. to: 'https://mydomain.com/#/taggs/list/',
  120. type: MENU_LINK_TYPE.V1,
  121. active: false,
  122. })
  123. })
  124. test('has only rights for menu activities', () => {
  125. ability.can = vi.fn(
  126. (action: string, subject: string) =>
  127. action === 'display' && subject === 'activities_page',
  128. )
  129. expect(menuBuilder.build()).toEqual({
  130. label: 'activities',
  131. icon: undefined,
  132. to: 'https://mydomain.com/#/activities/list/',
  133. type: MENU_LINK_TYPE.V1,
  134. active: false,
  135. })
  136. })
  137. test('has only rights for menu course_duplication', () => {
  138. ability.can = vi.fn(
  139. (action: string, subject: string) =>
  140. action === 'display' && subject === 'course_duplication_page',
  141. )
  142. expect(menuBuilder.build()).toEqual({
  143. label: 'course_duplication',
  144. icon: undefined,
  145. to: 'https://mydomain.com/#/duplicate_courses',
  146. type: MENU_LINK_TYPE.V1,
  147. active: false,
  148. })
  149. })
  150. test('has only rights for menu import', () => {
  151. ability.can = vi.fn(
  152. (action: string, subject: string) =>
  153. action === 'display' && subject === 'import_page',
  154. )
  155. expect(menuBuilder.build()).toEqual({
  156. label: 'import',
  157. icon: undefined,
  158. to: 'https://mydomain.com/#/import/all',
  159. type: MENU_LINK_TYPE.V1,
  160. active: false,
  161. })
  162. })
  163. })