accessMenuBuilder.test.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import { describe, test, expect, beforeEach, vi } from 'vitest'
  2. import type { RuntimeConfig } from '@nuxt/schema'
  3. //@ts-expect-error false error
  4. import type { AnyAbility } from '@casl/ability/dist/types'
  5. import type { Router } from 'vue-router'
  6. import type { AccessProfile, organizationState } from '~/types/interfaces'
  7. import AccessMenuBuilder from '~/services/layout/menuBuilder/accessMenuBuilder'
  8. import type { MenuGroup } from '~/types/layout'
  9. import { MENU_LINK_TYPE } from '~/types/enum/layout'
  10. let runtimeConfig: RuntimeConfig
  11. let ability: AnyAbility
  12. let organizationProfile: organizationState
  13. let accessProfile: AccessProfile
  14. let menuBuilder: AccessMenuBuilder
  15. let router: Router
  16. beforeEach(() => {
  17. runtimeConfig = vi.fn() as any as RuntimeConfig
  18. ability = vi.fn() as any as AnyAbility
  19. organizationProfile = vi.fn() as any as organizationState
  20. accessProfile = vi.fn() as any as AccessProfile
  21. // @ts-ignore
  22. router = vi.fn() as Router
  23. runtimeConfig.baseUrlAdminLegacy = 'https://mydomain.com/'
  24. menuBuilder = new AccessMenuBuilder(
  25. runtimeConfig,
  26. ability,
  27. organizationProfile,
  28. accessProfile,
  29. router,
  30. )
  31. })
  32. describe('getMenuName', () => {
  33. test('validate name', () => {
  34. expect(menuBuilder.getMenuName()).toEqual('Access')
  35. })
  36. })
  37. describe('build', () => {
  38. test('has all items', () => {
  39. // @ts-ignore
  40. organizationProfile.isSchool = true
  41. ability.can = vi.fn(() => true)
  42. // Should return a MenuGroup
  43. const result = menuBuilder.build() as MenuGroup
  44. expect(result.label).toEqual('address_book')
  45. expect(result.icon).toEqual({ name: 'fas fa-address-book' })
  46. // @ts-ignore
  47. expect(result.children.length).toEqual(6)
  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 person', () => {
  54. ability.can = vi.fn(
  55. (action: string, subject: string) =>
  56. action === 'display' && subject === 'accesses_page',
  57. )
  58. // @ts-ignore
  59. organizationProfile.isSchool = true
  60. expect(menuBuilder.build()).toEqual({
  61. label: 'person',
  62. icon: { name: 'fas fa-user' },
  63. to: 'https://mydomain.com/#/students/list/',
  64. target: '_self',
  65. type: MENU_LINK_TYPE.V1,
  66. active: false,
  67. endOfSubsection: false,
  68. })
  69. // @ts-ignore
  70. organizationProfile.isSchool = false
  71. expect(menuBuilder.build()).toEqual({
  72. label: 'person',
  73. icon: { name: 'fas fa-user' },
  74. to: 'https://mydomain.com/#/adherent/list/',
  75. target: '_self',
  76. type: MENU_LINK_TYPE.V1,
  77. active: false,
  78. endOfSubsection: false,
  79. })
  80. })
  81. test('has only rights for menu family_view', () => {
  82. ability.can = vi.fn(
  83. (action: string, subject: string) =>
  84. action === 'display' && subject === 'student_registration_page',
  85. )
  86. expect(menuBuilder.build()).toEqual({
  87. label: 'family_view',
  88. icon: { name: 'fas fa-users' },
  89. to: 'https://mydomain.com/#/student_registration/new',
  90. target: '_self',
  91. type: MENU_LINK_TYPE.V1,
  92. active: false,
  93. endOfSubsection: false,
  94. })
  95. })
  96. test('has only rights for menu education_student_next_year', () => {
  97. ability.can = vi.fn(
  98. (action: string, subject: string) =>
  99. action === 'display' && subject === 'education_student_next_year_page',
  100. )
  101. expect(menuBuilder.build()).toEqual({
  102. label: 'education_student_next_year',
  103. icon: { name: 'fas fa-list-alt' },
  104. to: 'https://mydomain.com/#/education_student_next_year/list/',
  105. target: '_self',
  106. type: MENU_LINK_TYPE.V1,
  107. active: false,
  108. endOfSubsection: false,
  109. })
  110. })
  111. test('has only rights for menu commissions_page', () => {
  112. ability.can = vi.fn(
  113. (action: string, subject: string) =>
  114. action === 'display' && subject === 'commissions_page',
  115. )
  116. expect(menuBuilder.build()).toEqual({
  117. label: 'commissions',
  118. icon: { name: 'fas fa-street-view' },
  119. to: 'https://mydomain.com/#/commissions/list/',
  120. target: '_self',
  121. type: MENU_LINK_TYPE.V1,
  122. active: false,
  123. endOfSubsection: false,
  124. })
  125. })
  126. test('has only rights for menu network_children_page', () => {
  127. ability.can = vi.fn(
  128. (action: string, subject: string) =>
  129. action === 'display' && subject === 'network_children_page',
  130. )
  131. expect(menuBuilder.build()).toEqual({
  132. label: 'network',
  133. icon: { name: 'fas fa-sitemap' },
  134. to: 'https://mydomain.com/#/networks/list/',
  135. target: '_self',
  136. type: MENU_LINK_TYPE.V1,
  137. active: false,
  138. endOfSubsection: false,
  139. })
  140. })
  141. test('has only rights for menu network_parents_page', () => {
  142. ability.can = vi.fn(
  143. (action: string, subject: string) =>
  144. action === 'display' && subject === 'network_parents_page',
  145. )
  146. expect(menuBuilder.build()).toEqual({
  147. label: 'my_network',
  148. icon: { name: 'fas fa-sitemap' },
  149. to: 'https://mydomain.com/#/network_artist_schools/list/',
  150. target: '_self',
  151. type: MENU_LINK_TYPE.V1,
  152. active: false,
  153. endOfSubsection: false,
  154. })
  155. })
  156. })