accountMenuBuilder.test.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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(() => {
  43. return { href: 'foo ' }
  44. })
  45. // Should return a MenuGroup
  46. const result = menuBuilder.build() as MenuGroup
  47. expect(result.label).toEqual('my_account')
  48. // @ts-ignore
  49. expect(result.children.length).toEqual(16)
  50. // @ts-ignore
  51. expect(result.actions.length).toEqual(1)
  52. // Has the logout action
  53. // @ts-ignore
  54. const logoutAction = result.actions[0]
  55. expect(logoutAction).toHaveProperty('label', 'logout')
  56. expect(logoutAction).toHaveProperty('icon', undefined)
  57. expect(logoutAction).toHaveProperty('to', 'https://mydomain.com/#/logout')
  58. expect(logoutAction).toHaveProperty('target', '_self')
  59. expect(logoutAction).toHaveProperty('type', MENU_LINK_TYPE.V1)
  60. expect(logoutAction).toHaveProperty('active', false)
  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.length).toEqual(1)
  91. // @ts-ignore
  92. const logoutAction = group.actions[0]
  93. expect(logoutAction).toHaveProperty('label', 'logout')
  94. expect(logoutAction).toHaveProperty('icon', undefined)
  95. expect(logoutAction).toHaveProperty('to', 'https://mydomain.com/#/logout')
  96. expect(logoutAction).toHaveProperty('target', '_self')
  97. expect(logoutAction).toHaveProperty('type', MENU_LINK_TYPE.V1)
  98. expect(logoutAction).toHaveProperty('active', false)
  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. const menuItem = menuBuilder.build().children[0]
  107. expect(menuItem).toHaveProperty('label', 'my_schedule_page')
  108. expect(menuItem).toHaveProperty('icon', undefined)
  109. expect(menuItem).toHaveProperty('to', 'https://mydomain.com/#/my_calendar')
  110. expect(menuItem).toHaveProperty('target', '_self')
  111. expect(menuItem).toHaveProperty('type', MENU_LINK_TYPE.V1)
  112. expect(menuItem).toHaveProperty('active', false)
  113. expect(menuItem).toHaveProperty('endOfSubsection', false)
  114. })
  115. test('has only rights for menu attendance_bookings_menu', () => {
  116. ability.can = vi.fn(
  117. (action: string, subject: string) =>
  118. action === 'display' && subject === 'attendance_bookings_page',
  119. )
  120. // @ts-ignore
  121. const menuItem = menuBuilder.build().children[0]
  122. expect(menuItem).toHaveProperty('label', 'attendance_bookings_menu')
  123. expect(menuItem).toHaveProperty('icon', undefined)
  124. expect(menuItem).toHaveProperty(
  125. 'to',
  126. 'https://mydomain.com/#/own_attendance',
  127. )
  128. expect(menuItem).toHaveProperty('target', '_self')
  129. expect(menuItem).toHaveProperty('type', MENU_LINK_TYPE.V1)
  130. expect(menuItem).toHaveProperty('active', false)
  131. expect(menuItem).toHaveProperty('endOfSubsection', false)
  132. })
  133. test('has only rights for menu my_attendance', () => {
  134. ability.can = vi.fn(
  135. (action: string, subject: string) =>
  136. action === 'display' && subject === 'my_attendance_page',
  137. )
  138. // @ts-ignore
  139. const menuItem = menuBuilder.build().children[0]
  140. expect(menuItem).toHaveProperty('label', 'my_attendance')
  141. expect(menuItem).toHaveProperty('icon', undefined)
  142. expect(menuItem).toHaveProperty(
  143. 'to',
  144. 'https://mydomain.com/#/my_attendances/list/',
  145. )
  146. expect(menuItem).toHaveProperty('target', '_self')
  147. expect(menuItem).toHaveProperty('type', MENU_LINK_TYPE.V1)
  148. expect(menuItem).toHaveProperty('active', false)
  149. expect(menuItem).toHaveProperty('endOfSubsection', false)
  150. })
  151. test('has only rights for menu my_invitation', () => {
  152. ability.can = vi.fn(
  153. (action: string, subject: string) =>
  154. action === 'display' && subject === 'my_invitation_page',
  155. )
  156. // @ts-ignore
  157. const menuItem = menuBuilder.build().children[0]
  158. expect(menuItem).toHaveProperty('label', 'my_invitation')
  159. expect(menuItem).toHaveProperty('icon', undefined)
  160. expect(menuItem).toHaveProperty(
  161. 'to',
  162. 'https://mydomain.com/#/my_invitations/list/',
  163. )
  164. expect(menuItem).toHaveProperty('target', '_self')
  165. expect(menuItem).toHaveProperty('type', MENU_LINK_TYPE.V1)
  166. expect(menuItem).toHaveProperty('active', false)
  167. expect(menuItem).toHaveProperty('endOfSubsection', false)
  168. })
  169. test('has only rights for menu my_students', () => {
  170. ability.can = vi.fn(
  171. (action: string, subject: string) =>
  172. action === 'display' && subject === 'my_students_page',
  173. )
  174. // @ts-ignore
  175. const menuItem = menuBuilder.build().children[0]
  176. expect(menuItem).toHaveProperty('label', 'my_students')
  177. expect(menuItem).toHaveProperty('icon', undefined)
  178. expect(menuItem).toHaveProperty(
  179. 'to',
  180. 'https://mydomain.com/#/my_students/list/',
  181. )
  182. expect(menuItem).toHaveProperty('target', '_self')
  183. expect(menuItem).toHaveProperty('type', MENU_LINK_TYPE.V1)
  184. expect(menuItem).toHaveProperty('active', false)
  185. expect(menuItem).toHaveProperty('endOfSubsection', false)
  186. })
  187. test('has only rights for menu my_students_education_students', () => {
  188. ability.can = vi.fn(
  189. (action: string, subject: string) =>
  190. action === 'display' &&
  191. subject === 'my_students_education_students_page',
  192. )
  193. // @ts-ignore
  194. const menuItem = menuBuilder.build().children[0]
  195. expect(menuItem).toHaveProperty('label', 'my_students_education_students')
  196. expect(menuItem).toHaveProperty('icon', undefined)
  197. expect(menuItem).toHaveProperty(
  198. 'to',
  199. 'https://mydomain.com/#/my_students_education_students/list/',
  200. )
  201. expect(menuItem).toHaveProperty('target', '_self')
  202. expect(menuItem).toHaveProperty('type', MENU_LINK_TYPE.V1)
  203. expect(menuItem).toHaveProperty('active', false)
  204. expect(menuItem).toHaveProperty('endOfSubsection', false)
  205. })
  206. test('has only rights for menu my_education_students', () => {
  207. ability.can = vi.fn(
  208. (action: string, subject: string) =>
  209. action === 'display' && subject === 'my_education_students_page',
  210. )
  211. // @ts-ignore
  212. const menuItem = menuBuilder.build().children[0]
  213. expect(menuItem).toHaveProperty('label', 'my_education_students')
  214. expect(menuItem).toHaveProperty('icon', undefined)
  215. expect(menuItem).toHaveProperty(
  216. 'to',
  217. 'https://mydomain.com/#/main/my_profile/123/dashboard/my_education_students/list/',
  218. )
  219. expect(menuItem).toHaveProperty('target', '_self')
  220. expect(menuItem).toHaveProperty('type', MENU_LINK_TYPE.V1)
  221. expect(menuItem).toHaveProperty('active', false)
  222. expect(menuItem).toHaveProperty('endOfSubsection', false)
  223. })
  224. test('has only rights for menu send_an_email', () => {
  225. ability.can = vi.fn(
  226. (action: string, subject: string) =>
  227. action === 'display' && subject === 'send_an_email_page',
  228. )
  229. // @ts-ignore
  230. const menuItem = menuBuilder.build().children[0]
  231. expect(menuItem).toHaveProperty('label', 'send_an_email')
  232. expect(menuItem).toHaveProperty('icon', undefined)
  233. expect(menuItem).toHaveProperty(
  234. 'to',
  235. 'https://mydomain.com/#/list/create/emails',
  236. )
  237. expect(menuItem).toHaveProperty('target', '_self')
  238. expect(menuItem).toHaveProperty('type', MENU_LINK_TYPE.V1)
  239. expect(menuItem).toHaveProperty('active', false)
  240. expect(menuItem).toHaveProperty('endOfSubsection', false)
  241. })
  242. test('has only rights for menu my_documents', () => {
  243. ability.can = vi.fn(
  244. (action: string, subject: string) =>
  245. action === 'display' && subject === 'my_documents_page',
  246. )
  247. // @ts-ignore
  248. const menuItem = menuBuilder.build().children[0]
  249. expect(menuItem).toHaveProperty('label', 'my_documents')
  250. expect(menuItem).toHaveProperty('icon', undefined)
  251. expect(menuItem).toHaveProperty(
  252. 'to',
  253. 'https://mydomain.com/#/main/my_profile/123/dashboard/show/my_access_file',
  254. )
  255. expect(menuItem).toHaveProperty('target', '_self')
  256. expect(menuItem).toHaveProperty('type', MENU_LINK_TYPE.V1)
  257. expect(menuItem).toHaveProperty('active', false)
  258. expect(menuItem).toHaveProperty('endOfSubsection', false)
  259. })
  260. test('has only rights for menu my_profile', () => {
  261. ability.can = vi.fn(
  262. (action: string, subject: string) =>
  263. action === 'display' && subject === 'my_profile_page',
  264. )
  265. // @ts-ignore
  266. expect(menuBuilder.build().children[0]).toEqual({
  267. label: 'my_profile',
  268. icon: undefined,
  269. to: 'https://mydomain.com/#/main/my_profile/123/dashboard',
  270. target: '_self',
  271. type: MENU_LINK_TYPE.V1,
  272. active: false,
  273. endOfSubsection: false,
  274. })
  275. })
  276. test('has only rights for menu adherent_list', () => {
  277. ability.can = vi.fn(
  278. (action: string, subject: string) =>
  279. action === 'display' && subject === 'adherent_list_page',
  280. )
  281. // @ts-ignore
  282. expect(menuBuilder.build().children[0]).toEqual({
  283. label: 'adherent_list',
  284. icon: undefined,
  285. to: 'https://mydomain.com/#/adherent_contacts/list/',
  286. target: '_self',
  287. type: MENU_LINK_TYPE.V1,
  288. active: false,
  289. endOfSubsection: true,
  290. })
  291. })
  292. test('has only rights for menu subscription', () => {
  293. ability.can = vi.fn(
  294. (action: string, subject: string) =>
  295. action === 'display' && subject === 'subscription_page',
  296. )
  297. // @ts-ignore
  298. router.resolve = vi.fn(() => {
  299. return { href: 'subscription' }
  300. })
  301. // @ts-ignore
  302. expect(menuBuilder.build().children[0]).toEqual({
  303. label: 'subscription_page',
  304. icon: undefined,
  305. to: 'subscription',
  306. type: MENU_LINK_TYPE.INTERNAL,
  307. active: false,
  308. endOfSubsection: true,
  309. })
  310. expect(router.resolve).toHaveBeenCalledWith({ name: 'subscription_page' })
  311. })
  312. test('has only rights for menu my_bills', () => {
  313. ability.can = vi.fn(
  314. (action: string, subject: string) =>
  315. action === 'display' && subject === 'my_bills_page',
  316. )
  317. // @ts-ignore
  318. expect(menuBuilder.build().children[0]).toEqual({
  319. label: 'my_bills',
  320. icon: undefined,
  321. to: 'https://mydomain.com/#/main/my_profile/123/dashboard/show/my_bills',
  322. target: '_self',
  323. type: MENU_LINK_TYPE.V1,
  324. active: false,
  325. endOfSubsection: false,
  326. })
  327. })
  328. test('has only rights for menu print_my_licence', () => {
  329. ability.can = vi.fn(
  330. (action: string, subject: string) =>
  331. action === 'display' && subject === 'cmf_licence_person_page',
  332. )
  333. // @ts-ignore
  334. expect(menuBuilder.build().children[0]).toEqual({
  335. label: 'print_my_licence',
  336. icon: undefined,
  337. to: 'https://mydomain.com/#/licence_cmf/user',
  338. target: '_self',
  339. type: MENU_LINK_TYPE.V1,
  340. active: false,
  341. endOfSubsection: false,
  342. })
  343. })
  344. })