accountMenuBuilder.test.ts 8.9 KB

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