accountMenuBuilder.test.ts 9.6 KB

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