accessMenuBuilder.test.ts 4.5 KB

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