accountMenuBuilder.test.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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(15)
  50. // @ts-ignore
  51. expect(result.actions.length).toEqual(1)
  52. // Has the logout action
  53. // @ts-ignore
  54. expect(result.actions).toEqual([
  55. {
  56. label: 'logout',
  57. icon: undefined,
  58. to: 'https://mydomain.com/#/logout',
  59. target: '_self',
  60. type: MENU_LINK_TYPE.V1,
  61. active: false,
  62. },
  63. ])
  64. })
  65. test('has profile icon : mister)', () => {
  66. ability.can = vi.fn(() => false)
  67. accessProfile.avatarId = 100
  68. accessProfile.gender = GENDER.MISTER
  69. const result = menuBuilder.build() as MenuGroup
  70. expect(result.icon).toEqual({
  71. avatarId: 100,
  72. avatarByDefault: '/images/default/men-1.png',
  73. })
  74. })
  75. test('has profile icon : miss)', () => {
  76. ability.can = vi.fn(() => false)
  77. accessProfile.avatarId = 100
  78. accessProfile.gender = GENDER.MISS
  79. const result = menuBuilder.build() as MenuGroup
  80. expect(result.icon).toEqual({
  81. avatarId: 100,
  82. avatarByDefault: '/images/default/women-1.png',
  83. })
  84. })
  85. test('has no items', () => {
  86. ability.can = vi.fn(() => false)
  87. const group = menuBuilder.build()
  88. // AccountMenuBuilder retourne toujours un groupe
  89. // @ts-ignore
  90. expect(group.children).toEqual([])
  91. // Still has the logout action
  92. // @ts-ignore
  93. expect(group.actions).toEqual([
  94. {
  95. label: 'logout',
  96. icon: undefined,
  97. to: 'https://mydomain.com/#/logout',
  98. target: '_self',
  99. type: MENU_LINK_TYPE.V1,
  100. active: false,
  101. },
  102. ])
  103. })
  104. test('has only rights for menu my_schedule_page', () => {
  105. ability.can = vi.fn(
  106. (action: string, subject: string) =>
  107. action === 'display' && subject === 'my_schedule_page',
  108. )
  109. // @ts-ignore
  110. expect(menuBuilder.build().children[0]).toEqual({
  111. label: 'my_schedule_page',
  112. icon: undefined,
  113. to: 'https://mydomain.com/#/my_calendar',
  114. target: '_self',
  115. type: MENU_LINK_TYPE.V1,
  116. active: false,
  117. })
  118. })
  119. test('has only rights for menu attendance_bookings_menu', () => {
  120. ability.can = vi.fn(
  121. (action: string, subject: string) =>
  122. action === 'display' && subject === 'attendance_bookings_page',
  123. )
  124. // @ts-ignore
  125. expect(menuBuilder.build().children[0]).toEqual({
  126. label: 'attendance_bookings_menu',
  127. icon: undefined,
  128. to: 'https://mydomain.com/#/own_attendance',
  129. target: '_self',
  130. type: MENU_LINK_TYPE.V1,
  131. active: false,
  132. })
  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. expect(menuBuilder.build().children[0]).toEqual({
  141. label: 'my_attendance',
  142. icon: undefined,
  143. to: 'https://mydomain.com/#/my_attendances/list/',
  144. target: '_self',
  145. type: MENU_LINK_TYPE.V1,
  146. active: false,
  147. })
  148. })
  149. test('has only rights for menu my_invitation', () => {
  150. ability.can = vi.fn(
  151. (action: string, subject: string) =>
  152. action === 'display' && subject === 'my_invitation_page',
  153. )
  154. // @ts-ignore
  155. expect(menuBuilder.build().children[0]).toEqual({
  156. label: 'my_invitation',
  157. icon: undefined,
  158. to: 'https://mydomain.com/#/my_invitations/list/',
  159. target: '_self',
  160. type: MENU_LINK_TYPE.V1,
  161. active: false,
  162. })
  163. })
  164. test('has only rights for menu my_students', () => {
  165. ability.can = vi.fn(
  166. (action: string, subject: string) =>
  167. action === 'display' && subject === 'my_students_page',
  168. )
  169. // @ts-ignore
  170. expect(menuBuilder.build().children[0]).toEqual({
  171. label: 'my_students',
  172. icon: undefined,
  173. to: 'https://mydomain.com/#/my_students/list/',
  174. target: '_self',
  175. type: MENU_LINK_TYPE.V1,
  176. active: false,
  177. })
  178. })
  179. test('has only rights for menu my_students_education_students', () => {
  180. ability.can = vi.fn(
  181. (action: string, subject: string) =>
  182. action === 'display' &&
  183. subject === 'my_students_education_students_page',
  184. )
  185. // @ts-ignore
  186. expect(menuBuilder.build().children[0]).toEqual({
  187. label: 'my_students_education_students',
  188. icon: undefined,
  189. to: 'https://mydomain.com/#/my_students_education_students/list/',
  190. target: '_self',
  191. type: MENU_LINK_TYPE.V1,
  192. active: false,
  193. })
  194. })
  195. test('has only rights for menu my_education_students', () => {
  196. ability.can = vi.fn(
  197. (action: string, subject: string) =>
  198. action === 'display' && subject === 'my_education_students_page',
  199. )
  200. // @ts-ignore
  201. expect(menuBuilder.build().children[0]).toEqual({
  202. label: 'my_education_students',
  203. icon: undefined,
  204. to: 'https://mydomain.com/#/main/my_profile/123/dashboard/my_education_students/list/',
  205. target: '_self',
  206. type: MENU_LINK_TYPE.V1,
  207. active: false,
  208. })
  209. })
  210. test('has only rights for menu send_an_email', () => {
  211. ability.can = vi.fn(
  212. (action: string, subject: string) =>
  213. action === 'display' && subject === 'send_an_email_page',
  214. )
  215. // @ts-ignore
  216. expect(menuBuilder.build().children[0]).toEqual({
  217. label: 'send_an_email',
  218. icon: undefined,
  219. to: 'https://mydomain.com/#/list/create/emails',
  220. target: '_self',
  221. type: MENU_LINK_TYPE.V1,
  222. active: false,
  223. })
  224. })
  225. test('has only rights for menu my_documents', () => {
  226. ability.can = vi.fn(
  227. (action: string, subject: string) =>
  228. action === 'display' && subject === 'my_documents_page',
  229. )
  230. // @ts-ignore
  231. expect(menuBuilder.build().children[0]).toEqual({
  232. label: 'my_documents',
  233. icon: undefined,
  234. to: 'https://mydomain.com/#/main/my_profile/123/dashboard/show/my_access_file',
  235. target: '_self',
  236. type: MENU_LINK_TYPE.V1,
  237. active: false,
  238. })
  239. })
  240. test('has only rights for menu my_profile', () => {
  241. ability.can = vi.fn(
  242. (action: string, subject: string) =>
  243. action === 'display' && subject === 'my_profile_page',
  244. )
  245. // @ts-ignore
  246. expect(menuBuilder.build().children[0]).toEqual({
  247. label: 'my_profile',
  248. icon: undefined,
  249. to: 'https://mydomain.com/#/main/my_profile/123/dashboard',
  250. target: '_self',
  251. type: MENU_LINK_TYPE.V1,
  252. active: false,
  253. })
  254. })
  255. test('has only rights for menu adherent_list', () => {
  256. ability.can = vi.fn(
  257. (action: string, subject: string) =>
  258. action === 'display' && subject === 'adherent_list_page',
  259. )
  260. // @ts-ignore
  261. expect(menuBuilder.build().children[0]).toEqual({
  262. label: 'adherent_list',
  263. icon: undefined,
  264. to: 'https://mydomain.com/#/adherent_contacts/list/',
  265. target: '_self',
  266. type: MENU_LINK_TYPE.V1,
  267. active: false,
  268. })
  269. })
  270. test('has only rights for menu subscription', () => {
  271. ability.can = vi.fn(
  272. (action: string, subject: string) =>
  273. action === 'display' && subject === 'subscription_page',
  274. )
  275. // @ts-ignore
  276. router.resolve = vi.fn(() => {
  277. return { href: 'subscription' }
  278. })
  279. // @ts-ignore
  280. expect(menuBuilder.build().children[0]).toEqual({
  281. label: 'subscription_page',
  282. icon: undefined,
  283. to: 'subscription',
  284. type: MENU_LINK_TYPE.INTERNAL,
  285. active: false,
  286. })
  287. expect(router.resolve).toHaveBeenCalledWith({ name: 'subscription_page' })
  288. })
  289. test('has only rights for menu my_bills', () => {
  290. ability.can = vi.fn(
  291. (action: string, subject: string) =>
  292. action === 'display' && subject === 'my_bills_page',
  293. )
  294. // @ts-ignore
  295. expect(menuBuilder.build().children[0]).toEqual({
  296. label: 'my_bills',
  297. icon: undefined,
  298. to: 'https://mydomain.com/#/main/my_profile/123/dashboard/show/my_bills',
  299. target: '_self',
  300. type: MENU_LINK_TYPE.V1,
  301. active: false,
  302. })
  303. })
  304. test('has only rights for menu print_my_licence', () => {
  305. ability.can = vi.fn(
  306. (action: string, subject: string) =>
  307. action === 'display' && subject === 'cmf_licence_person_page',
  308. )
  309. // @ts-ignore
  310. expect(menuBuilder.build().children[0]).toEqual({
  311. label: 'print_my_licence',
  312. icon: undefined,
  313. to: 'https://mydomain.com/#/licence_cmf/user',
  314. target: '_self',
  315. type: MENU_LINK_TYPE.V1,
  316. active: false,
  317. })
  318. })
  319. })