accountMenuBuilder.test.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. import { describe, expect, test, 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 AccountMenuBuilder from '~/services/layout/menuBuilder/accountMenuBuilder'
  7. import type { MenuGroup } from '~/types/layout'
  8. import { MENU_LINK_TYPE } from '~/types/enum/layout'
  9. import { GENDER } from '~/types/enum/enums'
  10. let runtimeConfig: RuntimeConfig
  11. let ability: AnyAbility
  12. let organizationProfile: organizationState
  13. let accessProfile: AccessProfile
  14. let menuBuilder: AccountMenuBuilder
  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 any as AccessProfile
  23. runtimeConfig.baseUrlAdminLegacy = 'https://mydomain.com/'
  24. accessProfile.id = 123
  25. menuBuilder = new AccountMenuBuilder(
  26. runtimeConfig,
  27. ability,
  28. organizationProfile,
  29. accessProfile,
  30. router,
  31. )
  32. })
  33. describe('getMenuName', () => {
  34. test('validate name', () => {
  35. expect(menuBuilder.getMenuName()).toEqual('Account')
  36. })
  37. })
  38. describe('build', () => {
  39. test('has all items (mister)', () => {
  40. ability.can = vi.fn(() => true)
  41. // @ts-ignore
  42. router.resolve = vi.fn(() => 'foo')
  43. // Should return a MenuGroup
  44. const result = menuBuilder.build() as MenuGroup
  45. expect(result.label).toEqual('my_account')
  46. // @ts-ignore
  47. expect(result.children.length).toEqual(14)
  48. // @ts-ignore
  49. expect(result.actions.length).toEqual(1)
  50. // Has the logout action
  51. // @ts-ignore
  52. expect(result.actions).toEqual([
  53. {
  54. label: 'logout',
  55. icon: undefined,
  56. to: 'https://mydomain.com/#/logout',
  57. type: MENU_LINK_TYPE.V1,
  58. active: false,
  59. },
  60. ])
  61. })
  62. test('has profile icon : mister)', () => {
  63. ability.can = vi.fn(() => false)
  64. accessProfile.avatarId = 100
  65. accessProfile.gender = GENDER.MISTER
  66. const result = menuBuilder.build() as MenuGroup
  67. expect(result.icon).toEqual({
  68. avatarId: 100,
  69. avatarByDefault: '/images/default/men-1.png',
  70. })
  71. })
  72. test('has profile icon : miss)', () => {
  73. ability.can = vi.fn(() => false)
  74. accessProfile.avatarId = 100
  75. accessProfile.gender = GENDER.MISS
  76. const result = menuBuilder.build() as MenuGroup
  77. expect(result.icon).toEqual({
  78. avatarId: 100,
  79. avatarByDefault: '/images/default/women-1.png',
  80. })
  81. })
  82. test('has no items', () => {
  83. ability.can = vi.fn(() => false)
  84. const group = menuBuilder.build()
  85. // AccountMenuBuilder retourne toujours un groupe
  86. // @ts-ignore
  87. expect(group.children).toEqual([])
  88. // Still has the logout action
  89. // @ts-ignore
  90. expect(group.actions).toEqual([
  91. {
  92. label: 'logout',
  93. icon: undefined,
  94. to: 'https://mydomain.com/#/logout',
  95. type: MENU_LINK_TYPE.V1,
  96. active: false,
  97. },
  98. ])
  99. })
  100. test('has only rights for menu my_schedule_page', () => {
  101. ability.can = vi.fn(
  102. (action: string, subject: string) =>
  103. action === 'display' && subject === 'my_schedule_page',
  104. )
  105. // @ts-ignore
  106. expect(menuBuilder.build().children[0]).toEqual({
  107. label: 'my_schedule_page',
  108. icon: undefined,
  109. to: 'https://mydomain.com/#/my_calendar',
  110. type: MENU_LINK_TYPE.V1,
  111. active: false,
  112. })
  113. })
  114. test('has only rights for menu attendance_bookings_menu', () => {
  115. ability.can = vi.fn(
  116. (action: string, subject: string) =>
  117. action === 'display' && subject === 'attendance_bookings_page',
  118. )
  119. // @ts-ignore
  120. expect(menuBuilder.build().children[0]).toEqual({
  121. label: 'attendance_bookings_menu',
  122. icon: undefined,
  123. to: 'https://mydomain.com/#/own_attendance',
  124. type: MENU_LINK_TYPE.V1,
  125. active: false,
  126. })
  127. })
  128. test('has only rights for menu my_attendance', () => {
  129. ability.can = vi.fn(
  130. (action: string, subject: string) =>
  131. action === 'display' && subject === 'my_attendance_page',
  132. )
  133. // @ts-ignore
  134. expect(menuBuilder.build().children[0]).toEqual({
  135. label: 'my_attendance',
  136. icon: undefined,
  137. to: 'https://mydomain.com/#/my_attendances/list/',
  138. type: MENU_LINK_TYPE.V1,
  139. active: false,
  140. })
  141. })
  142. test('has only rights for menu my_invitation', () => {
  143. ability.can = vi.fn(
  144. (action: string, subject: string) =>
  145. action === 'display' && subject === 'my_invitation_page',
  146. )
  147. // @ts-ignore
  148. expect(menuBuilder.build().children[0]).toEqual({
  149. label: 'my_invitation',
  150. icon: undefined,
  151. to: 'https://mydomain.com/#/my_invitations/list/',
  152. type: MENU_LINK_TYPE.V1,
  153. active: false,
  154. })
  155. })
  156. test('has only rights for menu my_students', () => {
  157. ability.can = vi.fn(
  158. (action: string, subject: string) =>
  159. action === 'display' && subject === 'my_students_page',
  160. )
  161. // @ts-ignore
  162. expect(menuBuilder.build().children[0]).toEqual({
  163. label: 'my_students',
  164. icon: undefined,
  165. to: 'https://mydomain.com/#/my_students/list/',
  166. type: MENU_LINK_TYPE.V1,
  167. active: false,
  168. })
  169. })
  170. test('has only rights for menu my_students_education_students', () => {
  171. ability.can = vi.fn(
  172. (action: string, subject: string) =>
  173. action === 'display' &&
  174. subject === 'my_students_education_students_page',
  175. )
  176. // @ts-ignore
  177. expect(menuBuilder.build().children[0]).toEqual({
  178. label: 'my_students_education_students',
  179. icon: undefined,
  180. to: 'https://mydomain.com/#/my_students_education_students/list/',
  181. type: MENU_LINK_TYPE.V1,
  182. active: false,
  183. })
  184. })
  185. test('has only rights for menu my_education_students', () => {
  186. ability.can = vi.fn(
  187. (action: string, subject: string) =>
  188. action === 'display' && subject === 'my_education_students_page',
  189. )
  190. // @ts-ignore
  191. expect(menuBuilder.build().children[0]).toEqual({
  192. label: 'my_education_students',
  193. icon: undefined,
  194. to: 'https://mydomain.com/#/main/my_profile/123/dashboard/my_education_students/list/',
  195. type: MENU_LINK_TYPE.V1,
  196. active: false,
  197. })
  198. })
  199. test('has only rights for menu send_an_email', () => {
  200. ability.can = vi.fn(
  201. (action: string, subject: string) =>
  202. action === 'display' && subject === 'send_an_email_page',
  203. )
  204. // @ts-ignore
  205. expect(menuBuilder.build().children[0]).toEqual({
  206. label: 'send_an_email',
  207. icon: undefined,
  208. to: 'https://mydomain.com/#/list/create/emails',
  209. type: MENU_LINK_TYPE.V1,
  210. active: false,
  211. })
  212. })
  213. test('has only rights for menu my_documents', () => {
  214. ability.can = vi.fn(
  215. (action: string, subject: string) =>
  216. action === 'display' && subject === 'my_documents_page',
  217. )
  218. // @ts-ignore
  219. expect(menuBuilder.build().children[0]).toEqual({
  220. label: 'my_documents',
  221. icon: undefined,
  222. to: 'https://mydomain.com/#/main/my_profile/123/dashboard/show/my_access_file',
  223. type: MENU_LINK_TYPE.V1,
  224. active: false,
  225. })
  226. })
  227. test('has only rights for menu my_profile', () => {
  228. ability.can = vi.fn(
  229. (action: string, subject: string) =>
  230. action === 'display' && subject === 'my_profile_page',
  231. )
  232. // @ts-ignore
  233. expect(menuBuilder.build().children[0]).toEqual({
  234. label: 'my_profile',
  235. icon: undefined,
  236. to: 'https://mydomain.com/#/main/my_profile/123/dashboard',
  237. type: MENU_LINK_TYPE.V1,
  238. active: false,
  239. })
  240. })
  241. test('has only rights for menu adherent_list', () => {
  242. ability.can = vi.fn(
  243. (action: string, subject: string) =>
  244. action === 'display' && subject === 'adherent_list_page',
  245. )
  246. // @ts-ignore
  247. expect(menuBuilder.build().children[0]).toEqual({
  248. label: 'adherent_list',
  249. icon: undefined,
  250. to: 'https://mydomain.com/#/adherent_contacts/list/',
  251. type: MENU_LINK_TYPE.V1,
  252. active: false,
  253. })
  254. })
  255. test('has only rights for menu my_bills', () => {
  256. ability.can = vi.fn(
  257. (action: string, subject: string) =>
  258. action === 'display' && subject === 'my_bills_page',
  259. )
  260. // @ts-ignore
  261. expect(menuBuilder.build().children[0]).toEqual({
  262. label: 'my_bills',
  263. icon: undefined,
  264. to: 'https://mydomain.com/#/main/my_profile/123/dashboard/show/my_bills',
  265. type: MENU_LINK_TYPE.V1,
  266. active: false,
  267. })
  268. })
  269. test('has only rights for menu print_my_licence', () => {
  270. ability.can = vi.fn(
  271. (action: string, subject: string) =>
  272. action === 'display' && subject === 'cmf_licence_person_page',
  273. )
  274. // @ts-ignore
  275. expect(menuBuilder.build().children[0]).toEqual({
  276. label: 'print_my_licence',
  277. icon: undefined,
  278. to: 'https://mydomain.com/#/licence_cmf/user',
  279. type: MENU_LINK_TYPE.V1,
  280. active: false,
  281. })
  282. })
  283. })