configurationMenuBuilder.test.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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: 'https://mydomain.com/#/licence_cmf/organization',
  73. type: MENU_LINK_TYPE.V1,
  74. active: false,
  75. })
  76. //
  77. // expect(menuBuilder.build()).toEqual({
  78. // label: 'cmf_licence_generate',
  79. // icon: undefined,
  80. // to: '/cmf_licence_structure',
  81. // type: MENU_LINK_TYPE.INTERNAL,
  82. // active: false,
  83. // })
  84. })
  85. test('has only rights for menu parameters', () => {
  86. ability.can = vi.fn(
  87. (action: string, subject: string) =>
  88. action === 'display' && subject === 'parameters_page',
  89. )
  90. menuBuilder.organizationProfile.id = 123
  91. expect(menuBuilder.build()).toEqual({
  92. label: 'parameters',
  93. icon: undefined,
  94. to: 'https://mydomain.com/#/main/edit/parameters/123',
  95. type: MENU_LINK_TYPE.V1,
  96. active: false,
  97. })
  98. // expect(menuBuilder.build()).toEqual({
  99. // label: 'parameters',
  100. // icon: undefined,
  101. // to: '/parameters',
  102. // type: MENU_LINK_TYPE.INTERNAL,
  103. // active: false,
  104. // })
  105. })
  106. test('has only rights for menu place', () => {
  107. ability.can = vi.fn(
  108. (action: string, subject: string) =>
  109. action === 'display' && subject === 'place_page',
  110. )
  111. expect(menuBuilder.build()).toEqual({
  112. label: 'places',
  113. icon: undefined,
  114. to: 'https://mydomain.com/#/places/list/',
  115. type: MENU_LINK_TYPE.V1,
  116. active: false,
  117. })
  118. })
  119. test('has only rights for menu education', () => {
  120. ability.can = vi.fn(
  121. (action: string, subject: string) =>
  122. action === 'display' && subject === 'education_page',
  123. )
  124. expect(menuBuilder.build()).toEqual({
  125. label: 'education',
  126. icon: undefined,
  127. to: 'https://mydomain.com/#/educations/list/',
  128. type: MENU_LINK_TYPE.V1,
  129. active: false,
  130. })
  131. })
  132. test('has only rights for menu tag', () => {
  133. ability.can = vi.fn(
  134. (action: string, subject: string) =>
  135. action === 'display' && subject === 'tag_page',
  136. )
  137. expect(menuBuilder.build()).toEqual({
  138. label: 'tags',
  139. icon: undefined,
  140. to: 'https://mydomain.com/#/taggs/list/',
  141. type: MENU_LINK_TYPE.V1,
  142. active: false,
  143. })
  144. })
  145. test('has only rights for menu activities', () => {
  146. ability.can = vi.fn(
  147. (action: string, subject: string) =>
  148. action === 'display' && subject === 'activities_page',
  149. )
  150. expect(menuBuilder.build()).toEqual({
  151. label: 'activities',
  152. icon: undefined,
  153. to: 'https://mydomain.com/#/activities/list/',
  154. type: MENU_LINK_TYPE.V1,
  155. active: false,
  156. })
  157. })
  158. test('has only rights for menu course_duplication', () => {
  159. ability.can = vi.fn(
  160. (action: string, subject: string) =>
  161. action === 'display' && subject === 'course_duplication_page',
  162. )
  163. expect(menuBuilder.build()).toEqual({
  164. label: 'course_duplication',
  165. icon: undefined,
  166. to: 'https://mydomain.com/#/duplicate_courses',
  167. type: MENU_LINK_TYPE.V1,
  168. active: false,
  169. })
  170. })
  171. test('has only rights for menu import', () => {
  172. ability.can = vi.fn(
  173. (action: string, subject: string) =>
  174. action === 'display' && subject === 'import_page',
  175. )
  176. expect(menuBuilder.build()).toEqual({
  177. label: 'import',
  178. icon: undefined,
  179. to: 'https://mydomain.com/#/import/all',
  180. type: MENU_LINK_TYPE.V1,
  181. active: false,
  182. })
  183. })
  184. })