accessMenuBuilder.test.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { describe, test, expect, beforeEach, vi } 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 AccessMenuBuilder from '~/services/layout/menuBuilder/accessMenuBuilder'
  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: AccessMenuBuilder
  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 AccessMenuBuilder(
  24. runtimeConfig,
  25. ability,
  26. organizationProfile,
  27. accessProfile,
  28. router,
  29. )
  30. })
  31. describe('getMenuName', () => {
  32. test('validate name', () => {
  33. expect(menuBuilder.getMenuName()).toEqual('Access')
  34. })
  35. })
  36. describe('build', () => {
  37. test('has all items', () => {
  38. // @ts-ignore
  39. organizationProfile.isSchool = true
  40. ability.can = vi.fn(() => true)
  41. // Should return a MenuGroup
  42. const result = menuBuilder.build() as MenuGroup
  43. expect(result.label).toEqual('address_book')
  44. expect(result.icon).toEqual({ name: 'fas fa-address-book' })
  45. // @ts-ignore
  46. expect(result.children.length).toEqual(6)
  47. })
  48. test('has no items', () => {
  49. ability.can = vi.fn(() => false)
  50. expect(menuBuilder.build()).toEqual(null)
  51. })
  52. test('has only rights for menu person', () => {
  53. ability.can = vi.fn(
  54. (action: string, subject: string) =>
  55. action === 'display' && subject === 'accesses_page',
  56. )
  57. // @ts-ignore
  58. organizationProfile.isSchool = true
  59. expect(menuBuilder.build()).toEqual({
  60. label: 'person',
  61. icon: { name: 'fas fa-user' },
  62. to: 'https://mydomain.com/#/students/list/',
  63. type: MENU_LINK_TYPE.V1,
  64. active: false,
  65. })
  66. // @ts-ignore
  67. organizationProfile.isSchool = false
  68. expect(menuBuilder.build()).toEqual({
  69. label: 'person',
  70. icon: { name: 'fas fa-user' },
  71. to: 'https://mydomain.com/#/adherent/list/',
  72. type: MENU_LINK_TYPE.V1,
  73. active: false,
  74. })
  75. })
  76. test('has only rights for menu family_view', () => {
  77. ability.can = vi.fn(
  78. (action: string, subject: string) =>
  79. action === 'display' && subject === 'student_registration_page',
  80. )
  81. expect(menuBuilder.build()).toEqual({
  82. label: 'family_view',
  83. icon: { name: 'fas fa-users' },
  84. to: 'https://mydomain.com/#/student_registration/new',
  85. type: MENU_LINK_TYPE.V1,
  86. active: false,
  87. })
  88. })
  89. test('has only rights for menu education_student_next_year', () => {
  90. ability.can = vi.fn(
  91. (action: string, subject: string) =>
  92. action === 'display' && subject === 'education_student_next_year_page',
  93. )
  94. expect(menuBuilder.build()).toEqual({
  95. label: 'education_student_next_year',
  96. icon: { name: 'fas fa-list-alt' },
  97. to: 'https://mydomain.com/#/education_student_next_year/list/',
  98. type: MENU_LINK_TYPE.V1,
  99. active: false,
  100. })
  101. })
  102. test('has only rights for menu commissions_page', () => {
  103. ability.can = vi.fn(
  104. (action: string, subject: string) =>
  105. action === 'display' && subject === 'commissions_page',
  106. )
  107. expect(menuBuilder.build()).toEqual({
  108. label: 'commissions',
  109. icon: { name: 'fas fa-street-view' },
  110. to: 'https://mydomain.com/#/commissions/list/',
  111. type: MENU_LINK_TYPE.V1,
  112. active: false,
  113. })
  114. })
  115. test('has only rights for menu network_children_page', () => {
  116. ability.can = vi.fn(
  117. (action: string, subject: string) =>
  118. action === 'display' && subject === 'network_children_page',
  119. )
  120. expect(menuBuilder.build()).toEqual({
  121. label: 'network',
  122. icon: { name: 'fas fa-sitemap' },
  123. to: 'https://mydomain.com/#/networks/list/',
  124. type: MENU_LINK_TYPE.V1,
  125. active: false,
  126. })
  127. })
  128. test('has only rights for menu network_parents_page', () => {
  129. ability.can = vi.fn(
  130. (action: string, subject: string) =>
  131. action === 'display' && subject === 'network_parents_page',
  132. )
  133. expect(menuBuilder.build()).toEqual({
  134. label: 'my_network',
  135. icon: { name: 'fas fa-sitemap' },
  136. to: 'https://mydomain.com/#/network_artist_schools/list/',
  137. type: MENU_LINK_TYPE.V1,
  138. active: false,
  139. })
  140. })
  141. })