accountMenuBuilder.test.ts 13 KB

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