configurationMenuBuilder.test.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import { describe, test, vi, expect, beforeEach } from 'vitest'
  2. import type { RuntimeConfig } from '@nuxt/schema'
  3. import type { AnyAbility } from '@casl/ability/dist/types'
  4. import type { Router } from 'vue-router'
  5. import type { AccessProfile, organizationState } from '~/types/interfaces'
  6. import ConfigurationMenuBuilder from '~/services/layout/menuBuilder/configurationMenuBuilder'
  7. import type { MenuGroup } from '~/types/layout'
  8. import { MENU_LINK_TYPE } from '~/types/enum/layout'
  9. let runtimeConfig: RuntimeConfig
  10. let ability: AnyAbility
  11. let organizationProfile: organizationState
  12. let accessProfile: AccessProfile
  13. let menuBuilder: ConfigurationMenuBuilder
  14. let router: Router
  15. beforeEach(() => {
  16. runtimeConfig = vi.fn() as any as RuntimeConfig
  17. ability = vi.fn() as any as AnyAbility
  18. organizationProfile = vi.fn() as any as organizationState
  19. accessProfile = vi.fn() as any as AccessProfile
  20. // @ts-ignore
  21. router = vi.fn() as Router
  22. runtimeConfig.baseUrlAdminLegacy = 'https://mydomain.com/'
  23. menuBuilder = new ConfigurationMenuBuilder(
  24. runtimeConfig,
  25. ability,
  26. organizationProfile,
  27. accessProfile,
  28. router,
  29. )
  30. })
  31. describe('getMenuName', () => {
  32. test('validate name', () => {
  33. expect(menuBuilder.getMenuName()).toEqual('Configuration')
  34. })
  35. })
  36. describe('build', () => {
  37. test('has all items', () => {
  38. ability.can = vi.fn(() => true)
  39. // Should return a MenuGroup
  40. const result = menuBuilder.build() as MenuGroup
  41. expect(result.label).toEqual('configuration')
  42. expect(result.icon).toEqual({ name: 'fas fa-cogs' })
  43. // @ts-ignore
  44. expect(result.children.length).toEqual(13)
  45. })
  46. test('has no items', () => {
  47. ability.can = vi.fn(() => false)
  48. expect(menuBuilder.build()).toEqual(null)
  49. })
  50. test('has only rights for menu organization_page', () => {
  51. ability.can = vi.fn(
  52. (action: string, subject: string) =>
  53. action === 'display' && subject === 'organization_page',
  54. )
  55. organizationProfile.id = 123
  56. expect(menuBuilder.build()).toEqual({
  57. label: 'organization_page',
  58. icon: undefined,
  59. to: 'https://mydomain.com/#/main/organizations/123/dashboard',
  60. type: MENU_LINK_TYPE.V1,
  61. active: false,
  62. })
  63. })
  64. test('has only rights for menu cmf_licence_generate', () => {
  65. ability.can = vi.fn(
  66. (action: string, subject: string) =>
  67. action === 'display' && subject === 'cmf_licence_page',
  68. )
  69. expect(menuBuilder.build()).toEqual({
  70. label: 'cmf_licence_generate',
  71. icon: undefined,
  72. to: '/cmf_licence_structure',
  73. type: MENU_LINK_TYPE.INTERNAL,
  74. active: false,
  75. })
  76. })
  77. test('has only rights for menu parameters', () => {
  78. ability.can = vi.fn(
  79. (action: string, subject: string) =>
  80. action === 'display' && subject === 'parameters_page',
  81. )
  82. expect(menuBuilder.build()).toEqual({
  83. label: 'parameters',
  84. icon: undefined,
  85. to: '/parameters',
  86. type: MENU_LINK_TYPE.INTERNAL,
  87. active: false,
  88. })
  89. })
  90. test('has only rights for menu place', () => {
  91. ability.can = vi.fn(
  92. (action: string, subject: string) =>
  93. action === 'display' && subject === 'place_page',
  94. )
  95. expect(menuBuilder.build()).toEqual({
  96. label: 'places',
  97. icon: undefined,
  98. to: 'https://mydomain.com/#/places/list/',
  99. type: MENU_LINK_TYPE.V1,
  100. active: false,
  101. })
  102. })
  103. test('has only rights for menu education', () => {
  104. ability.can = vi.fn(
  105. (action: string, subject: string) =>
  106. action === 'display' && subject === 'education_page',
  107. )
  108. expect(menuBuilder.build()).toEqual({
  109. label: 'education',
  110. icon: undefined,
  111. to: 'https://mydomain.com/#/educations/list/',
  112. type: MENU_LINK_TYPE.V1,
  113. active: false,
  114. })
  115. })
  116. test('has only rights for menu tag', () => {
  117. ability.can = vi.fn(
  118. (action: string, subject: string) =>
  119. action === 'display' && subject === 'tag_page',
  120. )
  121. expect(menuBuilder.build()).toEqual({
  122. label: 'tags',
  123. icon: undefined,
  124. to: 'https://mydomain.com/#/taggs/list/',
  125. type: MENU_LINK_TYPE.V1,
  126. active: false,
  127. })
  128. })
  129. test('has only rights for menu activities', () => {
  130. ability.can = vi.fn(
  131. (action: string, subject: string) =>
  132. action === 'display' && subject === 'activities_page',
  133. )
  134. expect(menuBuilder.build()).toEqual({
  135. label: 'activities',
  136. icon: undefined,
  137. to: 'https://mydomain.com/#/activities/list/',
  138. type: MENU_LINK_TYPE.V1,
  139. active: false,
  140. })
  141. })
  142. test('has only rights for menu course_duplication', () => {
  143. ability.can = vi.fn(
  144. (action: string, subject: string) =>
  145. action === 'display' && subject === 'course_duplication_page',
  146. )
  147. expect(menuBuilder.build()).toEqual({
  148. label: 'course_duplication',
  149. icon: undefined,
  150. to: 'https://mydomain.com/#/duplicate_courses',
  151. type: MENU_LINK_TYPE.V1,
  152. active: false,
  153. })
  154. })
  155. test('has only rights for menu import', () => {
  156. ability.can = vi.fn(
  157. (action: string, subject: string) =>
  158. action === 'display' && subject === 'import_page',
  159. )
  160. expect(menuBuilder.build()).toEqual({
  161. label: 'import',
  162. icon: undefined,
  163. to: 'https://mydomain.com/#/import/all',
  164. type: MENU_LINK_TYPE.V1,
  165. active: false,
  166. })
  167. })
  168. })