configurationMenuBuilder.test.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. router.resolve = vi.fn(() => {
  24. return 'some_route'
  25. })
  26. menuBuilder = new ConfigurationMenuBuilder(
  27. runtimeConfig,
  28. ability,
  29. organizationProfile,
  30. accessProfile,
  31. router,
  32. )
  33. })
  34. describe('getMenuName', () => {
  35. test('validate name', () => {
  36. expect(menuBuilder.getMenuName()).toEqual('Configuration')
  37. })
  38. })
  39. describe('build', () => {
  40. test('has all items', () => {
  41. ability.can = vi.fn(() => true)
  42. // Should return a MenuGroup
  43. const result = menuBuilder.build() as MenuGroup
  44. expect(result.label).toEqual('configuration')
  45. expect(result.icon).toEqual({ name: 'fas fa-cogs' })
  46. // @ts-ignore
  47. expect(result.children.length).toEqual(16)
  48. })
  49. test('has no items', () => {
  50. ability.can = vi.fn(() => false)
  51. expect(menuBuilder.build()).toEqual(null)
  52. })
  53. test('has only rights for menu organization_page', () => {
  54. ability.can = vi.fn(
  55. (action: string, subject: string) =>
  56. action === 'display' && subject === 'organization_page',
  57. )
  58. organizationProfile.id = 123
  59. expect(menuBuilder.build()).toEqual({
  60. label: 'organization_page',
  61. icon: undefined,
  62. to: 'https://mydomain.com/#/main/organizations/123/dashboard',
  63. target: '_self',
  64. type: MENU_LINK_TYPE.V1,
  65. active: false,
  66. })
  67. })
  68. test('has only rights for menu cmf_licence_generate', () => {
  69. ability.can = vi.fn(
  70. (action: string, subject: string) =>
  71. action === 'display' && subject === 'cmf_licence_page',
  72. )
  73. expect(menuBuilder.build()).toEqual({
  74. label: 'cmf_licence_generate',
  75. icon: undefined,
  76. to: 'https://mydomain.com/#/licence_cmf/organization',
  77. target: '_self',
  78. type: MENU_LINK_TYPE.V1,
  79. active: false,
  80. })
  81. //
  82. // expect(menuBuilder.build()).toEqual({
  83. // label: 'cmf_licence_generate',
  84. // icon: undefined,
  85. // to: '/cmf_licence_structure',
  86. // type: MENU_LINK_TYPE.INTERNAL,
  87. // active: false,
  88. // })
  89. })
  90. test('has only rights for menu parameters', () => {
  91. ability.can = vi.fn(
  92. (action: string, subject: string) =>
  93. action === 'display' && subject === 'parameters_page',
  94. )
  95. menuBuilder.organizationProfile.id = 123
  96. expect(menuBuilder.build()).toEqual({
  97. label: 'parameters_page',
  98. icon: undefined,
  99. to: undefined,
  100. type: MENU_LINK_TYPE.INTERNAL,
  101. active: false,
  102. })
  103. // expect(menuBuilder.build()).toEqual({
  104. // label: 'parameters',
  105. // icon: undefined,
  106. // to: '/parameters',
  107. // type: MENU_LINK_TYPE.INTERNAL,
  108. // active: false,
  109. // })
  110. })
  111. test('has only rights for menu place', () => {
  112. ability.can = vi.fn(
  113. (action: string, subject: string) =>
  114. action === 'display' && subject === 'place_page',
  115. )
  116. expect(menuBuilder.build()).toEqual({
  117. label: 'places',
  118. icon: undefined,
  119. to: 'https://mydomain.com/#/places/list/',
  120. target: '_self',
  121. type: MENU_LINK_TYPE.V1,
  122. active: false,
  123. })
  124. })
  125. test('has only rights for menu education', () => {
  126. ability.can = vi.fn(
  127. (action: string, subject: string) =>
  128. action === 'display' && subject === 'education_page',
  129. )
  130. expect(menuBuilder.build()).toEqual({
  131. label: 'education',
  132. icon: undefined,
  133. to: 'https://mydomain.com/#/educations/list/',
  134. target: '_self',
  135. type: MENU_LINK_TYPE.V1,
  136. active: false,
  137. })
  138. })
  139. test('has only rights for menu tag', () => {
  140. ability.can = vi.fn(
  141. (action: string, subject: string) =>
  142. action === 'display' && subject === 'tag_page',
  143. )
  144. expect(menuBuilder.build()).toEqual({
  145. label: 'tags',
  146. icon: undefined,
  147. to: 'https://mydomain.com/#/taggs/list/',
  148. target: '_self',
  149. type: MENU_LINK_TYPE.V1,
  150. active: false,
  151. })
  152. })
  153. test('has only rights for menu activities', () => {
  154. ability.can = vi.fn(
  155. (action: string, subject: string) =>
  156. action === 'display' && subject === 'activities_page',
  157. )
  158. expect(menuBuilder.build()).toEqual({
  159. label: 'activities',
  160. icon: undefined,
  161. to: 'https://mydomain.com/#/activities/list/',
  162. target: '_self',
  163. type: MENU_LINK_TYPE.V1,
  164. active: false,
  165. })
  166. })
  167. test('has only rights for menu course_duplication', () => {
  168. ability.can = vi.fn(
  169. (action: string, subject: string) =>
  170. action === 'display' && subject === 'course_duplication_page',
  171. )
  172. expect(menuBuilder.build()).toEqual({
  173. label: 'course_duplication',
  174. icon: undefined,
  175. to: 'https://mydomain.com/#/duplicate_courses',
  176. target: '_self',
  177. type: MENU_LINK_TYPE.V1,
  178. active: false,
  179. })
  180. })
  181. test('has only rights for menu import', () => {
  182. ability.can = vi.fn(
  183. (action: string, subject: string) =>
  184. action === 'display' && subject === 'import_page',
  185. )
  186. expect(menuBuilder.build()).toEqual({
  187. label: 'import',
  188. icon: undefined,
  189. to: 'https://mydomain.com/#/import/all',
  190. target: '_self',
  191. type: MENU_LINK_TYPE.V1,
  192. active: false,
  193. })
  194. })
  195. test('has only rights for menu parcours', () => {
  196. ability.can = vi.fn(
  197. (action: string, subject: string) =>
  198. action === 'display' && subject === 'parcours_page',
  199. )
  200. expect(menuBuilder.build()).toEqual({
  201. label: 'parcours',
  202. icon: undefined,
  203. to: 'https://mydomain.com/#/family_quotient_models/list/',
  204. target: '_self',
  205. type: MENU_LINK_TYPE.V1,
  206. active: false,
  207. })
  208. })
  209. test('has only rights for menu family_quotient_models', () => {
  210. ability.can = vi.fn(
  211. (action: string, subject: string) =>
  212. action === 'display' && subject === 'family_quotient_models_page',
  213. )
  214. expect(menuBuilder.build()).toEqual({
  215. label: 'family_quotient_models',
  216. icon: undefined,
  217. to: 'https://mydomain.com/#/family_quotient_models/list/',
  218. target: '_self',
  219. type: MENU_LINK_TYPE.V1,
  220. active: false,
  221. })
  222. })
  223. test('has only rights for menu billing_schedules', () => {
  224. ability.can = vi.fn(
  225. (action: string, subject: string) =>
  226. action === 'display' && subject === 'billing_schedules_settings_page',
  227. )
  228. expect(menuBuilder.build()).toEqual({
  229. label: 'billing_schedules',
  230. icon: undefined,
  231. to: 'https://mydomain.com/#/bill_schedules/list/',
  232. target: '_self',
  233. type: MENU_LINK_TYPE.V1,
  234. active: false,
  235. })
  236. })
  237. test('has only rights for menu pseudonymization', () => {
  238. ability.can = vi.fn(
  239. (action: string, subject: string) =>
  240. action === 'display' && subject === 'pseudonymization_page',
  241. )
  242. expect(menuBuilder.build()).toEqual({
  243. label: 'pseudonymization',
  244. icon: undefined,
  245. to: 'https://mydomain.com/#/pseudonymizationList/list/',
  246. target: '_self',
  247. type: MENU_LINK_TYPE.V1,
  248. active: false,
  249. })
  250. })
  251. })