accountMenuBuilder.test.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import {describe, expect, test} from 'vitest'
  2. import {RuntimeConfig} from "@nuxt/schema";
  3. import {AnyAbility} from "@casl/ability/dist/types";
  4. import {AccessProfile, organizationState} from "~/types/interfaces";
  5. import AccountMenuBuilder from "~/services/layout/menuBuilder/accountMenuBuilder";
  6. import {MenuGroup} from "~/types/layout";
  7. import {MENU_LINK_TYPE} from "~/types/enum/layout";
  8. import {GENDER} from "~/types/enum/enums";
  9. let runtimeConfig: RuntimeConfig
  10. let ability: AnyAbility
  11. let organizationProfile: organizationState
  12. let accessProfile: AccessProfile
  13. let menuBuilder: AccountMenuBuilder
  14. beforeEach(()=> {
  15. runtimeConfig = vi.fn() as any as RuntimeConfig
  16. ability = vi.fn() as any as AnyAbility
  17. organizationProfile = vi.fn() as any as organizationState
  18. accessProfile = vi.fn() as any as AccessProfile
  19. runtimeConfig.baseUrlAdminLegacy = 'https://mydomain.com/'
  20. accessProfile.id = 123
  21. menuBuilder = new AccountMenuBuilder(runtimeConfig, ability, organizationProfile, accessProfile)
  22. })
  23. describe('getMenuName', () => {
  24. test('validate name', () => {
  25. expect(menuBuilder.getMenuName()).toEqual("Account")
  26. })
  27. })
  28. describe('build', () => {
  29. test('has all items (mister)', () => {
  30. ability.can = vi.fn(() => true)
  31. // Should return a MenuGroup
  32. const result = menuBuilder.build() as MenuGroup
  33. expect(result.label).toEqual('my_account')
  34. // @ts-ignore
  35. expect(result.children.length).toEqual(15)
  36. // @ts-ignore
  37. expect(result.actions.length).toEqual(1)
  38. // Has the logout action
  39. // @ts-ignore
  40. expect(result.actions).toEqual([
  41. {label: 'logout', icon: undefined, to: 'https://mydomain.com/#/logout', type: MENU_LINK_TYPE.V1, active: false}
  42. ])
  43. })
  44. test('has profile icon : mister)', () => {
  45. ability.can = vi.fn(() => false)
  46. accessProfile.avatarId = 100
  47. accessProfile.gender = GENDER.MISTER
  48. const result = menuBuilder.build() as MenuGroup
  49. expect(result.icon).toEqual({avatarId: 100, avatarByDefault: '/images/default/men-1.png'})
  50. })
  51. test('has profile icon : miss)', () => {
  52. ability.can = vi.fn(() => false)
  53. accessProfile.avatarId = 100
  54. accessProfile.gender = GENDER.MISS
  55. const result = menuBuilder.build() as MenuGroup
  56. expect(result.icon).toEqual({avatarId: 100, avatarByDefault: '/images/default/women-1.png'})
  57. })
  58. test('has no items', () => {
  59. ability.can = vi.fn(() => false)
  60. const group = menuBuilder.build()
  61. // AccountMenuBuilder retourne toujours un groupe
  62. // @ts-ignore
  63. expect(group.children).toEqual([])
  64. // Still has the logout action
  65. // @ts-ignore
  66. expect(group.actions).toEqual([
  67. {label: 'logout', icon: undefined, to: 'https://mydomain.com/#/logout', type: MENU_LINK_TYPE.V1, active: false}
  68. ])
  69. })
  70. test('has only rights for menu my_schedule_page', () => {
  71. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'my_schedule_page')
  72. // @ts-ignore
  73. expect(menuBuilder.build().children[0]).toEqual(
  74. {label: 'my_schedule_page', icon: undefined, to: 'https://mydomain.com/#/my_calendar', type: MENU_LINK_TYPE.V1, active: false}
  75. )
  76. })
  77. test('has only rights for menu attendance_bookings_menu', () => {
  78. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'attendance_bookings_page')
  79. // @ts-ignore
  80. expect(menuBuilder.build().children[0]).toEqual(
  81. {label: 'attendance_bookings_menu', icon: undefined, to: 'https://mydomain.com/#/own_attendance', type: MENU_LINK_TYPE.V1, active: false}
  82. )
  83. })
  84. test('has only rights for menu my_attendance', () => {
  85. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'my_attendance_page')
  86. // @ts-ignore
  87. expect(menuBuilder.build().children[0]).toEqual(
  88. {label: 'my_attendance', icon: undefined, to: 'https://mydomain.com/#/my_attendances/list/', type: MENU_LINK_TYPE.V1, active: false}
  89. )
  90. })
  91. test('has only rights for menu my_invitation', () => {
  92. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'my_invitation_page')
  93. // @ts-ignore
  94. expect(menuBuilder.build().children[0]).toEqual(
  95. {label: 'my_invitation', icon: undefined, to: 'https://mydomain.com/#/my_invitations/list/', type: MENU_LINK_TYPE.V1, active: false}
  96. )
  97. })
  98. test('has only rights for menu my_students', () => {
  99. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'my_students_page')
  100. // @ts-ignore
  101. expect(menuBuilder.build().children[0]).toEqual(
  102. {label: 'my_students', icon: undefined, to: 'https://mydomain.com/#/my_students/list/', type: MENU_LINK_TYPE.V1, active: false}
  103. )
  104. })
  105. test('has only rights for menu my_students_education_students', () => {
  106. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'my_students_education_students_page')
  107. // @ts-ignore
  108. expect(menuBuilder.build().children[0]).toEqual(
  109. {label: 'my_students_education_students', icon: undefined, to: 'https://mydomain.com/#/my_students_education_students/list/', type: MENU_LINK_TYPE.V1, active: false}
  110. )
  111. })
  112. test('has only rights for menu my_education_students', () => {
  113. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'my_education_students_page')
  114. // @ts-ignore
  115. expect(menuBuilder.build().children[0]).toEqual(
  116. {label: 'my_education_students', icon: undefined, to: 'https://mydomain.com/#/main/my_profile/123/dashboard/my_education_students/list/', type: MENU_LINK_TYPE.V1, active: false}
  117. )
  118. })
  119. test('has only rights for menu send_an_email', () => {
  120. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'send_an_email_page')
  121. // @ts-ignore
  122. expect(menuBuilder.build().children[0]).toEqual(
  123. {label: 'send_an_email', icon: undefined, to: 'https://mydomain.com/#/list/create/emails', type: MENU_LINK_TYPE.V1, active: false}
  124. )
  125. })
  126. test('has only rights for menu my_documents', () => {
  127. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'my_documents_page')
  128. // @ts-ignore
  129. expect(menuBuilder.build().children[0]).toEqual(
  130. {label: 'my_documents', icon: undefined, to: 'https://mydomain.com/#/main/my_profile/123/dashboard/show/my_access_file', type: MENU_LINK_TYPE.V1, active: false}
  131. )
  132. })
  133. test('has only rights for menu my_profile', () => {
  134. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'my_profile_page')
  135. // @ts-ignore
  136. expect(menuBuilder.build().children[0]).toEqual(
  137. {label: 'my_profile', icon: undefined, to: 'https://mydomain.com/#/main/my_profile/123/dashboard', type: MENU_LINK_TYPE.V1, active: false}
  138. )
  139. })
  140. test('has only rights for menu adherent_list', () => {
  141. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'adherent_list_page')
  142. // @ts-ignore
  143. expect(menuBuilder.build().children[0]).toEqual(
  144. {label: 'adherent_list', icon: undefined, to: 'https://mydomain.com/#/adherent_contacts/list/', type: MENU_LINK_TYPE.V1, active: false}
  145. )
  146. })
  147. test('has only rights for menu my_bills', () => {
  148. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'my_bills_page')
  149. // @ts-ignore
  150. expect(menuBuilder.build().children[0]).toEqual(
  151. {label: 'my_bills', icon: undefined, to: 'https://mydomain.com/#/main/my_profile/123/dashboard/show/my_bills', type: MENU_LINK_TYPE.V1, active: false}
  152. )
  153. })
  154. test('has only rights for menu print_my_licence', () => {
  155. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'cmf_licence_person_page')
  156. // @ts-ignore
  157. expect(menuBuilder.build().children[0]).toEqual(
  158. {label: 'print_my_licence', icon: undefined, to: 'https://mydomain.com/#/licence_cmf/user', type: MENU_LINK_TYPE.V1, active: false}
  159. )
  160. })
  161. })