configurationMenuBuilder.test.ts 6.0 KB

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